1 | /* $Id: SrvIntNetR0.cpp 45717 2013-04-24 18:57:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Internal networking - The ring 0 service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SRV_INTNET
|
---|
23 | #include <VBox/intnet.h>
|
---|
24 | #include <VBox/intnetinline.h>
|
---|
25 | #include <VBox/vmm/pdmnetinline.h>
|
---|
26 | #include <VBox/sup.h>
|
---|
27 | #include <VBox/vmm/pdm.h>
|
---|
28 | #include <VBox/log.h>
|
---|
29 |
|
---|
30 | #include <iprt/asm.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/handletable.h>
|
---|
33 | #include <iprt/mp.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/net.h>
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Defined Constants And Macros *
|
---|
45 | *******************************************************************************/
|
---|
46 | /** @def INTNET_WITH_DHCP_SNOOPING
|
---|
47 | * Enabled DHCP snooping when in shared-mac-on-the-wire mode. */
|
---|
48 | #define INTNET_WITH_DHCP_SNOOPING
|
---|
49 |
|
---|
50 | /** The maximum number of interface in a network. */
|
---|
51 | #define INTNET_MAX_IFS (1023 + 1 + 16)
|
---|
52 |
|
---|
53 | /** The number of entries to grow the destination tables with. */
|
---|
54 | #if 0
|
---|
55 | # define INTNET_GROW_DSTTAB_SIZE 16
|
---|
56 | #else
|
---|
57 | # define INTNET_GROW_DSTTAB_SIZE 1
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | /** The wakeup bit in the INTNETIF::cBusy and INTNETRUNKIF::cBusy counters. */
|
---|
61 | #define INTNET_BUSY_WAKEUP_MASK RT_BIT_32(30)
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Structures and Typedefs *
|
---|
66 | *******************************************************************************/
|
---|
67 | /**
|
---|
68 | * MAC address lookup table entry.
|
---|
69 | */
|
---|
70 | typedef struct INTNETMACTABENTRY
|
---|
71 | {
|
---|
72 | /** The MAC address of this entry. */
|
---|
73 | RTMAC MacAddr;
|
---|
74 | /** Is it is effectively promiscuous mode. */
|
---|
75 | bool fPromiscuousEff;
|
---|
76 | /** Is it promiscuous and should it see unrelated trunk traffic. */
|
---|
77 | bool fPromiscuousSeeTrunk;
|
---|
78 | /** Is it active.
|
---|
79 | * We ignore the entry if this is clear and may end up sending packets addressed
|
---|
80 | * to this interface onto the trunk. The reasoning for this is that this could
|
---|
81 | * be the interface of a VM that just has been teleported to a different host. */
|
---|
82 | bool fActive;
|
---|
83 | /** Pointer to the network interface. */
|
---|
84 | struct INTNETIF *pIf;
|
---|
85 | } INTNETMACTABENTRY;
|
---|
86 | /** Pointer to a MAC address lookup table entry. */
|
---|
87 | typedef INTNETMACTABENTRY *PINTNETMACTABENTRY;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * MAC address lookup table.
|
---|
91 | *
|
---|
92 | * @todo Having this in a separate structure didn't work out as well as it
|
---|
93 | * should. Consider merging it into INTNETNETWORK.
|
---|
94 | */
|
---|
95 | typedef struct INTNETMACTAB
|
---|
96 | {
|
---|
97 | /** The current number of entries. */
|
---|
98 | uint32_t cEntries;
|
---|
99 | /** The number of entries we've allocated space for. */
|
---|
100 | uint32_t cEntriesAllocated;
|
---|
101 | /** Table entries. */
|
---|
102 | PINTNETMACTABENTRY paEntries;
|
---|
103 |
|
---|
104 | /** The number of interface entries currently in promicuous mode. */
|
---|
105 | uint32_t cPromiscuousEntries;
|
---|
106 | /** The number of interface entries currently in promicuous mode that
|
---|
107 | * shall not see unrelated trunk traffic. */
|
---|
108 | uint32_t cPromiscuousNoTrunkEntries;
|
---|
109 |
|
---|
110 | /** The host MAC address (reported). */
|
---|
111 | RTMAC HostMac;
|
---|
112 | /** The effective host promiscuous setting (reported). */
|
---|
113 | bool fHostPromiscuousEff;
|
---|
114 | /** The real host promiscuous setting (reported). */
|
---|
115 | bool fHostPromiscuousReal;
|
---|
116 | /** Whether the host is active. */
|
---|
117 | bool fHostActive;
|
---|
118 |
|
---|
119 | /** Whether the wire is promiscuous (config). */
|
---|
120 | bool fWirePromiscuousEff;
|
---|
121 | /** Whether the wire is promiscuous (config).
|
---|
122 | * (Shadows INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE in
|
---|
123 | * INTNETNETWORK::fFlags.) */
|
---|
124 | bool fWirePromiscuousReal;
|
---|
125 | /** Whether the wire is active. */
|
---|
126 | bool fWireActive;
|
---|
127 |
|
---|
128 | /** Pointer to the trunk interface. */
|
---|
129 | struct INTNETTRUNKIF *pTrunk;
|
---|
130 | } INTNETMACTAB;
|
---|
131 | /** Pointer to a MAC address . */
|
---|
132 | typedef INTNETMACTAB *PINTNETMACTAB;
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Destination table.
|
---|
136 | */
|
---|
137 | typedef struct INTNETDSTTAB
|
---|
138 | {
|
---|
139 | /** The trunk destinations. */
|
---|
140 | uint32_t fTrunkDst;
|
---|
141 | /** Pointer to the trunk interface (referenced) if fTrunkDst is non-zero. */
|
---|
142 | struct INTNETTRUNKIF *pTrunk;
|
---|
143 | /** The number of destination interfaces. */
|
---|
144 | uint32_t cIfs;
|
---|
145 | /** The interfaces (referenced). Variable sized array. */
|
---|
146 | struct
|
---|
147 | {
|
---|
148 | /** The destination interface. */
|
---|
149 | struct INTNETIF *pIf;
|
---|
150 | /** Whether to replace the destination MAC address.
|
---|
151 | * This is used when sharing MAC address with the host on the wire(less). */
|
---|
152 | bool fReplaceDstMac;
|
---|
153 | } aIfs[1];
|
---|
154 | } INTNETDSTTAB;
|
---|
155 | /** Pointer to a destination table. */
|
---|
156 | typedef INTNETDSTTAB *PINTNETDSTTAB;
|
---|
157 | /** Pointer to a const destination table. */
|
---|
158 | typedef INTNETDSTTAB const *PCINTNETDSTTAB;
|
---|
159 |
|
---|
160 |
|
---|
161 | /** Network layer address type. */
|
---|
162 | typedef enum INTNETADDRTYPE
|
---|
163 | {
|
---|
164 | /** The invalid 0 entry. */
|
---|
165 | kIntNetAddrType_Invalid = 0,
|
---|
166 | /** IP version 4. */
|
---|
167 | kIntNetAddrType_IPv4,
|
---|
168 | /** IP version 6. */
|
---|
169 | kIntNetAddrType_IPv6,
|
---|
170 | /** IPX. */
|
---|
171 | kIntNetAddrType_IPX,
|
---|
172 | /** The end of the valid values. */
|
---|
173 | kIntNetAddrType_End,
|
---|
174 | /** The usual 32-bit hack. */
|
---|
175 | kIntNetAddrType_32BitHack = 0x7fffffff
|
---|
176 | } INTNETADDRTYPE;
|
---|
177 | /** Pointer to a network layer address type. */
|
---|
178 | typedef INTNETADDRTYPE *PINTNETADDRTYPE;
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Address and type.
|
---|
183 | */
|
---|
184 | typedef struct INTNETADDR
|
---|
185 | {
|
---|
186 | /** The address type. */
|
---|
187 | INTNETADDRTYPE enmType;
|
---|
188 | /** The address. */
|
---|
189 | RTNETADDRU Addr;
|
---|
190 | } INTNETADDR;
|
---|
191 | /** Pointer to an address. */
|
---|
192 | typedef INTNETADDR *PINTNETADDR;
|
---|
193 | /** Pointer to a const address. */
|
---|
194 | typedef INTNETADDR const *PCINTNETADDR;
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Address cache for a specific network layer.
|
---|
199 | */
|
---|
200 | typedef struct INTNETADDRCACHE
|
---|
201 | {
|
---|
202 | /** Pointer to the table of addresses. */
|
---|
203 | uint8_t *pbEntries;
|
---|
204 | /** The number of valid address entries. */
|
---|
205 | uint8_t cEntries;
|
---|
206 | /** The number of allocated address entries. */
|
---|
207 | uint8_t cEntriesAlloc;
|
---|
208 | /** The address size. */
|
---|
209 | uint8_t cbAddress;
|
---|
210 | /** The size of an entry. */
|
---|
211 | uint8_t cbEntry;
|
---|
212 | } INTNETADDRCACHE;
|
---|
213 | /** Pointer to an address cache. */
|
---|
214 | typedef INTNETADDRCACHE *PINTNETADDRCACHE;
|
---|
215 | /** Pointer to a const address cache. */
|
---|
216 | typedef INTNETADDRCACHE const *PCINTNETADDRCACHE;
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * A network interface.
|
---|
221 | *
|
---|
222 | * Unless explicitly stated, all members are protect by the network semaphore.
|
---|
223 | */
|
---|
224 | typedef struct INTNETIF
|
---|
225 | {
|
---|
226 | /** The MAC address.
|
---|
227 | * This is shadowed by INTNETMACTABENTRY::MacAddr. */
|
---|
228 | RTMAC MacAddr;
|
---|
229 | /** Set if the INTNET::MacAddr member has been explicitly set. */
|
---|
230 | bool fMacSet;
|
---|
231 | /** Tracks the desired promiscuous setting of the interface. */
|
---|
232 | bool fPromiscuousReal;
|
---|
233 | /** Whether the interface is active or not.
|
---|
234 | * This is shadowed by INTNETMACTABENTRY::fActive. */
|
---|
235 | bool fActive;
|
---|
236 | /** Whether someone is currently in the destructor or has indicated that
|
---|
237 | * the end is nigh by means of IntNetR0IfAbortWait. */
|
---|
238 | bool volatile fDestroying;
|
---|
239 | /** The flags specified when opening this interface. */
|
---|
240 | uint32_t fOpenFlags;
|
---|
241 | /** Number of yields done to try make the interface read pending data.
|
---|
242 | * We will stop yielding when this reaches a threshold assuming that the VM is
|
---|
243 | * paused or that it simply isn't worth all the delay. It is cleared when a
|
---|
244 | * successful send has been done. */
|
---|
245 | uint32_t cYields;
|
---|
246 | /** Pointer to the current exchange buffer (ring-0). */
|
---|
247 | PINTNETBUF pIntBuf;
|
---|
248 | /** Pointer to ring-3 mapping of the current exchange buffer. */
|
---|
249 | R3PTRTYPE(PINTNETBUF) pIntBufR3;
|
---|
250 | /** Pointer to the default exchange buffer for the interface. */
|
---|
251 | PINTNETBUF pIntBufDefault;
|
---|
252 | /** Pointer to ring-3 mapping of the default exchange buffer. */
|
---|
253 | R3PTRTYPE(PINTNETBUF) pIntBufDefaultR3;
|
---|
254 | /** Event semaphore which a receiver/consumer thread will sleep on while
|
---|
255 | * waiting for data to arrive. */
|
---|
256 | RTSEMEVENT volatile hRecvEvent;
|
---|
257 | /** Number of threads sleeping on the event semaphore. */
|
---|
258 | uint32_t cSleepers;
|
---|
259 | /** The interface handle.
|
---|
260 | * When this is INTNET_HANDLE_INVALID a sleeper which is waking up
|
---|
261 | * should return with the appropriate error condition. */
|
---|
262 | INTNETIFHANDLE volatile hIf;
|
---|
263 | /** Pointer to the network this interface is connected to.
|
---|
264 | * This is protected by the INTNET::hMtxCreateOpenDestroy. */
|
---|
265 | struct INTNETNETWORK *pNetwork;
|
---|
266 | /** The session this interface is associated with. */
|
---|
267 | PSUPDRVSESSION pSession;
|
---|
268 | /** The SUPR0 object id. */
|
---|
269 | void *pvObj;
|
---|
270 | /** The network layer address cache. (Indexed by type, 0 entry isn't used.)
|
---|
271 | * This is protected by the address spinlock of the network. */
|
---|
272 | INTNETADDRCACHE aAddrCache[kIntNetAddrType_End];
|
---|
273 | /** Spinlock protecting the input (producer) side of the receive ring. */
|
---|
274 | RTSPINLOCK hRecvInSpinlock;
|
---|
275 | /** Busy count for tracking destination table references and active sends.
|
---|
276 | * Usually incremented while owning the switch table spinlock. The 30th bit
|
---|
277 | * is used to indicate wakeup. */
|
---|
278 | uint32_t volatile cBusy;
|
---|
279 | /** The preallocated destination table.
|
---|
280 | * This is NULL when it's in use as a precaution against unserialized
|
---|
281 | * transmitting. This is grown when new interfaces are added to the network. */
|
---|
282 | PINTNETDSTTAB volatile pDstTab;
|
---|
283 | /** Pointer to the trunk's per interface data. Can be NULL. */
|
---|
284 | void *pvIfData;
|
---|
285 | /** Header buffer for when we're carving GSO frames. */
|
---|
286 | uint8_t abGsoHdrs[256];
|
---|
287 | } INTNETIF;
|
---|
288 | /** Pointer to an internal network interface. */
|
---|
289 | typedef INTNETIF *PINTNETIF;
|
---|
290 |
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * A trunk interface.
|
---|
294 | */
|
---|
295 | typedef struct INTNETTRUNKIF
|
---|
296 | {
|
---|
297 | /** The port interface we present to the component. */
|
---|
298 | INTNETTRUNKSWPORT SwitchPort;
|
---|
299 | /** The port interface we get from the component. */
|
---|
300 | PINTNETTRUNKIFPORT pIfPort;
|
---|
301 | /** Pointer to the network we're connect to.
|
---|
302 | * This may be NULL if we're orphaned? */
|
---|
303 | struct INTNETNETWORK *pNetwork;
|
---|
304 | /** The current MAC address for the interface. (reported)
|
---|
305 | * Updated while owning the switch table spinlock. */
|
---|
306 | RTMAC MacAddr;
|
---|
307 | /** Whether to supply physical addresses with the outbound SGs. (reported) */
|
---|
308 | bool fPhysSG;
|
---|
309 | /** Explicit alignment. */
|
---|
310 | bool fUnused;
|
---|
311 | /** Busy count for tracking destination table references and active sends.
|
---|
312 | * Usually incremented while owning the switch table spinlock. The 30th bit
|
---|
313 | * is used to indicate wakeup. */
|
---|
314 | uint32_t volatile cBusy;
|
---|
315 | /** Mask of destinations that pfnXmit cope with disabled preemption for. */
|
---|
316 | uint32_t fNoPreemptDsts;
|
---|
317 | /** The GSO capabilities of the wire destination. (reported) */
|
---|
318 | uint32_t fWireGsoCapabilites;
|
---|
319 | /** The GSO capabilities of the host destination. (reported)
|
---|
320 | * This is as bit map where each bit represents the GSO type with the same
|
---|
321 | * number. */
|
---|
322 | uint32_t fHostGsoCapabilites;
|
---|
323 | /** The destination table spinlock, interrupt safe.
|
---|
324 | * Protects apTaskDstTabs and apIntDstTabs. */
|
---|
325 | RTSPINLOCK hDstTabSpinlock;
|
---|
326 | /** The number of entries in apIntDstTabs. */
|
---|
327 | uint32_t cIntDstTabs;
|
---|
328 | /** The task time destination tables.
|
---|
329 | * @remarks intnetR0NetworkEnsureTabSpace and others ASSUMES this immediately
|
---|
330 | * precedes apIntDstTabs so that these two tables can be used as one
|
---|
331 | * contiguous one. */
|
---|
332 | PINTNETDSTTAB apTaskDstTabs[2];
|
---|
333 | /** The interrupt / disabled-preemption time destination tables.
|
---|
334 | * This is a variable sized array. */
|
---|
335 | PINTNETDSTTAB apIntDstTabs[1];
|
---|
336 | } INTNETTRUNKIF;
|
---|
337 | /** Pointer to a trunk interface. */
|
---|
338 | typedef INTNETTRUNKIF *PINTNETTRUNKIF;
|
---|
339 |
|
---|
340 | /** Converts a pointer to INTNETTRUNKIF::SwitchPort to a PINTNETTRUNKIF. */
|
---|
341 | #define INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort) ((PINTNETTRUNKIF)(pSwitchPort))
|
---|
342 |
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Internal representation of a network.
|
---|
346 | */
|
---|
347 | typedef struct INTNETNETWORK
|
---|
348 | {
|
---|
349 | /** The Next network in the chain.
|
---|
350 | * This is protected by the INTNET::hMtxCreateOpenDestroy. */
|
---|
351 | struct INTNETNETWORK *pNext;
|
---|
352 |
|
---|
353 | /** The spinlock protecting MacTab and INTNETTRUNKIF::aAddrCache.
|
---|
354 | * Interrupt safe. */
|
---|
355 | RTSPINLOCK hAddrSpinlock;
|
---|
356 | /** MAC address table.
|
---|
357 | * This doubles as interface collection. */
|
---|
358 | INTNETMACTAB MacTab;
|
---|
359 |
|
---|
360 | /** Wait for an interface to stop being busy so it can be removed or have its
|
---|
361 | * destination table replaced. We have to wait upon this while owning the
|
---|
362 | * network mutex. Will only ever have one waiter because of the big mutex. */
|
---|
363 | RTSEMEVENT hEvtBusyIf;
|
---|
364 | /** Pointer to the instance data. */
|
---|
365 | struct INTNET *pIntNet;
|
---|
366 | /** The SUPR0 object id. */
|
---|
367 | void *pvObj;
|
---|
368 | /** Pointer to the temporary buffer that is used when snooping fragmented packets.
|
---|
369 | * This is allocated after this structure if we're sharing the MAC address with
|
---|
370 | * the host. The buffer is INTNETNETWORK_TMP_SIZE big and aligned on a 64-byte boundary. */
|
---|
371 | uint8_t *pbTmp;
|
---|
372 | /** Network creation flags (INTNET_OPEN_FLAGS_*). */
|
---|
373 | uint32_t fFlags;
|
---|
374 | /** Any restrictive policies required as a minimum by some interface.
|
---|
375 | * (INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES) */
|
---|
376 | uint32_t fMinFlags;
|
---|
377 | /** The number of active interfaces (excluding the trunk). */
|
---|
378 | uint32_t cActiveIFs;
|
---|
379 | /** The length of the network name. */
|
---|
380 | uint8_t cchName;
|
---|
381 | /** The network name. */
|
---|
382 | char szName[INTNET_MAX_NETWORK_NAME];
|
---|
383 | /** The trunk type. */
|
---|
384 | INTNETTRUNKTYPE enmTrunkType;
|
---|
385 | /** The trunk name. */
|
---|
386 | char szTrunk[INTNET_MAX_TRUNK_NAME];
|
---|
387 | } INTNETNETWORK;
|
---|
388 | /** Pointer to an internal network. */
|
---|
389 | typedef INTNETNETWORK *PINTNETNETWORK;
|
---|
390 | /** Pointer to a const internal network. */
|
---|
391 | typedef const INTNETNETWORK *PCINTNETNETWORK;
|
---|
392 |
|
---|
393 | /** The size of the buffer INTNETNETWORK::pbTmp points at. */
|
---|
394 | #define INTNETNETWORK_TMP_SIZE 2048
|
---|
395 |
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Internal networking instance.
|
---|
399 | */
|
---|
400 | typedef struct INTNET
|
---|
401 | {
|
---|
402 | /** Magic number (INTNET_MAGIC). */
|
---|
403 | uint32_t volatile u32Magic;
|
---|
404 | /** Mutex protecting the creation, opening and destruction of both networks and
|
---|
405 | * interfaces. (This means all operations affecting the pNetworks list.) */
|
---|
406 | RTSEMMUTEX hMtxCreateOpenDestroy;
|
---|
407 | /** List of networks. Protected by INTNET::Spinlock. */
|
---|
408 | PINTNETNETWORK volatile pNetworks;
|
---|
409 | /** Handle table for the interfaces. */
|
---|
410 | RTHANDLETABLE hHtIfs;
|
---|
411 | } INTNET;
|
---|
412 | /** Pointer to an internal network ring-0 instance. */
|
---|
413 | typedef struct INTNET *PINTNET;
|
---|
414 |
|
---|
415 | /** Magic number for the internal network instance data (Hayao Miyazaki). */
|
---|
416 | #define INTNET_MAGIC UINT32_C(0x19410105)
|
---|
417 |
|
---|
418 |
|
---|
419 | /*******************************************************************************
|
---|
420 | * Global Variables *
|
---|
421 | *******************************************************************************/
|
---|
422 | /** Pointer to the internal network instance data. */
|
---|
423 | static PINTNET volatile g_pIntNet = NULL;
|
---|
424 |
|
---|
425 | static const struct INTNETOPENNETWORKFLAGS
|
---|
426 | {
|
---|
427 | uint32_t fRestrictive; /**< The restrictive flag (deny/disabled). */
|
---|
428 | uint32_t fRelaxed; /**< The relaxed flag (allow/enabled). */
|
---|
429 | uint32_t fFixed; /**< The config-fixed flag. */
|
---|
430 | uint32_t fPair; /**< The pair of restrictive and relaxed flags. */
|
---|
431 | }
|
---|
432 | /** Open network policy flags relating to the network. */
|
---|
433 | g_afIntNetOpenNetworkNetFlags[] =
|
---|
434 | {
|
---|
435 | { INTNET_OPEN_FLAGS_ACCESS_RESTRICTED, INTNET_OPEN_FLAGS_ACCESS_PUBLIC, INTNET_OPEN_FLAGS_ACCESS_FIXED, INTNET_OPEN_FLAGS_ACCESS_RESTRICTED | INTNET_OPEN_FLAGS_ACCESS_PUBLIC },
|
---|
436 | { INTNET_OPEN_FLAGS_PROMISC_DENY_CLIENTS, INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_CLIENTS | INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS },
|
---|
437 | { INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_HOST, INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_HOST | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST },
|
---|
438 | { INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_WIRE, INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_WIRE | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE },
|
---|
439 | { INTNET_OPEN_FLAGS_TRUNK_HOST_DISABLED, INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_HOST_DISABLED | INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED },
|
---|
440 | { INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE, INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE | INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE },
|
---|
441 | { INTNET_OPEN_FLAGS_TRUNK_WIRE_DISABLED, INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_WIRE_DISABLED | INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED },
|
---|
442 | { INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE, INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE | INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE },
|
---|
443 | },
|
---|
444 | /** Open network policy flags relating to the new interface. */
|
---|
445 | g_afIntNetOpenNetworkIfFlags[] =
|
---|
446 | {
|
---|
447 | { INTNET_OPEN_FLAGS_IF_PROMISC_DENY, INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW, INTNET_OPEN_FLAGS_IF_FIXED, INTNET_OPEN_FLAGS_IF_PROMISC_DENY | INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW },
|
---|
448 | { INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK, INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK, INTNET_OPEN_FLAGS_IF_FIXED, INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK },
|
---|
449 | };
|
---|
450 |
|
---|
451 |
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Worker for intnetR0SgWritePart that deals with the case where the
|
---|
455 | * request doesn't fit into the first segment.
|
---|
456 | *
|
---|
457 | * @returns true, unless the request or SG invalid.
|
---|
458 | * @param pSG The SG list to write to.
|
---|
459 | * @param off Where to start writing (offset into the SG).
|
---|
460 | * @param cb How much to write.
|
---|
461 | * @param pvBuf The buffer to containing the bits to write.
|
---|
462 | */
|
---|
463 | static bool intnetR0SgWritePartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
|
---|
464 | {
|
---|
465 | if (RT_UNLIKELY(off + cb > pSG->cbTotal))
|
---|
466 | return false;
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * Skip ahead to the segment where off starts.
|
---|
470 | */
|
---|
471 | unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
|
---|
472 | unsigned iSeg = 0;
|
---|
473 | while (off > pSG->aSegs[iSeg].cb)
|
---|
474 | {
|
---|
475 | off -= pSG->aSegs[iSeg++].cb;
|
---|
476 | AssertReturn(iSeg < cSegs, false);
|
---|
477 | }
|
---|
478 |
|
---|
479 | /*
|
---|
480 | * Copy the data, hoping that it's all from one segment...
|
---|
481 | */
|
---|
482 | uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
|
---|
483 | if (cbCanCopy >= cb)
|
---|
484 | memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cb);
|
---|
485 | else
|
---|
486 | {
|
---|
487 | /* copy the portion in the current segment. */
|
---|
488 | memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cbCanCopy);
|
---|
489 | cb -= cbCanCopy;
|
---|
490 |
|
---|
491 | /* copy the portions in the other segments. */
|
---|
492 | do
|
---|
493 | {
|
---|
494 | pvBuf = (uint8_t const *)pvBuf + cbCanCopy;
|
---|
495 | iSeg++;
|
---|
496 | AssertReturn(iSeg < cSegs, false);
|
---|
497 |
|
---|
498 | cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
|
---|
499 | memcpy(pSG->aSegs[iSeg].pv, pvBuf, cbCanCopy);
|
---|
500 |
|
---|
501 | cb -= cbCanCopy;
|
---|
502 | } while (cb > 0);
|
---|
503 | }
|
---|
504 |
|
---|
505 | return true;
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Writes to a part of an SG.
|
---|
511 | *
|
---|
512 | * @returns true on success, false on failure (out of bounds).
|
---|
513 | * @param pSG The SG list to write to.
|
---|
514 | * @param off Where to start writing (offset into the SG).
|
---|
515 | * @param cb How much to write.
|
---|
516 | * @param pvBuf The buffer to containing the bits to write.
|
---|
517 | */
|
---|
518 | DECLINLINE(bool) intnetR0SgWritePart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
|
---|
519 | {
|
---|
520 | Assert(off + cb > off);
|
---|
521 |
|
---|
522 | /* The optimized case. */
|
---|
523 | if (RT_LIKELY( pSG->cSegsUsed == 1
|
---|
524 | || pSG->aSegs[0].cb >= off + cb))
|
---|
525 | {
|
---|
526 | Assert(pSG->cbTotal == pSG->aSegs[0].cb);
|
---|
527 | memcpy((uint8_t *)pSG->aSegs[0].pv + off, pvBuf, cb);
|
---|
528 | return true;
|
---|
529 | }
|
---|
530 | return intnetR0SgWritePartSlow(pSG, off, cb, pvBuf);
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Reads a byte from a SG list.
|
---|
536 | *
|
---|
537 | * @returns The byte on success. 0xff on failure.
|
---|
538 | * @param pSG The SG list to read.
|
---|
539 | * @param off The offset (into the SG) off the byte.
|
---|
540 | */
|
---|
541 | DECLINLINE(uint8_t) intnetR0SgReadByte(PCINTNETSG pSG, uint32_t off)
|
---|
542 | {
|
---|
543 | if (RT_LIKELY(pSG->aSegs[0].cb > off))
|
---|
544 | return ((uint8_t const *)pSG->aSegs[0].pv)[off];
|
---|
545 |
|
---|
546 | off -= pSG->aSegs[0].cb;
|
---|
547 | unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
|
---|
548 | for (unsigned iSeg = 1; iSeg < cSegs; iSeg++)
|
---|
549 | {
|
---|
550 | if (pSG->aSegs[iSeg].cb > off)
|
---|
551 | return ((uint8_t const *)pSG->aSegs[iSeg].pv)[off];
|
---|
552 | off -= pSG->aSegs[iSeg].cb;
|
---|
553 | }
|
---|
554 | return false;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Worker for intnetR0SgReadPart that deals with the case where the
|
---|
560 | * requested data isn't in the first segment.
|
---|
561 | *
|
---|
562 | * @returns true, unless the SG is invalid.
|
---|
563 | * @param pSG The SG list to read.
|
---|
564 | * @param off Where to start reading (offset into the SG).
|
---|
565 | * @param cb How much to read.
|
---|
566 | * @param pvBuf The buffer to read into.
|
---|
567 | */
|
---|
568 | static bool intnetR0SgReadPartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
|
---|
569 | {
|
---|
570 | if (RT_UNLIKELY(off + cb > pSG->cbTotal))
|
---|
571 | return false;
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Skip ahead to the segment where off starts.
|
---|
575 | */
|
---|
576 | unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
|
---|
577 | unsigned iSeg = 0;
|
---|
578 | while (off > pSG->aSegs[iSeg].cb)
|
---|
579 | {
|
---|
580 | off -= pSG->aSegs[iSeg++].cb;
|
---|
581 | AssertReturn(iSeg < cSegs, false);
|
---|
582 | }
|
---|
583 |
|
---|
584 | /*
|
---|
585 | * Copy the data, hoping that it's all from one segment...
|
---|
586 | */
|
---|
587 | uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
|
---|
588 | if (cbCanCopy >= cb)
|
---|
589 | memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cb);
|
---|
590 | else
|
---|
591 | {
|
---|
592 | /* copy the portion in the current segment. */
|
---|
593 | memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cbCanCopy);
|
---|
594 | cb -= cbCanCopy;
|
---|
595 |
|
---|
596 | /* copy the portions in the other segments. */
|
---|
597 | do
|
---|
598 | {
|
---|
599 | pvBuf = (uint8_t *)pvBuf + cbCanCopy;
|
---|
600 | iSeg++;
|
---|
601 | AssertReturn(iSeg < cSegs, false);
|
---|
602 |
|
---|
603 | cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
|
---|
604 | memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv, cbCanCopy);
|
---|
605 |
|
---|
606 | cb -= cbCanCopy;
|
---|
607 | } while (cb > 0);
|
---|
608 | }
|
---|
609 |
|
---|
610 | return true;
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Reads a part of an SG into a buffer.
|
---|
616 | *
|
---|
617 | * @returns true on success, false on failure (out of bounds).
|
---|
618 | * @param pSG The SG list to read.
|
---|
619 | * @param off Where to start reading (offset into the SG).
|
---|
620 | * @param cb How much to read.
|
---|
621 | * @param pvBuf The buffer to read into.
|
---|
622 | */
|
---|
623 | DECLINLINE(bool) intnetR0SgReadPart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
|
---|
624 | {
|
---|
625 | Assert(off + cb > off);
|
---|
626 |
|
---|
627 | /* The optimized case. */
|
---|
628 | if (RT_LIKELY( pSG->cSegsUsed == 1
|
---|
629 | || pSG->aSegs[0].cb >= off + cb))
|
---|
630 | {
|
---|
631 | Assert(pSG->cbTotal == pSG->aSegs[0].cb);
|
---|
632 | memcpy(pvBuf, (uint8_t const *)pSG->aSegs[0].pv + off, cb);
|
---|
633 | return true;
|
---|
634 | }
|
---|
635 | return intnetR0SgReadPartSlow(pSG, off, cb, pvBuf);
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * Wait for a busy counter to reach zero.
|
---|
641 | *
|
---|
642 | * @param pNetwork The network.
|
---|
643 | * @param pcBusy The busy counter.
|
---|
644 | */
|
---|
645 | static void intnetR0BusyWait(PINTNETNETWORK pNetwork, uint32_t volatile *pcBusy)
|
---|
646 | {
|
---|
647 | if (ASMAtomicReadU32(pcBusy) == 0)
|
---|
648 | return;
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * We have to be a bit cautious here so we don't destroy the network or the
|
---|
652 | * semaphore before intnetR0BusyDec has signalled us.
|
---|
653 | */
|
---|
654 |
|
---|
655 | /* Reset the semaphore and flip the wakeup bit. */
|
---|
656 | RTSemEventWait(pNetwork->hEvtBusyIf, 0); /* clear it */
|
---|
657 | uint32_t cCurBusy = ASMAtomicReadU32(pcBusy);
|
---|
658 | do
|
---|
659 | {
|
---|
660 | if (cCurBusy == 0)
|
---|
661 | return;
|
---|
662 | AssertMsg(!(cCurBusy & INTNET_BUSY_WAKEUP_MASK), ("%#x\n", cCurBusy));
|
---|
663 | AssertMsg((cCurBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cCurBusy));
|
---|
664 | } while (!ASMAtomicCmpXchgExU32(pcBusy, cCurBusy | INTNET_BUSY_WAKEUP_MASK, cCurBusy, &cCurBusy));
|
---|
665 |
|
---|
666 | /* Wait for the count to reach zero. */
|
---|
667 | do
|
---|
668 | {
|
---|
669 | int rc2 = RTSemEventWait(pNetwork->hEvtBusyIf, 30000); NOREF(rc2);
|
---|
670 | //AssertMsg(RT_SUCCESS(rc2), ("rc=%Rrc *pcBusy=%#x (%#x)\n", rc2, ASMAtomicReadU32(pcBusy), cCurBusy ));
|
---|
671 | cCurBusy = ASMAtomicReadU32(pcBusy);
|
---|
672 | AssertMsg((cCurBusy & INTNET_BUSY_WAKEUP_MASK), ("%#x\n", cCurBusy));
|
---|
673 | AssertMsg((cCurBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cCurBusy));
|
---|
674 | } while ( cCurBusy != INTNET_BUSY_WAKEUP_MASK
|
---|
675 | || !ASMAtomicCmpXchgU32(pcBusy, 0, INTNET_BUSY_WAKEUP_MASK));
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Decrements the busy counter and maybe wakes up any threads waiting for it to
|
---|
681 | * reach zero.
|
---|
682 | *
|
---|
683 | * @param pNetwork The network.
|
---|
684 | * @param pcBusy The busy counter.
|
---|
685 | */
|
---|
686 | DECLINLINE(void) intnetR0BusyDec(PINTNETNETWORK pNetwork, uint32_t volatile *pcBusy)
|
---|
687 | {
|
---|
688 | uint32_t cNewBusy = ASMAtomicDecU32(pcBusy);
|
---|
689 | if (RT_UNLIKELY( cNewBusy == INTNET_BUSY_WAKEUP_MASK
|
---|
690 | && pNetwork))
|
---|
691 | RTSemEventSignal(pNetwork->hEvtBusyIf);
|
---|
692 | AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Increments the busy count of the specified interface.
|
---|
698 | *
|
---|
699 | * The caller must own the MAC address table spinlock.
|
---|
700 | *
|
---|
701 | * @param pIf The interface.
|
---|
702 | */
|
---|
703 | DECLINLINE(void) intnetR0BusyDecIf(PINTNETIF pIf)
|
---|
704 | {
|
---|
705 | intnetR0BusyDec(pIf->pNetwork, &pIf->cBusy);
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Increments the busy count of the specified interface.
|
---|
711 | *
|
---|
712 | * The caller must own the MAC address table spinlock or an explicity reference.
|
---|
713 | *
|
---|
714 | * @param pTrunk The trunk.
|
---|
715 | */
|
---|
716 | DECLINLINE(void) intnetR0BusyDecTrunk(PINTNETTRUNKIF pTrunk)
|
---|
717 | {
|
---|
718 | intnetR0BusyDec(pTrunk->pNetwork, &pTrunk->cBusy);
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * Increments the busy count of the specified interface.
|
---|
724 | *
|
---|
725 | * The caller must own the MAC address table spinlock or an explicity reference.
|
---|
726 | *
|
---|
727 | * @param pIf The interface.
|
---|
728 | */
|
---|
729 | DECLINLINE(void) intnetR0BusyIncIf(PINTNETIF pIf)
|
---|
730 | {
|
---|
731 | uint32_t cNewBusy = ASMAtomicIncU32(&pIf->cBusy);
|
---|
732 | AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
|
---|
733 | NOREF(cNewBusy);
|
---|
734 | }
|
---|
735 |
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Increments the busy count of the specified interface.
|
---|
739 | *
|
---|
740 | * The caller must own the MAC address table spinlock or an explicity reference.
|
---|
741 | *
|
---|
742 | * @param pTrunk The trunk.
|
---|
743 | */
|
---|
744 | DECLINLINE(void) intnetR0BusyIncTrunk(PINTNETTRUNKIF pTrunk)
|
---|
745 | {
|
---|
746 | uint32_t cNewBusy = ASMAtomicIncU32(&pTrunk->cBusy);
|
---|
747 | AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
|
---|
748 | NOREF(cNewBusy);
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Retain an interface.
|
---|
754 | *
|
---|
755 | * @returns VBox status code, can assume success in most situations.
|
---|
756 | * @param pIf The interface instance.
|
---|
757 | * @param pSession The current session.
|
---|
758 | */
|
---|
759 | DECLINLINE(int) intnetR0IfRetain(PINTNETIF pIf, PSUPDRVSESSION pSession)
|
---|
760 | {
|
---|
761 | int rc = SUPR0ObjAddRefEx(pIf->pvObj, pSession, true /* fNoBlocking */);
|
---|
762 | AssertRCReturn(rc, rc);
|
---|
763 | return VINF_SUCCESS;
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Release an interface previously retained by intnetR0IfRetain or
|
---|
769 | * by handle lookup/freeing.
|
---|
770 | *
|
---|
771 | * @returns true if destroyed, false if not.
|
---|
772 | * @param pIf The interface instance.
|
---|
773 | * @param pSession The current session.
|
---|
774 | */
|
---|
775 | DECLINLINE(bool) intnetR0IfRelease(PINTNETIF pIf, PSUPDRVSESSION pSession)
|
---|
776 | {
|
---|
777 | int rc = SUPR0ObjRelease(pIf->pvObj, pSession);
|
---|
778 | AssertRC(rc);
|
---|
779 | return rc == VINF_OBJECT_DESTROYED;
|
---|
780 | }
|
---|
781 |
|
---|
782 |
|
---|
783 | /**
|
---|
784 | * RTHandleCreateEx callback that retains an object in the
|
---|
785 | * handle table before returning it.
|
---|
786 | *
|
---|
787 | * (Avoids racing the freeing of the handle.)
|
---|
788 | *
|
---|
789 | * @returns VBox status code.
|
---|
790 | * @param hHandleTable The handle table (ignored).
|
---|
791 | * @param pvObj The object (INTNETIF).
|
---|
792 | * @param pvCtx The context (SUPDRVSESSION).
|
---|
793 | * @param pvUser The user context (ignored).
|
---|
794 | */
|
---|
795 | static DECLCALLBACK(int) intnetR0IfRetainHandle(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
|
---|
796 | {
|
---|
797 | NOREF(pvUser);
|
---|
798 | NOREF(hHandleTable);
|
---|
799 | PINTNETIF pIf = (PINTNETIF)pvObj;
|
---|
800 | if (pIf->hIf != INTNET_HANDLE_INVALID) /* Don't try retain it if called from intnetR0IfDestruct. */
|
---|
801 | return intnetR0IfRetain(pIf, (PSUPDRVSESSION)pvCtx);
|
---|
802 | return VINF_SUCCESS;
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Checks if the interface has a usable MAC address or not.
|
---|
809 | *
|
---|
810 | * @returns true if MacAddr is usable, false if not.
|
---|
811 | * @param pIf The interface.
|
---|
812 | */
|
---|
813 | DECL_FORCE_INLINE(bool) intnetR0IfHasMacAddr(PINTNETIF pIf)
|
---|
814 | {
|
---|
815 | return pIf->fMacSet || !(pIf->MacAddr.au8[0] & 1);
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Locates the MAC address table entry for the given interface.
|
---|
821 | *
|
---|
822 | * The caller holds the MAC address table spinlock, obviously.
|
---|
823 | *
|
---|
824 | * @returns Pointer to the entry on if found, NULL if not.
|
---|
825 | * @param pNetwork The network.
|
---|
826 | * @param pIf The interface.
|
---|
827 | */
|
---|
828 | DECLINLINE(PINTNETMACTABENTRY) intnetR0NetworkFindMacAddrEntry(PINTNETNETWORK pNetwork, PINTNETIF pIf)
|
---|
829 | {
|
---|
830 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
831 | while (iIf-- > 0)
|
---|
832 | {
|
---|
833 | if (pNetwork->MacTab.paEntries[iIf].pIf == pIf)
|
---|
834 | return &pNetwork->MacTab.paEntries[iIf];
|
---|
835 | }
|
---|
836 | return NULL;
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * Checks if the IPv6 address is a good interface address.
|
---|
842 | * @returns true/false.
|
---|
843 | * @param addr The address, network endian.
|
---|
844 | */
|
---|
845 | DECLINLINE(bool) intnetR0IPv6AddrIsGood(RTNETADDRIPV6 addr)
|
---|
846 | {
|
---|
847 | return !( ( addr.QWords.qw0 == 0 && addr.QWords.qw1 == 0) /* :: */
|
---|
848 | || ( (addr.Words.w0 & RT_H2BE_U16(0xff00)) == RT_H2BE_U16(0xff00)) /* multicast */
|
---|
849 | || ( addr.Words.w0 == 0 && addr.Words.w1 == 0
|
---|
850 | && addr.Words.w2 == 0 && addr.Words.w3 == 0
|
---|
851 | && addr.Words.w4 == 0 && addr.Words.w5 == 0
|
---|
852 | && addr.Words.w6 == 0 && addr.Words.w7 == RT_H2BE_U16(0x0001))); /* ::1 */
|
---|
853 | }
|
---|
854 |
|
---|
855 |
|
---|
856 | /**
|
---|
857 | * Checks if the IPv4 address is a broadcast address.
|
---|
858 | * @returns true/false.
|
---|
859 | * @param Addr The address, network endian.
|
---|
860 | */
|
---|
861 | DECLINLINE(bool) intnetR0IPv4AddrIsBroadcast(RTNETADDRIPV4 Addr)
|
---|
862 | {
|
---|
863 | /* Just check for 255.255.255.255 atm. */
|
---|
864 | return Addr.u == UINT32_MAX;
|
---|
865 | }
|
---|
866 |
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Checks if the IPv4 address is a good interface address.
|
---|
870 | * @returns true/false.
|
---|
871 | * @param Addr The address, network endian.
|
---|
872 | */
|
---|
873 | DECLINLINE(bool) intnetR0IPv4AddrIsGood(RTNETADDRIPV4 Addr)
|
---|
874 | {
|
---|
875 | /* Usual suspects. */
|
---|
876 | if ( Addr.u == UINT32_MAX /* 255.255.255.255 - broadcast. */
|
---|
877 | || Addr.au8[0] == 0) /* Current network, can be used as source address. */
|
---|
878 | return false;
|
---|
879 |
|
---|
880 | /* Unusual suspects. */
|
---|
881 | if (RT_UNLIKELY( Addr.au8[0] == 127 /* Loopback */
|
---|
882 | || (Addr.au8[0] & 0xf0) == 224 /* Multicast */
|
---|
883 | ))
|
---|
884 | return false;
|
---|
885 | return true;
|
---|
886 | }
|
---|
887 |
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Gets the address size of a network layer type.
|
---|
891 | *
|
---|
892 | * @returns size in bytes.
|
---|
893 | * @param enmType The type.
|
---|
894 | */
|
---|
895 | DECLINLINE(uint8_t) intnetR0AddrSize(INTNETADDRTYPE enmType)
|
---|
896 | {
|
---|
897 | switch (enmType)
|
---|
898 | {
|
---|
899 | case kIntNetAddrType_IPv4: return 4;
|
---|
900 | case kIntNetAddrType_IPv6: return 16;
|
---|
901 | case kIntNetAddrType_IPX: return 4 + 6;
|
---|
902 | default: AssertFailedReturn(0);
|
---|
903 | }
|
---|
904 | }
|
---|
905 |
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * Compares two address to see if they are equal, assuming naturally align structures.
|
---|
909 | *
|
---|
910 | * @returns true if equal, false if not.
|
---|
911 | * @param pAddr1 The first address.
|
---|
912 | * @param pAddr2 The second address.
|
---|
913 | * @param cbAddr The address size.
|
---|
914 | */
|
---|
915 | DECLINLINE(bool) intnetR0AddrUIsEqualEx(PCRTNETADDRU pAddr1, PCRTNETADDRU pAddr2, uint8_t const cbAddr)
|
---|
916 | {
|
---|
917 | switch (cbAddr)
|
---|
918 | {
|
---|
919 | case 4: /* IPv4 */
|
---|
920 | return pAddr1->au32[0] == pAddr2->au32[0];
|
---|
921 | case 16: /* IPv6 */
|
---|
922 | return pAddr1->au64[0] == pAddr2->au64[0]
|
---|
923 | && pAddr1->au64[1] == pAddr2->au64[1];
|
---|
924 | case 10: /* IPX */
|
---|
925 | return pAddr1->au64[0] == pAddr2->au64[0]
|
---|
926 | && pAddr1->au16[4] == pAddr2->au16[4];
|
---|
927 | default:
|
---|
928 | AssertFailedReturn(false);
|
---|
929 | }
|
---|
930 | }
|
---|
931 |
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * Worker for intnetR0IfAddrCacheLookup that performs the lookup
|
---|
935 | * in the remaining cache entries after the caller has check the
|
---|
936 | * most likely ones.
|
---|
937 | *
|
---|
938 | * @returns -1 if not found, the index of the cache entry if found.
|
---|
939 | * @param pCache The cache.
|
---|
940 | * @param pAddr The address.
|
---|
941 | * @param cbAddr The address size (optimization).
|
---|
942 | */
|
---|
943 | static int intnetR0IfAddrCacheLookupSlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
|
---|
944 | {
|
---|
945 | unsigned i = pCache->cEntries - 2;
|
---|
946 | uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
|
---|
947 | while (i >= 1)
|
---|
948 | {
|
---|
949 | if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr))
|
---|
950 | return i;
|
---|
951 | pbEntry -= pCache->cbEntry;
|
---|
952 | i--;
|
---|
953 | }
|
---|
954 |
|
---|
955 | return -1;
|
---|
956 | }
|
---|
957 |
|
---|
958 | /**
|
---|
959 | * Lookup an address in a cache without any expectations.
|
---|
960 | *
|
---|
961 | * @returns -1 if not found, the index of the cache entry if found.
|
---|
962 | * @param pCache The cache.
|
---|
963 | * @param pAddr The address.
|
---|
964 | * @param cbAddr The address size (optimization).
|
---|
965 | */
|
---|
966 | DECLINLINE(int) intnetR0IfAddrCacheLookup(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
|
---|
967 | {
|
---|
968 | Assert(pCache->cbAddress == cbAddr);
|
---|
969 |
|
---|
970 | /*
|
---|
971 | * The optimized case is when there is one cache entry and
|
---|
972 | * it doesn't match.
|
---|
973 | */
|
---|
974 | unsigned i = pCache->cEntries;
|
---|
975 | if ( i > 0
|
---|
976 | && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr))
|
---|
977 | return 0;
|
---|
978 | if (i <= 1)
|
---|
979 | return -1;
|
---|
980 |
|
---|
981 | /*
|
---|
982 | * Check the last entry.
|
---|
983 | */
|
---|
984 | i--;
|
---|
985 | if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))
|
---|
986 | return i;
|
---|
987 | if (i <= 1)
|
---|
988 | return -1;
|
---|
989 |
|
---|
990 | return intnetR0IfAddrCacheLookupSlow(pCache, pAddr, cbAddr);
|
---|
991 | }
|
---|
992 |
|
---|
993 |
|
---|
994 | /** Same as intnetR0IfAddrCacheLookup except we expect the address to be present already. */
|
---|
995 | DECLINLINE(int) intnetR0IfAddrCacheLookupLikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
|
---|
996 | {
|
---|
997 | /** @todo implement this. */
|
---|
998 | return intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | /**
|
---|
1003 | * Worker for intnetR0IfAddrCacheLookupUnlikely that performs
|
---|
1004 | * the lookup in the remaining cache entries after the caller
|
---|
1005 | * has check the most likely ones.
|
---|
1006 | *
|
---|
1007 | * The routine is expecting not to find the address.
|
---|
1008 | *
|
---|
1009 | * @returns -1 if not found, the index of the cache entry if found.
|
---|
1010 | * @param pCache The cache.
|
---|
1011 | * @param pAddr The address.
|
---|
1012 | * @param cbAddr The address size (optimization).
|
---|
1013 | */
|
---|
1014 | static int intnetR0IfAddrCacheInCacheUnlikelySlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
|
---|
1015 | {
|
---|
1016 | /*
|
---|
1017 | * Perform a full table lookup.
|
---|
1018 | */
|
---|
1019 | unsigned i = pCache->cEntries - 2;
|
---|
1020 | uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
|
---|
1021 | while (i >= 1)
|
---|
1022 | {
|
---|
1023 | if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
|
---|
1024 | return i;
|
---|
1025 | pbEntry -= pCache->cbEntry;
|
---|
1026 | i--;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | return -1;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Lookup an address in a cache expecting not to find it.
|
---|
1035 | *
|
---|
1036 | * @returns -1 if not found, the index of the cache entry if found.
|
---|
1037 | * @param pCache The cache.
|
---|
1038 | * @param pAddr The address.
|
---|
1039 | * @param cbAddr The address size (optimization).
|
---|
1040 | */
|
---|
1041 | DECLINLINE(int) intnetR0IfAddrCacheLookupUnlikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
|
---|
1042 | {
|
---|
1043 | Assert(pCache->cbAddress == cbAddr);
|
---|
1044 |
|
---|
1045 | /*
|
---|
1046 | * The optimized case is when there is one cache entry and
|
---|
1047 | * it doesn't match.
|
---|
1048 | */
|
---|
1049 | unsigned i = pCache->cEntries;
|
---|
1050 | if (RT_UNLIKELY( i > 0
|
---|
1051 | && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)))
|
---|
1052 | return 0;
|
---|
1053 | if (RT_LIKELY(i <= 1))
|
---|
1054 | return -1;
|
---|
1055 |
|
---|
1056 | /*
|
---|
1057 | * Then check the last entry and return if there are just two cache entries.
|
---|
1058 | */
|
---|
1059 | i--;
|
---|
1060 | if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr)))
|
---|
1061 | return i;
|
---|
1062 | if (i <= 1)
|
---|
1063 | return -1;
|
---|
1064 |
|
---|
1065 | return intnetR0IfAddrCacheInCacheUnlikelySlow(pCache, pAddr, cbAddr);
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | /**
|
---|
1070 | * Deletes a specific cache entry.
|
---|
1071 | *
|
---|
1072 | * Worker for intnetR0NetworkAddrCacheDelete and intnetR0NetworkAddrCacheDeleteMinusIf.
|
---|
1073 | *
|
---|
1074 | * @param pIf The interface (for logging).
|
---|
1075 | * @param pCache The cache.
|
---|
1076 | * @param iEntry The entry to delete.
|
---|
1077 | * @param pszMsg Log message.
|
---|
1078 | */
|
---|
1079 | static void intnetR0IfAddrCacheDeleteIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, int iEntry, const char *pszMsg)
|
---|
1080 | {
|
---|
1081 | AssertReturnVoid(iEntry < pCache->cEntries);
|
---|
1082 | AssertReturnVoid(iEntry >= 0);
|
---|
1083 | #ifdef LOG_ENABLED
|
---|
1084 | INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
|
---|
1085 | PCRTNETADDRU pAddr = (PCRTNETADDRU)(pCache->pbEntries + iEntry * pCache->cbEntry);
|
---|
1086 | switch (enmAddrType)
|
---|
1087 | {
|
---|
1088 | case kIntNetAddrType_IPv4:
|
---|
1089 | Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv4 deleted #%d %RTnaipv4 %s\n",
|
---|
1090 | pIf->hIf, &pIf->MacAddr, iEntry, pAddr->IPv4, pszMsg));
|
---|
1091 | break;
|
---|
1092 | case kIntNetAddrType_IPv6:
|
---|
1093 | Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv6 deleted #%d %RTnaipv6 %s\n",
|
---|
1094 | pIf->hIf, &pIf->MacAddr, iEntry, pAddr->IPv6, pszMsg));
|
---|
1095 | break;
|
---|
1096 | default:
|
---|
1097 | Log(("intnetR0IfAddrCacheDeleteIt: hIf=%RX32 MAC=%.6Rhxs type=%d #%d %.*Rhxs %s\n",
|
---|
1098 | pIf->hIf, &pIf->MacAddr, enmAddrType, iEntry, pCache->cbAddress, pAddr, pszMsg));
|
---|
1099 | break;
|
---|
1100 | }
|
---|
1101 | #endif
|
---|
1102 |
|
---|
1103 | pCache->cEntries--;
|
---|
1104 | if (iEntry < pCache->cEntries)
|
---|
1105 | memmove(pCache->pbEntries + iEntry * pCache->cbEntry,
|
---|
1106 | pCache->pbEntries + (iEntry + 1) * pCache->cbEntry,
|
---|
1107 | (pCache->cEntries - iEntry) * pCache->cbEntry);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | /**
|
---|
1112 | * Deletes an address from the cache, assuming it isn't actually in the cache.
|
---|
1113 | *
|
---|
1114 | * May or may not own the spinlock when calling this.
|
---|
1115 | *
|
---|
1116 | * @param pIf The interface (for logging).
|
---|
1117 | * @param pCache The cache.
|
---|
1118 | * @param pAddr The address.
|
---|
1119 | * @param cbAddr The address size (optimization).
|
---|
1120 | */
|
---|
1121 | DECLINLINE(void) intnetR0IfAddrCacheDelete(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
|
---|
1122 | {
|
---|
1123 | int i = intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
|
---|
1124 | if (RT_UNLIKELY(i >= 0))
|
---|
1125 | intnetR0IfAddrCacheDeleteIt(pIf, pCache, i, pszMsg);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 |
|
---|
1129 | /**
|
---|
1130 | * Deletes the address from all the interface caches.
|
---|
1131 | *
|
---|
1132 | * This is used to remove stale entries that has been reassigned to
|
---|
1133 | * other machines on the network.
|
---|
1134 | *
|
---|
1135 | * @param pNetwork The network.
|
---|
1136 | * @param pAddr The address.
|
---|
1137 | * @param enmType The address type.
|
---|
1138 | * @param cbAddr The address size (optimization).
|
---|
1139 | * @param pszMsg Log message.
|
---|
1140 | */
|
---|
1141 | DECLINLINE(void) intnetR0NetworkAddrCacheDelete(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType,
|
---|
1142 | uint8_t const cbAddr, const char *pszMsg)
|
---|
1143 | {
|
---|
1144 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1145 |
|
---|
1146 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
1147 | while (iIf--)
|
---|
1148 | {
|
---|
1149 | PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
1150 | int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
|
---|
1151 | if (RT_UNLIKELY(i >= 0))
|
---|
1152 | intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 |
|
---|
1159 | /**
|
---|
1160 | * Deletes the address from all the interface caches except the specified one.
|
---|
1161 | *
|
---|
1162 | * This is used to remove stale entries that has been reassigned to
|
---|
1163 | * other machines on the network.
|
---|
1164 | *
|
---|
1165 | * @param pNetwork The network.
|
---|
1166 | * @param pAddr The address.
|
---|
1167 | * @param enmType The address type.
|
---|
1168 | * @param cbAddr The address size (optimization).
|
---|
1169 | * @param pszMsg Log message.
|
---|
1170 | */
|
---|
1171 | DECLINLINE(void) intnetR0NetworkAddrCacheDeleteMinusIf(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCRTNETADDRU pAddr,
|
---|
1172 | INTNETADDRTYPE const enmType, uint8_t const cbAddr, const char *pszMsg)
|
---|
1173 | {
|
---|
1174 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1175 |
|
---|
1176 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
1177 | while (iIf--)
|
---|
1178 | {
|
---|
1179 | PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
1180 | if (pIf != pIfSender)
|
---|
1181 | {
|
---|
1182 | int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
|
---|
1183 | if (RT_UNLIKELY(i >= 0))
|
---|
1184 | intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | /**
|
---|
1193 | * Lookup an address on the network, returning the (first) interface having it
|
---|
1194 | * in its address cache.
|
---|
1195 | *
|
---|
1196 | * @returns Pointer to the interface on success, NULL if not found. The caller
|
---|
1197 | * must release the interface by calling intnetR0BusyDecIf.
|
---|
1198 | * @param pNetwork The network.
|
---|
1199 | * @param pAddr The address to lookup.
|
---|
1200 | * @param enmType The address type.
|
---|
1201 | * @param cbAddr The size of the address.
|
---|
1202 | */
|
---|
1203 | DECLINLINE(PINTNETIF) intnetR0NetworkAddrCacheLookupIf(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType, uint8_t const cbAddr)
|
---|
1204 | {
|
---|
1205 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1206 |
|
---|
1207 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
1208 | while (iIf--)
|
---|
1209 | {
|
---|
1210 | PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
1211 | int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
|
---|
1212 | if (i >= 0)
|
---|
1213 | {
|
---|
1214 | intnetR0BusyIncIf(pIf);
|
---|
1215 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1216 | return pIf;
|
---|
1217 | }
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1221 | return NULL;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 |
|
---|
1225 | /**
|
---|
1226 | * Adds an address to the cache, the caller is responsible for making sure it's
|
---|
1227 | * not already in the cache.
|
---|
1228 | *
|
---|
1229 | * The caller must not
|
---|
1230 | *
|
---|
1231 | * @param pIf The interface (for logging).
|
---|
1232 | * @param pCache The address cache.
|
---|
1233 | * @param pAddr The address.
|
---|
1234 | * @param pszMsg log message.
|
---|
1235 | */
|
---|
1236 | static void intnetR0IfAddrCacheAddIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, const char *pszMsg)
|
---|
1237 | {
|
---|
1238 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
1239 | AssertReturnVoid(pNetwork);
|
---|
1240 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1241 |
|
---|
1242 | if (RT_UNLIKELY(!pCache->cEntriesAlloc))
|
---|
1243 | {
|
---|
1244 | /* This shouldn't happen*/
|
---|
1245 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1246 | return;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | /* When the table is full, drop the older entry (FIFO). Do proper ageing? */
|
---|
1250 | if (pCache->cEntries >= pCache->cEntriesAlloc)
|
---|
1251 | {
|
---|
1252 | Log(("intnetR0IfAddrCacheAddIt: type=%d replacing %.*Rhxs\n",
|
---|
1253 | (int)(uintptr_t)(pCache - &pIf->aAddrCache[0]), pCache->cbAddress, pCache->pbEntries));
|
---|
1254 | memmove(pCache->pbEntries, pCache->pbEntries + pCache->cbEntry, pCache->cbEntry * (pCache->cEntries - 1));
|
---|
1255 | pCache->cEntries--;
|
---|
1256 | Assert(pCache->cEntries < pCache->cEntriesAlloc);
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | /*
|
---|
1260 | * Add the new entry to the end of the array.
|
---|
1261 | */
|
---|
1262 | uint8_t *pbEntry = pCache->pbEntries + pCache->cEntries * pCache->cbEntry;
|
---|
1263 | memcpy(pbEntry, pAddr, pCache->cbAddress);
|
---|
1264 | memset(pbEntry + pCache->cbAddress, '\0', pCache->cbEntry - pCache->cbAddress);
|
---|
1265 | #ifdef LOG_ENABLED
|
---|
1266 | INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
|
---|
1267 | switch (enmAddrType)
|
---|
1268 | {
|
---|
1269 | case kIntNetAddrType_IPv4:
|
---|
1270 | Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %RTnaipv4 %s\n",
|
---|
1271 | pIf->hIf, &pIf->MacAddr, pCache->cEntries, pAddr->IPv4, pszMsg));
|
---|
1272 | break;
|
---|
1273 | case kIntNetAddrType_IPv6:
|
---|
1274 | Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv6 added #%d %RTnaipv6 %s\n",
|
---|
1275 | pIf->hIf, &pIf->MacAddr, pCache->cEntries, pAddr->IPv6, pszMsg));
|
---|
1276 | break;
|
---|
1277 | default:
|
---|
1278 | Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs type=%d added #%d %.*Rhxs %s\n",
|
---|
1279 | pIf->hIf, &pIf->MacAddr, enmAddrType, pCache->cEntries, pCache->cbAddress, pAddr, pszMsg));
|
---|
1280 | break;
|
---|
1281 | }
|
---|
1282 | #endif
|
---|
1283 | pCache->cEntries++;
|
---|
1284 | Assert(pCache->cEntries <= pCache->cEntriesAlloc);
|
---|
1285 |
|
---|
1286 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 |
|
---|
1290 | /**
|
---|
1291 | * A intnetR0IfAddrCacheAdd worker that performs the rest of the lookup.
|
---|
1292 | *
|
---|
1293 | * @param pIf The interface (for logging).
|
---|
1294 | * @param pCache The address cache.
|
---|
1295 | * @param pAddr The address.
|
---|
1296 | * @param cbAddr The size of the address (optimization).
|
---|
1297 | * @param pszMsg Log message.
|
---|
1298 | */
|
---|
1299 | static void intnetR0IfAddrCacheAddSlow(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
|
---|
1300 | {
|
---|
1301 | /*
|
---|
1302 | * Check all but the first and last entries, the caller
|
---|
1303 | * has already checked those.
|
---|
1304 | */
|
---|
1305 | int i = pCache->cEntries - 2;
|
---|
1306 | uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry;
|
---|
1307 | while (i >= 1)
|
---|
1308 | {
|
---|
1309 | if (RT_LIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
|
---|
1310 | return;
|
---|
1311 | pbEntry += pCache->cbEntry;
|
---|
1312 | i--;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /*
|
---|
1316 | * Not found, add it.
|
---|
1317 | */
|
---|
1318 | intnetR0IfAddrCacheAddIt(pIf, pCache, pAddr, pszMsg);
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | /**
|
---|
1323 | * Adds an address to the cache if it's not already there.
|
---|
1324 | *
|
---|
1325 | * Must not own any spinlocks when calling this function.
|
---|
1326 | *
|
---|
1327 | * @param pIf The interface (for logging).
|
---|
1328 | * @param pCache The address cache.
|
---|
1329 | * @param pAddr The address.
|
---|
1330 | * @param cbAddr The size of the address (optimization).
|
---|
1331 | * @param pszMsg Log message.
|
---|
1332 | */
|
---|
1333 | DECLINLINE(void) intnetR0IfAddrCacheAdd(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr,
|
---|
1334 | uint8_t const cbAddr, const char *pszMsg)
|
---|
1335 | {
|
---|
1336 | Assert(pCache->cbAddress == cbAddr);
|
---|
1337 |
|
---|
1338 | /*
|
---|
1339 | * The optimized case is when the address the first or last cache entry.
|
---|
1340 | */
|
---|
1341 | unsigned i = pCache->cEntries;
|
---|
1342 | if (RT_LIKELY( i > 0
|
---|
1343 | && ( intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)
|
---|
1344 | || (i > 1
|
---|
1345 | && intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))) ))
|
---|
1346 | return;
|
---|
1347 | intnetR0IfAddrCacheAddSlow(pIf, pCache, pAddr, cbAddr, pszMsg);
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Destroys the specified address cache.
|
---|
1353 | * @param pCache The address cache.
|
---|
1354 | */
|
---|
1355 | static void intnetR0IfAddrCacheDestroy(PINTNETADDRCACHE pCache)
|
---|
1356 | {
|
---|
1357 | void *pvFree = pCache->pbEntries;
|
---|
1358 | pCache->pbEntries = NULL;
|
---|
1359 | pCache->cEntries = 0;
|
---|
1360 | pCache->cEntriesAlloc = 0;
|
---|
1361 | RTMemFree(pvFree);
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 |
|
---|
1365 | /**
|
---|
1366 | * Initialize the address cache for the specified address type.
|
---|
1367 | *
|
---|
1368 | * The cache storage is preallocated and fixed size so that we can handle
|
---|
1369 | * inserts from problematic contexts.
|
---|
1370 | *
|
---|
1371 | * @returns VINF_SUCCESS or VERR_NO_MEMORY.
|
---|
1372 | * @param pCache The cache to initialize.
|
---|
1373 | * @param enmAddrType The address type.
|
---|
1374 | * @param fEnabled Whether the address cache is enabled or not.
|
---|
1375 | */
|
---|
1376 | static int intnetR0IfAddrCacheInit(PINTNETADDRCACHE pCache, INTNETADDRTYPE enmAddrType, bool fEnabled)
|
---|
1377 | {
|
---|
1378 | pCache->cEntries = 0;
|
---|
1379 | pCache->cbAddress = intnetR0AddrSize(enmAddrType);
|
---|
1380 | pCache->cbEntry = RT_ALIGN(pCache->cbAddress, 4);
|
---|
1381 | if (fEnabled)
|
---|
1382 | {
|
---|
1383 | pCache->cEntriesAlloc = 32;
|
---|
1384 | pCache->pbEntries = (uint8_t *)RTMemAllocZ(pCache->cEntriesAlloc * pCache->cbEntry);
|
---|
1385 | if (!pCache->pbEntries)
|
---|
1386 | return VERR_NO_MEMORY;
|
---|
1387 | }
|
---|
1388 | else
|
---|
1389 | {
|
---|
1390 | pCache->cEntriesAlloc = 0;
|
---|
1391 | pCache->pbEntries = NULL;
|
---|
1392 | }
|
---|
1393 | return VINF_SUCCESS;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 |
|
---|
1397 | /**
|
---|
1398 | * Is it a multicast or broadcast MAC address?
|
---|
1399 | *
|
---|
1400 | * @returns true if multicast, false if not.
|
---|
1401 | * @param pMacAddr The address to inspect.
|
---|
1402 | */
|
---|
1403 | DECL_FORCE_INLINE(bool) intnetR0IsMacAddrMulticast(PCRTMAC pMacAddr)
|
---|
1404 | {
|
---|
1405 | return !!(pMacAddr->au8[0] & 0x01);
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Is it a dummy MAC address?
|
---|
1411 | *
|
---|
1412 | * We use dummy MAC addresses for interfaces which we don't know the MAC
|
---|
1413 | * address of because they haven't sent anything (learning) or explicitly set
|
---|
1414 | * it.
|
---|
1415 | *
|
---|
1416 | * @returns true if dummy, false if not.
|
---|
1417 | * @param pMacAddr The address to inspect.
|
---|
1418 | */
|
---|
1419 | DECL_FORCE_INLINE(bool) intnetR0IsMacAddrDummy(PCRTMAC pMacAddr)
|
---|
1420 | {
|
---|
1421 | /* The dummy address are broadcast addresses, don't bother check it all. */
|
---|
1422 | return pMacAddr->au16[0] == 0xffff;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 |
|
---|
1426 | /**
|
---|
1427 | * Compares two MAC addresses.
|
---|
1428 | *
|
---|
1429 | * @returns true if equal, false if not.
|
---|
1430 | * @param pDstAddr1 Address 1.
|
---|
1431 | * @param pDstAddr2 Address 2.
|
---|
1432 | */
|
---|
1433 | DECL_FORCE_INLINE(bool) intnetR0AreMacAddrsEqual(PCRTMAC pDstAddr1, PCRTMAC pDstAddr2)
|
---|
1434 | {
|
---|
1435 | return pDstAddr1->au16[2] == pDstAddr2->au16[2]
|
---|
1436 | && pDstAddr1->au16[1] == pDstAddr2->au16[1]
|
---|
1437 | && pDstAddr1->au16[0] == pDstAddr2->au16[0];
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 |
|
---|
1441 | /**
|
---|
1442 | * Switch a unicast frame based on the network layer address (OSI level 3) and
|
---|
1443 | * return a destination table.
|
---|
1444 | *
|
---|
1445 | * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
|
---|
1446 | * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
|
---|
1447 | * @param pNetwork The network to switch on.
|
---|
1448 | * @param pDstMacAddr The destination MAC address.
|
---|
1449 | * @param enmL3AddrType The level-3 destination address type.
|
---|
1450 | * @param pL3Addr The level-3 destination address.
|
---|
1451 | * @param cbL3Addr The size of the level-3 destination address.
|
---|
1452 | * @param fSrc The frame source (INTNETTRUNKDIR_WIRE).
|
---|
1453 | * @param pDstTab The destination output table.
|
---|
1454 | */
|
---|
1455 | static INTNETSWDECISION intnetR0NetworkSwitchLevel3(PINTNETNETWORK pNetwork, PCRTMAC pDstMacAddr,
|
---|
1456 | INTNETADDRTYPE enmL3AddrType, PCRTNETADDRU pL3Addr, uint8_t cbL3Addr,
|
---|
1457 | uint32_t fSrc, PINTNETDSTTAB pDstTab)
|
---|
1458 | {
|
---|
1459 | Assert(fSrc == INTNETTRUNKDIR_WIRE);
|
---|
1460 |
|
---|
1461 | /*
|
---|
1462 | * Grab the spinlock first and do the switching.
|
---|
1463 | */
|
---|
1464 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1465 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1466 |
|
---|
1467 | pDstTab->fTrunkDst = 0;
|
---|
1468 | pDstTab->pTrunk = 0;
|
---|
1469 | pDstTab->cIfs = 0;
|
---|
1470 |
|
---|
1471 | /* Find exactly matching or promiscuous interfaces. */
|
---|
1472 | uint32_t cExactHits = 0;
|
---|
1473 | uint32_t iIfMac = pTab->cEntries;
|
---|
1474 | while (iIfMac-- > 0)
|
---|
1475 | {
|
---|
1476 | if (pTab->paEntries[iIfMac].fActive)
|
---|
1477 | {
|
---|
1478 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1479 | bool fExact = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmL3AddrType], pL3Addr, cbL3Addr) >= 0;
|
---|
1480 | if (fExact || pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
|
---|
1481 | {
|
---|
1482 | cExactHits += fExact;
|
---|
1483 |
|
---|
1484 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1485 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1486 | pDstTab->aIfs[iIfDst].fReplaceDstMac = fExact;
|
---|
1487 | intnetR0BusyIncIf(pIf);
|
---|
1488 |
|
---|
1489 | if (fExact)
|
---|
1490 | pDstMacAddr = &pIf->MacAddr; /* Avoids duplicates being sent to the host. */
|
---|
1491 | }
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | /* Network only promicuous mode ifs should see related trunk traffic. */
|
---|
1496 | if ( cExactHits
|
---|
1497 | && fSrc
|
---|
1498 | && pNetwork->MacTab.cPromiscuousNoTrunkEntries)
|
---|
1499 | {
|
---|
1500 | iIfMac = pTab->cEntries;
|
---|
1501 | while (iIfMac-- > 0)
|
---|
1502 | {
|
---|
1503 | if ( pTab->paEntries[iIfMac].fActive
|
---|
1504 | && pTab->paEntries[iIfMac].fPromiscuousEff
|
---|
1505 | && !pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
|
---|
1506 | {
|
---|
1507 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1508 | if (intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmL3AddrType], pL3Addr, cbL3Addr) < 0)
|
---|
1509 | {
|
---|
1510 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1511 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1512 | pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
|
---|
1513 | intnetR0BusyIncIf(pIf);
|
---|
1514 | }
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | /* Does it match the host, or is the host promiscuous? */
|
---|
1520 | if (pTab->fHostActive)
|
---|
1521 | {
|
---|
1522 | bool fExact = intnetR0AreMacAddrsEqual(&pTab->HostMac, pDstMacAddr);
|
---|
1523 | if ( fExact
|
---|
1524 | || intnetR0IsMacAddrDummy(&pTab->HostMac)
|
---|
1525 | || pTab->fHostPromiscuousEff)
|
---|
1526 | {
|
---|
1527 | cExactHits += fExact;
|
---|
1528 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
|
---|
1529 | }
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | /* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
|
---|
1533 | if (pTab->fWireActive && (!cExactHits || pTab->fWirePromiscuousEff))
|
---|
1534 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
|
---|
1535 | pDstTab->fTrunkDst &= ~fSrc;
|
---|
1536 | if (pDstTab->fTrunkDst)
|
---|
1537 | {
|
---|
1538 | PINTNETTRUNKIF pTrunk = pTab->pTrunk;
|
---|
1539 | pDstTab->pTrunk = pTrunk;
|
---|
1540 | intnetR0BusyIncTrunk(pTrunk);
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1544 | return pDstTab->cIfs
|
---|
1545 | ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST)
|
---|
1546 | : (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK);
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 |
|
---|
1550 | /**
|
---|
1551 | * Pre-switch a unicast MAC address.
|
---|
1552 | *
|
---|
1553 | * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
|
---|
1554 | * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
|
---|
1555 | * @param pNetwork The network to switch on.
|
---|
1556 | * @param fSrc The frame source.
|
---|
1557 | * @param pSrcAddr The source address of the frame.
|
---|
1558 | * @param pDstAddr The destination address of the frame.
|
---|
1559 | */
|
---|
1560 | static INTNETSWDECISION intnetR0NetworkPreSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PCRTMAC pSrcAddr,
|
---|
1561 | PCRTMAC pDstAddr)
|
---|
1562 | {
|
---|
1563 | Assert(!intnetR0IsMacAddrMulticast(pDstAddr));
|
---|
1564 | Assert(fSrc);
|
---|
1565 |
|
---|
1566 | /*
|
---|
1567 | * Grab the spinlock first and do the switching.
|
---|
1568 | */
|
---|
1569 | INTNETSWDECISION enmSwDecision = INTNETSWDECISION_BROADCAST;
|
---|
1570 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1571 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1572 |
|
---|
1573 | /* Iterate the internal network interfaces and look for matching source and
|
---|
1574 | destination addresses. */
|
---|
1575 | uint32_t iIfMac = pTab->cEntries;
|
---|
1576 | while (iIfMac-- > 0)
|
---|
1577 | {
|
---|
1578 | if (pTab->paEntries[iIfMac].fActive)
|
---|
1579 | {
|
---|
1580 | /* Unknown interface address? */
|
---|
1581 | if (intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr))
|
---|
1582 | break;
|
---|
1583 |
|
---|
1584 | /* Promiscuous mode? */
|
---|
1585 | if (pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
|
---|
1586 | break;
|
---|
1587 |
|
---|
1588 | /* Paranoia - this shouldn't happen, right? */
|
---|
1589 | if ( pSrcAddr
|
---|
1590 | && intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pSrcAddr))
|
---|
1591 | break;
|
---|
1592 |
|
---|
1593 | /* Exact match? */
|
---|
1594 | if (intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr))
|
---|
1595 | {
|
---|
1596 | enmSwDecision = pTab->fHostPromiscuousEff && fSrc == INTNETTRUNKDIR_WIRE
|
---|
1597 | ? INTNETSWDECISION_BROADCAST
|
---|
1598 | : INTNETSWDECISION_INTNET;
|
---|
1599 | break;
|
---|
1600 | }
|
---|
1601 | }
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1605 | return enmSwDecision;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 |
|
---|
1609 | /**
|
---|
1610 | * Switch a unicast MAC address and return a destination table.
|
---|
1611 | *
|
---|
1612 | * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
|
---|
1613 | * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
|
---|
1614 | * @param pNetwork The network to switch on.
|
---|
1615 | * @param fSrc The frame source.
|
---|
1616 | * @param pIfSender The sender interface, NULL if trunk. Used to
|
---|
1617 | * prevent sending an echo to the sender.
|
---|
1618 | * @param pDstAddr The destination address of the frame.
|
---|
1619 | * @param pDstTab The destination output table.
|
---|
1620 | */
|
---|
1621 | static INTNETSWDECISION intnetR0NetworkSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
|
---|
1622 | PCRTMAC pDstAddr, PINTNETDSTTAB pDstTab)
|
---|
1623 | {
|
---|
1624 | AssertPtr(pDstTab);
|
---|
1625 | Assert(!intnetR0IsMacAddrMulticast(pDstAddr));
|
---|
1626 |
|
---|
1627 | /*
|
---|
1628 | * Grab the spinlock first and do the switching.
|
---|
1629 | */
|
---|
1630 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1631 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1632 |
|
---|
1633 | pDstTab->fTrunkDst = 0;
|
---|
1634 | pDstTab->pTrunk = 0;
|
---|
1635 | pDstTab->cIfs = 0;
|
---|
1636 |
|
---|
1637 | /* Find exactly matching or promiscuous interfaces. */
|
---|
1638 | uint32_t cExactHits = 0;
|
---|
1639 | uint32_t iIfMac = pTab->cEntries;
|
---|
1640 | while (iIfMac-- > 0)
|
---|
1641 | {
|
---|
1642 | if (pTab->paEntries[iIfMac].fActive)
|
---|
1643 | {
|
---|
1644 | bool fExact = intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr);
|
---|
1645 | if ( fExact
|
---|
1646 | || intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr)
|
---|
1647 | || ( pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
|
---|
1648 | || (!fSrc && pTab->paEntries[iIfMac].fPromiscuousEff) )
|
---|
1649 | )
|
---|
1650 | {
|
---|
1651 | cExactHits += fExact;
|
---|
1652 |
|
---|
1653 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1654 | if (RT_LIKELY(pIf != pIfSender)) /* paranoia */
|
---|
1655 | {
|
---|
1656 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1657 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1658 | pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
|
---|
1659 | intnetR0BusyIncIf(pIf);
|
---|
1660 | }
|
---|
1661 | }
|
---|
1662 | }
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | /* Network only promicuous mode ifs should see related trunk traffic. */
|
---|
1666 | if ( cExactHits
|
---|
1667 | && fSrc
|
---|
1668 | && pNetwork->MacTab.cPromiscuousNoTrunkEntries)
|
---|
1669 | {
|
---|
1670 | iIfMac = pTab->cEntries;
|
---|
1671 | while (iIfMac-- > 0)
|
---|
1672 | {
|
---|
1673 | if ( pTab->paEntries[iIfMac].fPromiscuousEff
|
---|
1674 | && !pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
|
---|
1675 | && pTab->paEntries[iIfMac].fActive
|
---|
1676 | && !intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr)
|
---|
1677 | && !intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr) )
|
---|
1678 | {
|
---|
1679 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1680 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1681 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1682 | pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
|
---|
1683 | intnetR0BusyIncIf(pIf);
|
---|
1684 | }
|
---|
1685 | }
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | /* Does it match the host, or is the host promiscuous? */
|
---|
1689 | if ( fSrc != INTNETTRUNKDIR_HOST
|
---|
1690 | && pTab->fHostActive)
|
---|
1691 | {
|
---|
1692 | bool fExact = intnetR0AreMacAddrsEqual(&pTab->HostMac, pDstAddr);
|
---|
1693 | if ( fExact
|
---|
1694 | || intnetR0IsMacAddrDummy(&pTab->HostMac)
|
---|
1695 | || pTab->fHostPromiscuousEff)
|
---|
1696 | {
|
---|
1697 | cExactHits += fExact;
|
---|
1698 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
|
---|
1699 | }
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | /* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
|
---|
1703 | if ( fSrc != INTNETTRUNKDIR_WIRE
|
---|
1704 | && pTab->fWireActive
|
---|
1705 | && (!cExactHits || pTab->fWirePromiscuousEff)
|
---|
1706 | )
|
---|
1707 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
|
---|
1708 |
|
---|
1709 | /* Grab the trunk if we're sending to it. */
|
---|
1710 | if (pDstTab->fTrunkDst)
|
---|
1711 | {
|
---|
1712 | PINTNETTRUNKIF pTrunk = pTab->pTrunk;
|
---|
1713 | pDstTab->pTrunk = pTrunk;
|
---|
1714 | intnetR0BusyIncTrunk(pTrunk);
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1718 | return pDstTab->cIfs
|
---|
1719 | ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST)
|
---|
1720 | : (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK);
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Create a destination table for a broadcast frame.
|
---|
1726 | *
|
---|
1727 | * @returns INTNETSWDECISION_BROADCAST.
|
---|
1728 | * @param pNetwork The network to switch on.
|
---|
1729 | * @param fSrc The frame source.
|
---|
1730 | * @param pIfSender The sender interface, NULL if trunk. Used to
|
---|
1731 | * prevent sending an echo to the sender.
|
---|
1732 | * @param pDstTab The destination output table.
|
---|
1733 | */
|
---|
1734 | static INTNETSWDECISION intnetR0NetworkSwitchBroadcast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
|
---|
1735 | PINTNETDSTTAB pDstTab)
|
---|
1736 | {
|
---|
1737 | AssertPtr(pDstTab);
|
---|
1738 |
|
---|
1739 | /*
|
---|
1740 | * Grab the spinlock first and record all active interfaces.
|
---|
1741 | */
|
---|
1742 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1743 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1744 |
|
---|
1745 | pDstTab->fTrunkDst = 0;
|
---|
1746 | pDstTab->pTrunk = 0;
|
---|
1747 | pDstTab->cIfs = 0;
|
---|
1748 |
|
---|
1749 | /* Regular interfaces. */
|
---|
1750 | uint32_t iIfMac = pTab->cEntries;
|
---|
1751 | while (iIfMac-- > 0)
|
---|
1752 | {
|
---|
1753 | if (pTab->paEntries[iIfMac].fActive)
|
---|
1754 | {
|
---|
1755 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1756 | if (pIf != pIfSender)
|
---|
1757 | {
|
---|
1758 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1759 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1760 | pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
|
---|
1761 | intnetR0BusyIncIf(pIf);
|
---|
1762 | }
|
---|
1763 | }
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | /* The trunk interface. */
|
---|
1767 | if (pTab->fHostActive)
|
---|
1768 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
|
---|
1769 | if (pTab->fWireActive)
|
---|
1770 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
|
---|
1771 | pDstTab->fTrunkDst &= ~fSrc;
|
---|
1772 | if (pDstTab->fTrunkDst)
|
---|
1773 | {
|
---|
1774 | PINTNETTRUNKIF pTrunk = pTab->pTrunk;
|
---|
1775 | pDstTab->pTrunk = pTrunk;
|
---|
1776 | intnetR0BusyIncTrunk(pTrunk);
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1780 | return INTNETSWDECISION_BROADCAST;
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 |
|
---|
1784 | /**
|
---|
1785 | * Create a destination table with the trunk and any promiscuous interfaces.
|
---|
1786 | *
|
---|
1787 | * This is only used in a fallback case of the level-3 switching, so we can
|
---|
1788 | * assume the wire as source and skip the sender interface filtering.
|
---|
1789 | *
|
---|
1790 | * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
|
---|
1791 | * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
|
---|
1792 | * @param pNetwork The network to switch on.
|
---|
1793 | * @param fSrc The frame source.
|
---|
1794 | * @param pDstTab The destination output table.
|
---|
1795 | */
|
---|
1796 | static INTNETSWDECISION intnetR0NetworkSwitchTrunkAndPromisc(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
|
---|
1797 | {
|
---|
1798 | Assert(fSrc == INTNETTRUNKDIR_WIRE);
|
---|
1799 |
|
---|
1800 | /*
|
---|
1801 | * Grab the spinlock first and do the switching.
|
---|
1802 | */
|
---|
1803 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1804 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1805 |
|
---|
1806 | pDstTab->fTrunkDst = 0;
|
---|
1807 | pDstTab->pTrunk = 0;
|
---|
1808 | pDstTab->cIfs = 0;
|
---|
1809 |
|
---|
1810 | /* Find promiscuous interfaces. */
|
---|
1811 | uint32_t iIfMac = pTab->cEntries;
|
---|
1812 | while (iIfMac-- > 0)
|
---|
1813 | {
|
---|
1814 | if ( pTab->paEntries[iIfMac].fActive
|
---|
1815 | && ( pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
|
---|
1816 | || (!fSrc && pTab->paEntries[iIfMac].fPromiscuousEff) )
|
---|
1817 | )
|
---|
1818 | {
|
---|
1819 | PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
|
---|
1820 | uint32_t iIfDst = pDstTab->cIfs++;
|
---|
1821 | pDstTab->aIfs[iIfDst].pIf = pIf;
|
---|
1822 | pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
|
---|
1823 | intnetR0BusyIncIf(pIf);
|
---|
1824 | }
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | /* The trunk interface. */
|
---|
1828 | if (pTab->fHostActive)
|
---|
1829 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
|
---|
1830 | if (pTab->fWireActive)
|
---|
1831 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
|
---|
1832 | pDstTab->fTrunkDst &= ~fSrc;
|
---|
1833 | if (pDstTab->fTrunkDst)
|
---|
1834 | {
|
---|
1835 | PINTNETTRUNKIF pTrunk = pTab->pTrunk;
|
---|
1836 | pDstTab->pTrunk = pTrunk;
|
---|
1837 | intnetR0BusyIncTrunk(pTrunk);
|
---|
1838 | }
|
---|
1839 |
|
---|
1840 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1841 | return !pDstTab->cIfs
|
---|
1842 | ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK)
|
---|
1843 | : (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST);
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 |
|
---|
1847 | /**
|
---|
1848 | * Create a destination table for a trunk frame.
|
---|
1849 | *
|
---|
1850 | * @returns INTNETSWDECISION_BROADCAST.
|
---|
1851 | * @param pNetwork The network to switch on.
|
---|
1852 | * @param fSrc The frame source.
|
---|
1853 | * @param pDstTab The destination output table.
|
---|
1854 | */
|
---|
1855 | static INTNETSWDECISION intnetR0NetworkSwitchTrunk(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
|
---|
1856 | {
|
---|
1857 | AssertPtr(pDstTab);
|
---|
1858 |
|
---|
1859 | /*
|
---|
1860 | * Grab the spinlock first and record all active interfaces.
|
---|
1861 | */
|
---|
1862 | PINTNETMACTAB pTab= &pNetwork->MacTab;
|
---|
1863 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1864 |
|
---|
1865 | pDstTab->fTrunkDst = 0;
|
---|
1866 | pDstTab->pTrunk = 0;
|
---|
1867 | pDstTab->cIfs = 0;
|
---|
1868 |
|
---|
1869 | /* The trunk interface. */
|
---|
1870 | if (pTab->fHostActive)
|
---|
1871 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
|
---|
1872 | if (pTab->fWireActive)
|
---|
1873 | pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
|
---|
1874 | pDstTab->fTrunkDst &= ~fSrc;
|
---|
1875 | if (pDstTab->fTrunkDst)
|
---|
1876 | {
|
---|
1877 | PINTNETTRUNKIF pTrunk = pTab->pTrunk;
|
---|
1878 | pDstTab->pTrunk = pTrunk;
|
---|
1879 | intnetR0BusyIncTrunk(pTrunk);
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
1883 | return pDstTab->fTrunkDst ? INTNETSWDECISION_TRUNK : INTNETSWDECISION_DROP;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 |
|
---|
1887 | /**
|
---|
1888 | * Wrapper around RTMemAlloc for allocating a destination table.
|
---|
1889 | *
|
---|
1890 | * @returns VINF_SUCCESS or VERR_NO_MEMORY.
|
---|
1891 | * @param cEntries The size given as an entry count.
|
---|
1892 | * @param ppDstTab Where to store the pointer (always).
|
---|
1893 | */
|
---|
1894 | DECLINLINE(int) intnetR0AllocDstTab(uint32_t cEntries, PINTNETDSTTAB *ppDstTab)
|
---|
1895 | {
|
---|
1896 | PINTNETDSTTAB pDstTab;
|
---|
1897 | *ppDstTab = pDstTab = (PINTNETDSTTAB)RTMemAlloc(RT_OFFSETOF(INTNETDSTTAB, aIfs[cEntries]));
|
---|
1898 | if (RT_UNLIKELY(!pDstTab))
|
---|
1899 | return VERR_NO_MEMORY;
|
---|
1900 | return VINF_SUCCESS;
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 |
|
---|
1904 | /**
|
---|
1905 | * Ensures that there is space for another interface in the MAC address lookup
|
---|
1906 | * table as well as all the destination tables.
|
---|
1907 | *
|
---|
1908 | * The caller must own the create/open/destroy mutex.
|
---|
1909 | *
|
---|
1910 | * @returns VINF_SUCCESS, VERR_NO_MEMORY or VERR_OUT_OF_RANGE.
|
---|
1911 | * @param pNetwork The network to operate on.
|
---|
1912 | */
|
---|
1913 | static int intnetR0NetworkEnsureTabSpace(PINTNETNETWORK pNetwork)
|
---|
1914 | {
|
---|
1915 | /*
|
---|
1916 | * The cEntries and cEntriesAllocated members are only updated while
|
---|
1917 | * owning the big mutex, so we only need the spinlock when doing the
|
---|
1918 | * actual table replacing.
|
---|
1919 | */
|
---|
1920 | PINTNETMACTAB pTab = &pNetwork->MacTab;
|
---|
1921 | int rc = VINF_SUCCESS;
|
---|
1922 | AssertReturn(pTab->cEntries <= pTab->cEntriesAllocated, VERR_INTERNAL_ERROR_2);
|
---|
1923 | if (pTab->cEntries + 1 > pTab->cEntriesAllocated)
|
---|
1924 | {
|
---|
1925 | uint32_t const cAllocated = pTab->cEntriesAllocated + INTNET_GROW_DSTTAB_SIZE;
|
---|
1926 | if (cAllocated <= INTNET_MAX_IFS)
|
---|
1927 | {
|
---|
1928 | /*
|
---|
1929 | * Resize the destination tables first, this can be kind of tedious.
|
---|
1930 | */
|
---|
1931 | for (uint32_t i = 0; i < pTab->cEntries; i++)
|
---|
1932 | {
|
---|
1933 | PINTNETIF pIf = pTab->paEntries[i].pIf; AssertPtr(pIf);
|
---|
1934 | PINTNETDSTTAB pNew;
|
---|
1935 | rc = intnetR0AllocDstTab(cAllocated, &pNew);
|
---|
1936 | if (RT_FAILURE(rc))
|
---|
1937 | break;
|
---|
1938 |
|
---|
1939 | for (;;)
|
---|
1940 | {
|
---|
1941 | PINTNETDSTTAB pOld = pIf->pDstTab;
|
---|
1942 | if ( pOld
|
---|
1943 | && ASMAtomicCmpXchgPtr(&pIf->pDstTab, pNew, pOld))
|
---|
1944 | {
|
---|
1945 | RTMemFree(pOld);
|
---|
1946 | break;
|
---|
1947 | }
|
---|
1948 | intnetR0BusyWait(pNetwork, &pIf->cBusy);
|
---|
1949 | }
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | /*
|
---|
1953 | * The trunk.
|
---|
1954 | */
|
---|
1955 | if ( RT_SUCCESS(rc)
|
---|
1956 | && pNetwork->MacTab.pTrunk)
|
---|
1957 | {
|
---|
1958 | AssertCompileAdjacentMembers(INTNETTRUNKIF, apTaskDstTabs, apIntDstTabs);
|
---|
1959 | PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
|
---|
1960 | PINTNETDSTTAB * const ppEndDstTab = &pTrunk->apIntDstTabs[pTrunk->cIntDstTabs];
|
---|
1961 | for (PINTNETDSTTAB *ppDstTab = &pTrunk->apTaskDstTabs[0];
|
---|
1962 | ppDstTab != ppEndDstTab && RT_SUCCESS(rc);
|
---|
1963 | ppDstTab++)
|
---|
1964 | {
|
---|
1965 | PINTNETDSTTAB pNew;
|
---|
1966 | rc = intnetR0AllocDstTab(cAllocated, &pNew);
|
---|
1967 | if (RT_FAILURE(rc))
|
---|
1968 | break;
|
---|
1969 |
|
---|
1970 | for (;;)
|
---|
1971 | {
|
---|
1972 | RTSpinlockAcquire(pTrunk->hDstTabSpinlock);
|
---|
1973 | void *pvOld = *ppDstTab;
|
---|
1974 | if (pvOld)
|
---|
1975 | *ppDstTab = pNew;
|
---|
1976 | RTSpinlockReleaseNoInts(pTrunk->hDstTabSpinlock);
|
---|
1977 | if (pvOld)
|
---|
1978 | {
|
---|
1979 | RTMemFree(pvOld);
|
---|
1980 | break;
|
---|
1981 | }
|
---|
1982 | intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
|
---|
1983 | }
|
---|
1984 | }
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 | /*
|
---|
1988 | * The MAC Address table itself.
|
---|
1989 | */
|
---|
1990 | if (RT_SUCCESS(rc))
|
---|
1991 | {
|
---|
1992 | PINTNETMACTABENTRY paNew = (PINTNETMACTABENTRY)RTMemAlloc(sizeof(INTNETMACTABENTRY) * cAllocated);
|
---|
1993 | if (paNew)
|
---|
1994 | {
|
---|
1995 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
1996 |
|
---|
1997 | PINTNETMACTABENTRY paOld = pTab->paEntries;
|
---|
1998 | uint32_t i = pTab->cEntries;
|
---|
1999 | while (i-- > 0)
|
---|
2000 | {
|
---|
2001 | paNew[i] = paOld[i];
|
---|
2002 |
|
---|
2003 | paOld[i].fActive = false;
|
---|
2004 | paOld[i].pIf = NULL;
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | pTab->paEntries = paNew;
|
---|
2008 | pTab->cEntriesAllocated = cAllocated;
|
---|
2009 |
|
---|
2010 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
2011 |
|
---|
2012 | RTMemFree(paOld);
|
---|
2013 | }
|
---|
2014 | else
|
---|
2015 | rc = VERR_NO_MEMORY;
|
---|
2016 | }
|
---|
2017 | }
|
---|
2018 | else
|
---|
2019 | rc = VERR_OUT_OF_RANGE;
|
---|
2020 | }
|
---|
2021 | return rc;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 |
|
---|
2025 |
|
---|
2026 |
|
---|
2027 | #ifdef INTNET_WITH_DHCP_SNOOPING
|
---|
2028 |
|
---|
2029 | /**
|
---|
2030 | * Snoops IP assignments and releases from the DHCPv4 traffic.
|
---|
2031 | *
|
---|
2032 | * The caller is responsible for making sure this traffic between the
|
---|
2033 | * BOOTPS and BOOTPC ports and validate the IP header. The UDP packet
|
---|
2034 | * need not be validated beyond the ports.
|
---|
2035 | *
|
---|
2036 | * @param pNetwork The network this frame was seen on.
|
---|
2037 | * @param pIpHdr Pointer to a valid IP header. This is for pseudo
|
---|
2038 | * header validation, so only the minimum header size
|
---|
2039 | * needs to be available and valid here.
|
---|
2040 | * @param pUdpHdr Pointer to the UDP header in the frame.
|
---|
2041 | * @param cbUdpPkt What's left of the frame when starting at the UDP header.
|
---|
2042 | * @param fGso Set if this is a GSO frame, clear if regular.
|
---|
2043 | */
|
---|
2044 | static void intnetR0NetworkSnoopDhcp(PINTNETNETWORK pNetwork, PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, uint32_t cbUdpPkt)
|
---|
2045 | {
|
---|
2046 | /*
|
---|
2047 | * Check if the DHCP message is valid and get the type.
|
---|
2048 | */
|
---|
2049 | if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbUdpPkt, true /*fCheckSum*/))
|
---|
2050 | {
|
---|
2051 | Log6(("Bad UDP packet\n"));
|
---|
2052 | return;
|
---|
2053 | }
|
---|
2054 | PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)(pUdpHdr + 1);
|
---|
2055 | uint8_t MsgType;
|
---|
2056 | if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbUdpPkt - sizeof(*pUdpHdr), &MsgType))
|
---|
2057 | {
|
---|
2058 | Log6(("Bad DHCP packet\n"));
|
---|
2059 | return;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | #ifdef LOG_ENABLED
|
---|
2063 | /*
|
---|
2064 | * Log it.
|
---|
2065 | */
|
---|
2066 | const char *pszType = "unknown";
|
---|
2067 | switch (MsgType)
|
---|
2068 | {
|
---|
2069 | case RTNET_DHCP_MT_DISCOVER: pszType = "discover"; break;
|
---|
2070 | case RTNET_DHCP_MT_OFFER: pszType = "offer"; break;
|
---|
2071 | case RTNET_DHCP_MT_REQUEST: pszType = "request"; break;
|
---|
2072 | case RTNET_DHCP_MT_DECLINE: pszType = "decline"; break;
|
---|
2073 | case RTNET_DHCP_MT_ACK: pszType = "ack"; break;
|
---|
2074 | case RTNET_DHCP_MT_NAC: pszType = "nac"; break;
|
---|
2075 | case RTNET_DHCP_MT_RELEASE: pszType = "release"; break;
|
---|
2076 | case RTNET_DHCP_MT_INFORM: pszType = "inform"; break;
|
---|
2077 | }
|
---|
2078 | Log6(("DHCP msg: %d (%s) client %.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d\n", MsgType, pszType, &pDhcp->bp_chaddr,
|
---|
2079 | pDhcp->bp_ciaddr.au8[0], pDhcp->bp_ciaddr.au8[1], pDhcp->bp_ciaddr.au8[2], pDhcp->bp_ciaddr.au8[3],
|
---|
2080 | pDhcp->bp_yiaddr.au8[0], pDhcp->bp_yiaddr.au8[1], pDhcp->bp_yiaddr.au8[2], pDhcp->bp_yiaddr.au8[3]));
|
---|
2081 | #endif /* LOG_EANBLED */
|
---|
2082 |
|
---|
2083 | /*
|
---|
2084 | * Act upon the message.
|
---|
2085 | */
|
---|
2086 | switch (MsgType)
|
---|
2087 | {
|
---|
2088 | #if 0
|
---|
2089 | case RTNET_DHCP_MT_REQUEST:
|
---|
2090 | /** @todo Check for valid non-broadcast requests w/ IP for any of the MACs we
|
---|
2091 | * know, and add the IP to the cache. */
|
---|
2092 | break;
|
---|
2093 | #endif
|
---|
2094 |
|
---|
2095 |
|
---|
2096 | /*
|
---|
2097 | * Lookup the interface by its MAC address and insert the IPv4 address into the cache.
|
---|
2098 | * Delete the old client address first, just in case it changed in a renewal.
|
---|
2099 | */
|
---|
2100 | case RTNET_DHCP_MT_ACK:
|
---|
2101 | if (intnetR0IPv4AddrIsGood(pDhcp->bp_yiaddr))
|
---|
2102 | {
|
---|
2103 | PINTNETIF pMatchingIf = NULL;
|
---|
2104 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
2105 |
|
---|
2106 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
2107 | while (iIf-- > 0)
|
---|
2108 | {
|
---|
2109 | PINTNETIF pCur = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
2110 | if ( intnetR0IfHasMacAddr(pCur)
|
---|
2111 | && !memcmp(&pCur->MacAddr, &pDhcp->bp_chaddr, sizeof(RTMAC)))
|
---|
2112 | {
|
---|
2113 | intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2114 | (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
|
---|
2115 | if (!pMatchingIf)
|
---|
2116 | {
|
---|
2117 | pMatchingIf = pCur;
|
---|
2118 | intnetR0BusyIncIf(pMatchingIf);
|
---|
2119 | }
|
---|
2120 | }
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
2124 |
|
---|
2125 | if (pMatchingIf)
|
---|
2126 | {
|
---|
2127 | intnetR0IfAddrCacheAdd(pMatchingIf, &pMatchingIf->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2128 | (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
|
---|
2129 | intnetR0BusyDecIf(pMatchingIf);
|
---|
2130 | }
|
---|
2131 | }
|
---|
2132 | return;
|
---|
2133 |
|
---|
2134 |
|
---|
2135 | /*
|
---|
2136 | * Lookup the interface by its MAC address and remove the IPv4 address(es) from the cache.
|
---|
2137 | */
|
---|
2138 | case RTNET_DHCP_MT_RELEASE:
|
---|
2139 | {
|
---|
2140 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
2141 |
|
---|
2142 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
2143 | while (iIf-- > 0)
|
---|
2144 | {
|
---|
2145 | PINTNETIF pCur = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
2146 | if ( intnetR0IfHasMacAddr(pCur)
|
---|
2147 | && !memcmp(&pCur->MacAddr, &pDhcp->bp_chaddr, sizeof(RTMAC)))
|
---|
2148 | {
|
---|
2149 | intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2150 | (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
|
---|
2151 | intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2152 | (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
|
---|
2153 | }
|
---|
2154 | }
|
---|
2155 |
|
---|
2156 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
2157 | break;
|
---|
2158 | }
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | }
|
---|
2162 |
|
---|
2163 |
|
---|
2164 | /**
|
---|
2165 | * Worker for intnetR0TrunkIfSnoopAddr that takes care of what
|
---|
2166 | * is likely to be a DHCP message.
|
---|
2167 | *
|
---|
2168 | * The caller has already check that the UDP source and destination ports
|
---|
2169 | * are BOOTPS or BOOTPC.
|
---|
2170 | *
|
---|
2171 | * @param pNetwork The network this frame was seen on.
|
---|
2172 | * @param pSG The gather list for the frame.
|
---|
2173 | */
|
---|
2174 | static void intnetR0TrunkIfSnoopDhcp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
|
---|
2175 | {
|
---|
2176 | /*
|
---|
2177 | * Get a pointer to a linear copy of the full packet, using the
|
---|
2178 | * temporary buffer if necessary.
|
---|
2179 | */
|
---|
2180 | PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((PCRTNETETHERHDR)pSG->aSegs[0].pv + 1);
|
---|
2181 | uint32_t cbPacket = pSG->cbTotal - sizeof(RTNETETHERHDR);
|
---|
2182 | if (pSG->cSegsUsed > 1)
|
---|
2183 | {
|
---|
2184 | cbPacket = RT_MIN(cbPacket, INTNETNETWORK_TMP_SIZE);
|
---|
2185 | Log6(("intnetR0TrunkIfSnoopDhcp: Copying IPv4/UDP/DHCP pkt %u\n", cbPacket));
|
---|
2186 | if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
|
---|
2187 | return;
|
---|
2188 | //pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
|
---|
2189 | pIpHdr = (PCRTNETIPV4)pNetwork->pbTmp;
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 | /*
|
---|
2193 | * Validate the IP header and find the UDP packet.
|
---|
2194 | */
|
---|
2195 | if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fChecksum*/))
|
---|
2196 | {
|
---|
2197 | Log(("intnetR0TrunkIfSnoopDhcp: bad ip header\n"));
|
---|
2198 | return;
|
---|
2199 | }
|
---|
2200 | uint32_t cbIpHdr = pIpHdr->ip_hl * 4;
|
---|
2201 |
|
---|
2202 | /*
|
---|
2203 | * Hand it over to the common DHCP snooper.
|
---|
2204 | */
|
---|
2205 | intnetR0NetworkSnoopDhcp(pNetwork, pIpHdr, (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr), cbPacket - cbIpHdr);
|
---|
2206 | }
|
---|
2207 |
|
---|
2208 | #endif /* INTNET_WITH_DHCP_SNOOPING */
|
---|
2209 |
|
---|
2210 |
|
---|
2211 | /**
|
---|
2212 | * Snoops up source addresses from ARP requests and purge these from the address
|
---|
2213 | * caches.
|
---|
2214 | *
|
---|
2215 | * The purpose of this purging is to get rid of stale addresses.
|
---|
2216 | *
|
---|
2217 | * @param pNetwork The network this frame was seen on.
|
---|
2218 | * @param pSG The gather list for the frame.
|
---|
2219 | */
|
---|
2220 | static void intnetR0TrunkIfSnoopArp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
|
---|
2221 | {
|
---|
2222 | /*
|
---|
2223 | * Check the minimum size first.
|
---|
2224 | */
|
---|
2225 | if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
|
---|
2226 | return;
|
---|
2227 |
|
---|
2228 | /*
|
---|
2229 | * Copy to temporary buffer if necessary.
|
---|
2230 | */
|
---|
2231 | uint32_t cbPacket = RT_MIN(pSG->cbTotal, sizeof(RTNETARPIPV4));
|
---|
2232 | PCRTNETARPIPV4 pArpIPv4 = (PCRTNETARPIPV4)((uintptr_t)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
|
---|
2233 | if ( pSG->cSegsUsed != 1
|
---|
2234 | && pSG->aSegs[0].cb < cbPacket)
|
---|
2235 | {
|
---|
2236 | if ( (pSG->fFlags & (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP))
|
---|
2237 | != (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP)
|
---|
2238 | && !intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
|
---|
2239 | return;
|
---|
2240 | pArpIPv4 = (PCRTNETARPIPV4)pNetwork->pbTmp;
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 | /*
|
---|
2244 | * Ignore packets which doesn't interest us or we perceive as malformed.
|
---|
2245 | */
|
---|
2246 | if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
|
---|
2247 | || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
|
---|
2248 | || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
|
---|
2249 | || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
|
---|
2250 | return;
|
---|
2251 | uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
|
---|
2252 | if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
|
---|
2253 | && ar_oper != RTNET_ARPOP_REPLY))
|
---|
2254 | {
|
---|
2255 | Log6(("ts-ar: op=%#x\n", ar_oper));
|
---|
2256 | return;
|
---|
2257 | }
|
---|
2258 |
|
---|
2259 | /*
|
---|
2260 | * Delete the source address if it's OK.
|
---|
2261 | */
|
---|
2262 | if ( !intnetR0IsMacAddrMulticast(&pArpIPv4->ar_sha)
|
---|
2263 | && ( pArpIPv4->ar_sha.au16[0]
|
---|
2264 | || pArpIPv4->ar_sha.au16[1]
|
---|
2265 | || pArpIPv4->ar_sha.au16[2])
|
---|
2266 | && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
|
---|
2267 | {
|
---|
2268 | Log6(("ts-ar: %d.%d.%d.%d / %.6Rhxs\n", pArpIPv4->ar_spa.au8[0], pArpIPv4->ar_spa.au8[1],
|
---|
2269 | pArpIPv4->ar_spa.au8[2], pArpIPv4->ar_spa.au8[3], &pArpIPv4->ar_sha));
|
---|
2270 | intnetR0NetworkAddrCacheDelete(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_spa,
|
---|
2271 | kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_spa), "tif/arp");
|
---|
2272 | }
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 |
|
---|
2276 | #ifdef INTNET_WITH_DHCP_SNOOPING
|
---|
2277 | /**
|
---|
2278 | * Snoop up addresses from ARP and DHCP traffic from frames coming
|
---|
2279 | * over the trunk connection.
|
---|
2280 | *
|
---|
2281 | * The caller is responsible for do some basic filtering before calling
|
---|
2282 | * this function.
|
---|
2283 | * For IPv4 this means checking against the minimum DHCPv4 frame size.
|
---|
2284 | *
|
---|
2285 | * @param pNetwork The network.
|
---|
2286 | * @param pSG The SG list for the frame.
|
---|
2287 | * @param EtherType The Ethertype of the frame.
|
---|
2288 | */
|
---|
2289 | static void intnetR0TrunkIfSnoopAddr(PINTNETNETWORK pNetwork, PCINTNETSG pSG, uint16_t EtherType)
|
---|
2290 | {
|
---|
2291 | switch (EtherType)
|
---|
2292 | {
|
---|
2293 | case RTNET_ETHERTYPE_IPV4:
|
---|
2294 | {
|
---|
2295 | uint32_t cbIpHdr;
|
---|
2296 | uint8_t b;
|
---|
2297 |
|
---|
2298 | Assert(pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN);
|
---|
2299 | if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN)
|
---|
2300 | {
|
---|
2301 | /* check if the protocol is UDP */
|
---|
2302 | PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
|
---|
2303 | if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
|
---|
2304 | return;
|
---|
2305 |
|
---|
2306 | /* get the TCP header length */
|
---|
2307 | cbIpHdr = pIpHdr->ip_hl * 4;
|
---|
2308 | }
|
---|
2309 | else
|
---|
2310 | {
|
---|
2311 | /* check if the protocol is UDP */
|
---|
2312 | if ( intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_p))
|
---|
2313 | != RTNETIPV4_PROT_UDP)
|
---|
2314 | return;
|
---|
2315 |
|
---|
2316 | /* get the TCP header length */
|
---|
2317 | b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + 0); /* (IPv4 first byte, a bitfield) */
|
---|
2318 | cbIpHdr = (b & 0x0f) * 4;
|
---|
2319 | }
|
---|
2320 | if (cbIpHdr < RTNETIPV4_MIN_LEN)
|
---|
2321 | return;
|
---|
2322 |
|
---|
2323 | /* compare the ports. */
|
---|
2324 | if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + cbIpHdr + RTNETUDP_MIN_LEN)
|
---|
2325 | {
|
---|
2326 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR) + cbIpHdr);
|
---|
2327 | if ( ( RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPS
|
---|
2328 | && RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPS)
|
---|
2329 | || ( RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPC
|
---|
2330 | && RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPC))
|
---|
2331 | return;
|
---|
2332 | }
|
---|
2333 | else
|
---|
2334 | {
|
---|
2335 | /* get the lower byte of the UDP source port number. */
|
---|
2336 | b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport) + 1);
|
---|
2337 | if ( b != RTNETIPV4_PORT_BOOTPS
|
---|
2338 | && b != RTNETIPV4_PORT_BOOTPC)
|
---|
2339 | return;
|
---|
2340 | uint8_t SrcPort = b;
|
---|
2341 | b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport));
|
---|
2342 | if (b)
|
---|
2343 | return;
|
---|
2344 |
|
---|
2345 | /* get the lower byte of the UDP destination port number. */
|
---|
2346 | b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport) + 1);
|
---|
2347 | if ( b != RTNETIPV4_PORT_BOOTPS
|
---|
2348 | && b != RTNETIPV4_PORT_BOOTPC)
|
---|
2349 | return;
|
---|
2350 | if (b == SrcPort)
|
---|
2351 | return;
|
---|
2352 | b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport));
|
---|
2353 | if (b)
|
---|
2354 | return;
|
---|
2355 | }
|
---|
2356 | intnetR0TrunkIfSnoopDhcp(pNetwork, pSG);
|
---|
2357 | break;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | case RTNET_ETHERTYPE_ARP:
|
---|
2361 | intnetR0TrunkIfSnoopArp(pNetwork, pSG);
|
---|
2362 | break;
|
---|
2363 | }
|
---|
2364 | }
|
---|
2365 | #endif /* INTNET_WITH_DHCP_SNOOPING */
|
---|
2366 |
|
---|
2367 | /**
|
---|
2368 | * Deals with an IPv6 packet.
|
---|
2369 | *
|
---|
2370 | * This will fish out the source IP address and add it to the cache.
|
---|
2371 | * Then it will look for DHCPRELEASE requests (?) and anything else
|
---|
2372 | * that we might find useful later.
|
---|
2373 | *
|
---|
2374 | * @param pIf The interface that's sending the frame.
|
---|
2375 | * @param pIpHdr Pointer to the IPv4 header in the frame.
|
---|
2376 | * @param cbPacket The size of the packet, or more correctly the
|
---|
2377 | * size of the frame without the ethernet header.
|
---|
2378 | * @param fGso Set if this is a GSO frame, clear if regular.
|
---|
2379 | */
|
---|
2380 | static void intnetR0IfSnoopIPv6SourceAddr(PINTNETIF pIf, PCRTNETIPV6 pIpHdr, uint32_t cbPacket, bool fGso)
|
---|
2381 | {
|
---|
2382 | /*
|
---|
2383 | * Check the header size first to prevent access invalid data.
|
---|
2384 | */
|
---|
2385 | if (cbPacket < RTNETIPV6_MIN_LEN)
|
---|
2386 | return;
|
---|
2387 |
|
---|
2388 | /*
|
---|
2389 | * If the source address is good (not multicast) and
|
---|
2390 | * not already in the address cache of the sender, add it.
|
---|
2391 | */
|
---|
2392 | RTNETADDRU Addr;
|
---|
2393 | Addr.IPv6 = pIpHdr->ip6_src;
|
---|
2394 |
|
---|
2395 | if ( intnetR0IPv6AddrIsGood(Addr.IPv6) && (pIpHdr->ip6_hlim == 0xff)
|
---|
2396 | && intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv6], &Addr, sizeof(Addr.IPv6)) < 0)
|
---|
2397 | {
|
---|
2398 | intnetR0IfAddrCacheAddIt(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv6], &Addr, "if/ipv6");
|
---|
2399 | }
|
---|
2400 | }
|
---|
2401 |
|
---|
2402 |
|
---|
2403 | /**
|
---|
2404 | * Deals with an IPv4 packet.
|
---|
2405 | *
|
---|
2406 | * This will fish out the source IP address and add it to the cache.
|
---|
2407 | * Then it will look for DHCPRELEASE requests (?) and anything else
|
---|
2408 | * that we might find useful later.
|
---|
2409 | *
|
---|
2410 | * @param pIf The interface that's sending the frame.
|
---|
2411 | * @param pIpHdr Pointer to the IPv4 header in the frame.
|
---|
2412 | * @param cbPacket The size of the packet, or more correctly the
|
---|
2413 | * size of the frame without the ethernet header.
|
---|
2414 | * @param fGso Set if this is a GSO frame, clear if regular.
|
---|
2415 | */
|
---|
2416 | static void intnetR0IfSnoopIPv4SourceAddr(PINTNETIF pIf, PCRTNETIPV4 pIpHdr, uint32_t cbPacket, bool fGso)
|
---|
2417 | {
|
---|
2418 | /*
|
---|
2419 | * Check the header size first to prevent access invalid data.
|
---|
2420 | */
|
---|
2421 | if (cbPacket < RTNETIPV4_MIN_LEN)
|
---|
2422 | return;
|
---|
2423 | uint32_t cbHdr = (uint32_t)pIpHdr->ip_hl * 4;
|
---|
2424 | if ( cbHdr < RTNETIPV4_MIN_LEN
|
---|
2425 | || cbPacket < cbHdr)
|
---|
2426 | return;
|
---|
2427 |
|
---|
2428 | /*
|
---|
2429 | * If the source address is good (not broadcast or my network) and
|
---|
2430 | * not already in the address cache of the sender, add it. Validate
|
---|
2431 | * the IP header before adding it.
|
---|
2432 | */
|
---|
2433 | bool fValidatedIpHdr = false;
|
---|
2434 | RTNETADDRU Addr;
|
---|
2435 | Addr.IPv4 = pIpHdr->ip_src;
|
---|
2436 | if ( intnetR0IPv4AddrIsGood(Addr.IPv4)
|
---|
2437 | && intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(Addr.IPv4)) < 0)
|
---|
2438 | {
|
---|
2439 | if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket, !fGso /*fChecksum*/))
|
---|
2440 | {
|
---|
2441 | Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header\n"));
|
---|
2442 | return;
|
---|
2443 | }
|
---|
2444 | intnetR0IfAddrCacheAddIt(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, "if/ipv4");
|
---|
2445 | fValidatedIpHdr = true;
|
---|
2446 | }
|
---|
2447 |
|
---|
2448 | #ifdef INTNET_WITH_DHCP_SNOOPING
|
---|
2449 | /*
|
---|
2450 | * Check for potential DHCP packets.
|
---|
2451 | */
|
---|
2452 | if ( pIpHdr->ip_p == RTNETIPV4_PROT_UDP /* DHCP is UDP. */
|
---|
2453 | && cbPacket >= cbHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN /* Min DHCP packet len. */
|
---|
2454 | && !fGso) /* GSO is not applicable to DHCP traffic. */
|
---|
2455 | {
|
---|
2456 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pIpHdr + cbHdr);
|
---|
2457 | if ( ( RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS
|
---|
2458 | || RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPS)
|
---|
2459 | && ( RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPC
|
---|
2460 | || RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPC))
|
---|
2461 | {
|
---|
2462 | if ( fValidatedIpHdr
|
---|
2463 | || RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket, !fGso /*fChecksum*/))
|
---|
2464 | intnetR0NetworkSnoopDhcp(pIf->pNetwork, pIpHdr, pUdpHdr, cbPacket - cbHdr);
|
---|
2465 | else
|
---|
2466 | Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header (dhcp)\n"));
|
---|
2467 | }
|
---|
2468 | }
|
---|
2469 | #endif /* INTNET_WITH_DHCP_SNOOPING */
|
---|
2470 | }
|
---|
2471 |
|
---|
2472 |
|
---|
2473 | /**
|
---|
2474 | * Snoop up source addresses from an ARP request or reply.
|
---|
2475 | *
|
---|
2476 | * @param pIf The interface that's sending the frame.
|
---|
2477 | * @param pHdr The ARP header.
|
---|
2478 | * @param cbPacket The size of the packet (might be larger than the ARP
|
---|
2479 | * request 'cause of min ethernet frame size).
|
---|
2480 | * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
|
---|
2481 | * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
|
---|
2482 | */
|
---|
2483 | static void intnetR0IfSnoopArpAddr(PINTNETIF pIf, PCRTNETARPIPV4 pArpIPv4, uint32_t cbPacket, uint16_t *pfSgFlags)
|
---|
2484 | {
|
---|
2485 | /*
|
---|
2486 | * Ignore packets which doesn't interest us or we perceive as malformed.
|
---|
2487 | */
|
---|
2488 | if (RT_UNLIKELY(cbPacket < sizeof(RTNETARPIPV4)))
|
---|
2489 | return;
|
---|
2490 | if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
|
---|
2491 | || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
|
---|
2492 | || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
|
---|
2493 | || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
|
---|
2494 | return;
|
---|
2495 | uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
|
---|
2496 | if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
|
---|
2497 | && ar_oper != RTNET_ARPOP_REPLY))
|
---|
2498 | {
|
---|
2499 | Log6(("ar_oper=%#x\n", ar_oper));
|
---|
2500 | return;
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | /*
|
---|
2504 | * Tag the SG as ARP IPv4 for later editing, then check for addresses
|
---|
2505 | * which can be removed or added to the address cache of the sender.
|
---|
2506 | */
|
---|
2507 | *pfSgFlags |= INTNETSG_FLAGS_ARP_IPV4;
|
---|
2508 |
|
---|
2509 | if ( ar_oper == RTNET_ARPOP_REPLY
|
---|
2510 | && !intnetR0IsMacAddrMulticast(&pArpIPv4->ar_tha)
|
---|
2511 | && ( pArpIPv4->ar_tha.au16[0]
|
---|
2512 | || pArpIPv4->ar_tha.au16[1]
|
---|
2513 | || pArpIPv4->ar_tha.au16[2])
|
---|
2514 | && intnetR0IPv4AddrIsGood(pArpIPv4->ar_tpa))
|
---|
2515 | intnetR0IfAddrCacheDelete(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2516 | (PCRTNETADDRU)&pArpIPv4->ar_tpa, sizeof(RTNETADDRIPV4), "if/arp");
|
---|
2517 |
|
---|
2518 | if ( !memcmp(&pArpIPv4->ar_sha, &pIf->MacAddr, sizeof(RTMAC))
|
---|
2519 | && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
|
---|
2520 | intnetR0IfAddrCacheAdd(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
|
---|
2521 | (PCRTNETADDRU)&pArpIPv4->ar_spa, sizeof(RTNETADDRIPV4), "if/arp");
|
---|
2522 | }
|
---|
2523 |
|
---|
2524 |
|
---|
2525 |
|
---|
2526 | /**
|
---|
2527 | * Checks packets send by a normal interface for new network
|
---|
2528 | * layer addresses.
|
---|
2529 | *
|
---|
2530 | * @param pIf The interface that's sending the frame.
|
---|
2531 | * @param pbFrame The frame.
|
---|
2532 | * @param cbFrame The size of the frame.
|
---|
2533 | * @param fGso Set if this is a GSO frame, clear if regular.
|
---|
2534 | * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
|
---|
2535 | * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
|
---|
2536 | */
|
---|
2537 | static void intnetR0IfSnoopAddr(PINTNETIF pIf, uint8_t const *pbFrame, uint32_t cbFrame, bool fGso, uint16_t *pfSgFlags)
|
---|
2538 | {
|
---|
2539 | /*
|
---|
2540 | * Fish out the ethertype and look for stuff we can handle.
|
---|
2541 | */
|
---|
2542 | if (cbFrame <= sizeof(RTNETETHERHDR))
|
---|
2543 | return;
|
---|
2544 | cbFrame -= sizeof(RTNETETHERHDR);
|
---|
2545 |
|
---|
2546 | uint16_t EtherType = RT_H2BE_U16(((PCRTNETETHERHDR)pbFrame)->EtherType);
|
---|
2547 | switch (EtherType)
|
---|
2548 | {
|
---|
2549 | case RTNET_ETHERTYPE_IPV4:
|
---|
2550 | intnetR0IfSnoopIPv4SourceAddr(pIf, (PCRTNETIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, fGso);
|
---|
2551 | break;
|
---|
2552 |
|
---|
2553 | case RTNET_ETHERTYPE_IPV6:
|
---|
2554 | intnetR0IfSnoopIPv6SourceAddr(pIf, (PCRTNETIPV6)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, fGso);
|
---|
2555 | break;
|
---|
2556 |
|
---|
2557 | #if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
|
---|
2558 | case RTNET_ETHERTYPE_IPX_1:
|
---|
2559 | case RTNET_ETHERTYPE_IPX_2:
|
---|
2560 | case RTNET_ETHERTYPE_IPX_3:
|
---|
2561 | intnetR0IfSnoopIpxSourceAddr(pIf, (PCINTNETIPX)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
|
---|
2562 | break;
|
---|
2563 | #endif
|
---|
2564 | case RTNET_ETHERTYPE_ARP:
|
---|
2565 | intnetR0IfSnoopArpAddr(pIf, (PCRTNETARPIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
|
---|
2566 | break;
|
---|
2567 | }
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 |
|
---|
2571 | /**
|
---|
2572 | * Writes a frame packet to the ring buffer.
|
---|
2573 | *
|
---|
2574 | * @returns VBox status code.
|
---|
2575 | * @param pBuf The buffer.
|
---|
2576 | * @param pRingBuf The ring buffer to read from.
|
---|
2577 | * @param pSG The gather list.
|
---|
2578 | * @param pNewDstMac Set the destination MAC address to the address if specified.
|
---|
2579 | */
|
---|
2580 | static int intnetR0RingWriteFrame(PINTNETRINGBUF pRingBuf, PCINTNETSG pSG, PCRTMAC pNewDstMac)
|
---|
2581 | {
|
---|
2582 | PINTNETHDR pHdr = NULL; /* shut up gcc*/
|
---|
2583 | void *pvDst = NULL; /* ditto */
|
---|
2584 | int rc;
|
---|
2585 | if (pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
|
---|
2586 | rc = IntNetRingAllocateFrame(pRingBuf, pSG->cbTotal, &pHdr, &pvDst);
|
---|
2587 | else
|
---|
2588 | rc = IntNetRingAllocateGsoFrame(pRingBuf, pSG->cbTotal, &pSG->GsoCtx, &pHdr, &pvDst);
|
---|
2589 | if (RT_SUCCESS(rc))
|
---|
2590 | {
|
---|
2591 | IntNetSgRead(pSG, pvDst);
|
---|
2592 | if (pNewDstMac)
|
---|
2593 | ((PRTNETETHERHDR)pvDst)->DstMac = *pNewDstMac;
|
---|
2594 |
|
---|
2595 | IntNetRingCommitFrame(pRingBuf, pHdr);
|
---|
2596 | return VINF_SUCCESS;
|
---|
2597 | }
|
---|
2598 | return rc;
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 |
|
---|
2602 | /**
|
---|
2603 | * Sends a frame to a specific interface.
|
---|
2604 | *
|
---|
2605 | * @param pIf The interface.
|
---|
2606 | * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
|
---|
2607 | * @param pSG The gather buffer which data is being sent to the interface.
|
---|
2608 | * @param pNewDstMac Set the destination MAC address to the address if specified.
|
---|
2609 | */
|
---|
2610 | static void intnetR0IfSend(PINTNETIF pIf, PINTNETIF pIfSender, PINTNETSG pSG, PCRTMAC pNewDstMac)
|
---|
2611 | {
|
---|
2612 | /*
|
---|
2613 | * Grab the receive/producer lock and copy over the frame.
|
---|
2614 | */
|
---|
2615 | RTSpinlockAcquire(pIf->hRecvInSpinlock);
|
---|
2616 | int rc = intnetR0RingWriteFrame(&pIf->pIntBuf->Recv, pSG, pNewDstMac);
|
---|
2617 | RTSpinlockReleaseNoInts(pIf->hRecvInSpinlock);
|
---|
2618 | if (RT_SUCCESS(rc))
|
---|
2619 | {
|
---|
2620 | pIf->cYields = 0;
|
---|
2621 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
2622 | return;
|
---|
2623 | }
|
---|
2624 |
|
---|
2625 | Log(("intnetR0IfSend: overflow cb=%d hIf=%RX32\n", pSG->cbTotal, pIf->hIf));
|
---|
2626 |
|
---|
2627 | /*
|
---|
2628 | * Scheduling hack, for unicore machines primarily.
|
---|
2629 | */
|
---|
2630 | if ( pIf->fActive
|
---|
2631 | && pIf->cYields < 4 /* just twice */
|
---|
2632 | && pIfSender /* but not if it's from the trunk */
|
---|
2633 | && RTThreadPreemptIsEnabled(NIL_RTTHREAD)
|
---|
2634 | )
|
---|
2635 | {
|
---|
2636 | unsigned cYields = 2;
|
---|
2637 | while (--cYields > 0)
|
---|
2638 | {
|
---|
2639 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
2640 | RTThreadYield();
|
---|
2641 |
|
---|
2642 | RTSpinlockAcquire(pIf->hRecvInSpinlock);
|
---|
2643 | rc = intnetR0RingWriteFrame(&pIf->pIntBuf->Recv, pSG, pNewDstMac);
|
---|
2644 | RTSpinlockReleaseNoInts(pIf->hRecvInSpinlock);
|
---|
2645 | if (RT_SUCCESS(rc))
|
---|
2646 | {
|
---|
2647 | STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsOk);
|
---|
2648 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
2649 | return;
|
---|
2650 | }
|
---|
2651 | pIf->cYields++;
|
---|
2652 | }
|
---|
2653 | STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsNok);
|
---|
2654 | }
|
---|
2655 |
|
---|
2656 | /* ok, the frame is lost. */
|
---|
2657 | STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatLost);
|
---|
2658 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
2659 | }
|
---|
2660 |
|
---|
2661 |
|
---|
2662 | /**
|
---|
2663 | * Fallback path that does the GSO segmenting before passing the frame on to the
|
---|
2664 | * trunk interface.
|
---|
2665 | *
|
---|
2666 | * The caller holds the trunk lock.
|
---|
2667 | *
|
---|
2668 | * @param pThis The trunk.
|
---|
2669 | * @param pIfSender The IF sending the frame.
|
---|
2670 | * @param pSG Pointer to the gather list.
|
---|
2671 | * @param fDst The destination flags.
|
---|
2672 | */
|
---|
2673 | static int intnetR0TrunkIfSendGsoFallback(PINTNETTRUNKIF pThis, PINTNETIF pIfSender, PINTNETSG pSG, uint32_t fDst)
|
---|
2674 | {
|
---|
2675 | /*
|
---|
2676 | * Since we're only using this for GSO frame coming from the internal
|
---|
2677 | * network interfaces and never the trunk, we can assume there is only
|
---|
2678 | * one segment. This simplifies the code quite a bit.
|
---|
2679 | */
|
---|
2680 | Assert(PDMNetGsoIsValid(&pSG->GsoCtx, sizeof(pSG->GsoCtx), pSG->cbTotal));
|
---|
2681 | AssertReturn(pSG->cSegsUsed == 1, VERR_INTERNAL_ERROR_4);
|
---|
2682 |
|
---|
2683 | union
|
---|
2684 | {
|
---|
2685 | uint8_t abBuf[sizeof(INTNETSG) + sizeof(INTNETSEG)];
|
---|
2686 | INTNETSG SG;
|
---|
2687 | } u;
|
---|
2688 |
|
---|
2689 | /*
|
---|
2690 | * Carve out the frame segments with the header and frame in different
|
---|
2691 | * scatter / gather segments.
|
---|
2692 | */
|
---|
2693 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(&pSG->GsoCtx, pSG->cbTotal);
|
---|
2694 | for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
2695 | {
|
---|
2696 | uint32_t cbSegPayload, cbSegHdrs;
|
---|
2697 | uint32_t offSegPayload = PDMNetGsoCarveSegment(&pSG->GsoCtx, (uint8_t *)pSG->aSegs[0].pv, pSG->cbTotal, iSeg, cSegs,
|
---|
2698 | pIfSender->abGsoHdrs, &cbSegHdrs, &cbSegPayload);
|
---|
2699 |
|
---|
2700 | IntNetSgInitTempSegs(&u.SG, cbSegHdrs + cbSegPayload, 2, 2);
|
---|
2701 | u.SG.aSegs[0].Phys = NIL_RTHCPHYS;
|
---|
2702 | u.SG.aSegs[0].pv = pIfSender->abGsoHdrs;
|
---|
2703 | u.SG.aSegs[0].cb = cbSegHdrs;
|
---|
2704 | u.SG.aSegs[1].Phys = NIL_RTHCPHYS;
|
---|
2705 | u.SG.aSegs[1].pv = (uint8_t *)pSG->aSegs[0].pv + offSegPayload;
|
---|
2706 | u.SG.aSegs[1].cb = (uint32_t)cbSegPayload;
|
---|
2707 |
|
---|
2708 | int rc = pThis->pIfPort->pfnXmit(pThis->pIfPort, pIfSender->pvIfData, &u.SG, fDst);
|
---|
2709 | if (RT_FAILURE(rc))
|
---|
2710 | return rc;
|
---|
2711 | }
|
---|
2712 | return VINF_SUCCESS;
|
---|
2713 | }
|
---|
2714 |
|
---|
2715 |
|
---|
2716 | /**
|
---|
2717 | * Checks if any of the given trunk destinations can handle this kind of GSO SG.
|
---|
2718 | *
|
---|
2719 | * @returns true if it can, false if it cannot.
|
---|
2720 | * @param pThis The trunk.
|
---|
2721 | * @param pSG The scatter / gather buffer.
|
---|
2722 | * @param fDst The destination mask.
|
---|
2723 | */
|
---|
2724 | DECLINLINE(bool) intnetR0TrunkIfCanHandleGsoFrame(PINTNETTRUNKIF pThis, PINTNETSG pSG, uint32_t fDst)
|
---|
2725 | {
|
---|
2726 | uint8_t u8Type = pSG->GsoCtx.u8Type;
|
---|
2727 | AssertReturn(u8Type < 32, false); /* paranoia */
|
---|
2728 | uint32_t fMask = RT_BIT_32(u8Type);
|
---|
2729 |
|
---|
2730 | if (fDst == INTNETTRUNKDIR_HOST)
|
---|
2731 | return !!(pThis->fHostGsoCapabilites & fMask);
|
---|
2732 | if (fDst == INTNETTRUNKDIR_WIRE)
|
---|
2733 | return !!(pThis->fWireGsoCapabilites & fMask);
|
---|
2734 | Assert(fDst == (INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST));
|
---|
2735 | return !!(pThis->fHostGsoCapabilites & pThis->fWireGsoCapabilites & fMask);
|
---|
2736 | }
|
---|
2737 |
|
---|
2738 |
|
---|
2739 | /**
|
---|
2740 | * Calculates the checksum of a full ipv6 frame.
|
---|
2741 | *
|
---|
2742 | * @returns 16-bit hecksum value.
|
---|
2743 | * @param pIpHdr The IPv6 header (network endian (big)).
|
---|
2744 | * @param bProtocol The protocol number. This can be the same as the
|
---|
2745 | * ip6_nxt field, but doesn't need to be.
|
---|
2746 | * @param cbPkt The packet size (host endian of course). This can
|
---|
2747 | * be the same as the ip6_plen field, but as with @a
|
---|
2748 | * bProtocol it won't be when extension headers are
|
---|
2749 | * present. For UDP this will be uh_ulen converted to
|
---|
2750 | * host endian.
|
---|
2751 | */
|
---|
2752 | static uint16_t computeIPv6FullChecksum(PCRTNETIPV6 pIpHdr)
|
---|
2753 | {
|
---|
2754 | uint16_t const *data;
|
---|
2755 | int len = RT_BE2H_U16(pIpHdr->ip6_plen);
|
---|
2756 | uint32_t sum = RTNetIPv6PseudoChecksum(pIpHdr);
|
---|
2757 |
|
---|
2758 | /* add the payload */
|
---|
2759 | data = (uint16_t *) (pIpHdr + 1);
|
---|
2760 | while(len > 1)
|
---|
2761 | {
|
---|
2762 | sum += *(data);
|
---|
2763 | data++;
|
---|
2764 | len -= 2;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | if(len > 0)
|
---|
2768 | sum += *((uint8_t *) data);
|
---|
2769 |
|
---|
2770 | while(sum >> 16)
|
---|
2771 | sum = (sum & 0xffff) + (sum >> 16);
|
---|
2772 |
|
---|
2773 | return (uint16_t) ~sum;
|
---|
2774 | }
|
---|
2775 |
|
---|
2776 | /**
|
---|
2777 | * Sends a frame down the trunk.
|
---|
2778 | *
|
---|
2779 | * @param pThis The trunk.
|
---|
2780 | * @param pNetwork The network the frame is being sent to.
|
---|
2781 | * @param pIfSender The IF sending the frame. Used for MAC address
|
---|
2782 | * checks in shared MAC mode.
|
---|
2783 | * @param fDst The destination flags.
|
---|
2784 | * @param pSG Pointer to the gather list.
|
---|
2785 | */
|
---|
2786 | static void intnetR0TrunkIfSend(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork, PINTNETIF pIfSender,
|
---|
2787 | uint32_t fDst, PINTNETSG pSG)
|
---|
2788 | {
|
---|
2789 | /*
|
---|
2790 | * Quick sanity check.
|
---|
2791 | */
|
---|
2792 | AssertPtr(pThis);
|
---|
2793 | AssertPtr(pNetwork);
|
---|
2794 | AssertPtr(pIfSender);
|
---|
2795 | AssertPtr(pSG);
|
---|
2796 | Assert(fDst);
|
---|
2797 | AssertReturnVoid(pThis->pIfPort);
|
---|
2798 |
|
---|
2799 | /*
|
---|
2800 | * Edit the frame if we're sharing the MAC address with the host on the wire.
|
---|
2801 | *
|
---|
2802 | * If the frame is headed for both the host and the wire, we'll have to send
|
---|
2803 | * it to the host before making any modifications, and force the OS specific
|
---|
2804 | * backend to copy it. We do this by marking it as TEMP (which is always the
|
---|
2805 | * case right now).
|
---|
2806 | */
|
---|
2807 | if ( (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
2808 | && (fDst & INTNETTRUNKDIR_WIRE))
|
---|
2809 | {
|
---|
2810 | /*
|
---|
2811 | * Dispatch it to the host before making changes.
|
---|
2812 | */
|
---|
2813 | if (fDst & INTNETTRUNKDIR_HOST)
|
---|
2814 | {
|
---|
2815 | Assert(pSG->fFlags & INTNETSG_FLAGS_TEMP); /* make sure copy is forced */
|
---|
2816 | intnetR0TrunkIfSend(pThis, pNetwork, pIfSender, INTNETTRUNKDIR_HOST, pSG);
|
---|
2817 | fDst &= ~INTNETTRUNKDIR_HOST;
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | /*
|
---|
2821 | * Edit the source address so that it it's the same as the host.
|
---|
2822 | */
|
---|
2823 | /* ASSUME frame from IntNetR0IfSend! */
|
---|
2824 | AssertReturnVoid(pSG->cSegsUsed == 1);
|
---|
2825 | AssertReturnVoid(pSG->cbTotal >= sizeof(RTNETETHERHDR));
|
---|
2826 | AssertReturnVoid(pIfSender);
|
---|
2827 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)pSG->aSegs[0].pv;
|
---|
2828 |
|
---|
2829 | pEthHdr->SrcMac = pThis->MacAddr;
|
---|
2830 |
|
---|
2831 | /*
|
---|
2832 | * Deal with tags from the snooping phase.
|
---|
2833 | */
|
---|
2834 | if (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4)
|
---|
2835 | {
|
---|
2836 | /*
|
---|
2837 | * APR IPv4: replace hardware (MAC) addresses because these end up
|
---|
2838 | * in ARP caches. So, if we don't the other machines will
|
---|
2839 | * send the packets to the MAC address of the guest
|
---|
2840 | * instead of the one of the host, which won't work on
|
---|
2841 | * wireless of course...
|
---|
2842 | */
|
---|
2843 | PRTNETARPIPV4 pArp = (PRTNETARPIPV4)(pEthHdr + 1);
|
---|
2844 | if (!memcmp(&pArp->ar_sha, &pIfSender->MacAddr, sizeof(RTMAC)))
|
---|
2845 | {
|
---|
2846 | Log6(("tw: ar_sha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_sha, &pThis->MacAddr));
|
---|
2847 | pArp->ar_sha = pThis->MacAddr;
|
---|
2848 | }
|
---|
2849 | if (!memcmp(&pArp->ar_tha, &pIfSender->MacAddr, sizeof(RTMAC))) /* just in case... */
|
---|
2850 | {
|
---|
2851 | Log6(("tw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_tha, &pThis->MacAddr));
|
---|
2852 | pArp->ar_tha = pThis->MacAddr;
|
---|
2853 | }
|
---|
2854 | }
|
---|
2855 | else if (pEthHdr->EtherType == RT_H2BE_U16(RTNET_ETHERTYPE_IPV6))
|
---|
2856 | {
|
---|
2857 | /*
|
---|
2858 | * IPV6 ICMP Neighbor Discovery : replace
|
---|
2859 | * 1) the advertised source mac address in outgoing neighbor sollicitations
|
---|
2860 | * with the HW MAC address of the trunk interface,
|
---|
2861 | * 2) the advertised target mac address in outgoing neighbor advertisements
|
---|
2862 | * with the HW mac address of the trunk interface.
|
---|
2863 | *
|
---|
2864 | * Note that this only applies to traffic going out on the trunk. Incoming
|
---|
2865 | * NS/NA will never advertise any VM mac address, so we do not need to touch
|
---|
2866 | * them. Other VMs on this bridge as well as the host will see and use the VM's
|
---|
2867 | * actual mac addresses.
|
---|
2868 | *
|
---|
2869 | */
|
---|
2870 |
|
---|
2871 | PRTNETIPV6 pIPv6 = (PRTNETIPV6)(pEthHdr + 1);
|
---|
2872 | PRTNETNDP pNd = (PRTNETNDP)(pIPv6 + 1);
|
---|
2873 | PRTNETNDP_SLLA_OPT pLLAOpt = (PRTNETNDP_SLLA_OPT)(pNd + 1);
|
---|
2874 |
|
---|
2875 | /* make sure we have enough bytes to work with */
|
---|
2876 | if(pSG->cbTotal >= (RTNETIPV6_MIN_LEN + RTNETIPV6_ICMPV6_ND_WITH_LLA_OPT_MIN_LEN) &&
|
---|
2877 | /* ensure the packet came from our LAN (not gone through any router) */
|
---|
2878 | pIPv6->ip6_hlim == 0xff &&
|
---|
2879 | /* protocol has to be icmpv6 */
|
---|
2880 | pIPv6->ip6_nxt == RTNETIPV6_PROT_ICMPV6 &&
|
---|
2881 | /* we either have a sollicitation with source link layer addr. opt, or */
|
---|
2882 | ((pNd->icmp6_type == RTNETIPV6_ICMP_NS_TYPE &&
|
---|
2883 | pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0 &&
|
---|
2884 | pLLAOpt->type == RTNETIPV6_ICMP_ND_SLLA_OPT) ||
|
---|
2885 | /* an advertisement with target link layer addr. option */
|
---|
2886 | ((pNd->icmp6_type == RTNETIPV6_ICMP_NA_TYPE &&
|
---|
2887 | pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0 &&
|
---|
2888 | pLLAOpt->type == RTNETIPV6_ICMP_ND_TLLA_OPT)) ) &&
|
---|
2889 | pLLAOpt->len == RTNETIPV6_ICMP_ND_LLA_LEN)
|
---|
2890 | {
|
---|
2891 | /* swap the advertised VM MAC address with the trunk's */
|
---|
2892 | pLLAOpt->slla = pThis->MacAddr;
|
---|
2893 |
|
---|
2894 | /* recompute the checksum since we changed the packet */
|
---|
2895 | pNd->icmp6_cksum = 0;
|
---|
2896 | pNd->icmp6_cksum = computeIPv6FullChecksum(pIPv6);
|
---|
2897 | }
|
---|
2898 |
|
---|
2899 | }
|
---|
2900 | }
|
---|
2901 |
|
---|
2902 | /*
|
---|
2903 | * Send the frame, handling the GSO fallback .
|
---|
2904 | * .
|
---|
2905 | * Note! The trunk implementation will re-check that the trunk is active .
|
---|
2906 | * before sending, so we don't have to duplicate that effort here.
|
---|
2907 | */
|
---|
2908 | STAM_REL_PROFILE_START(&pIfSender->pIntBuf->StatSend2, a);
|
---|
2909 | int rc;
|
---|
2910 | if ( pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID
|
---|
2911 | || intnetR0TrunkIfCanHandleGsoFrame(pThis, pSG, fDst) )
|
---|
2912 | rc = pThis->pIfPort->pfnXmit(pThis->pIfPort, pIfSender->pvIfData, pSG, fDst);
|
---|
2913 | else
|
---|
2914 | rc = intnetR0TrunkIfSendGsoFallback(pThis, pIfSender, pSG, fDst);
|
---|
2915 | STAM_REL_PROFILE_STOP(&pIfSender->pIntBuf->StatSend2, a);
|
---|
2916 |
|
---|
2917 | /** @todo failure statistics? */
|
---|
2918 | Log2(("intnetR0TrunkIfSend: %Rrc fDst=%d\n", rc, fDst)); NOREF(rc);
|
---|
2919 | }
|
---|
2920 |
|
---|
2921 |
|
---|
2922 | /**
|
---|
2923 | * Work around the issue with WiFi routers that replace IPv6 multicast
|
---|
2924 | * Ethernet addresses with unicast ones. We check IPv6 destination address
|
---|
2925 | * to determine if the packet originally had a multicast address, and if so
|
---|
2926 | * we restore the original address and treat the modified packet as being a
|
---|
2927 | * broadcast.
|
---|
2928 | *
|
---|
2929 | * @param pNetwork The network the frame is being sent to.
|
---|
2930 | * @param pSG Pointer to the gather list for the frame.
|
---|
2931 | * @param pEthHdr Pointer to the ethernet header.
|
---|
2932 | */
|
---|
2933 | static bool intnetR0NetworkDetectAndFixNdBroadcast(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
|
---|
2934 | {
|
---|
2935 | if (RT_BE2H_U16(pEthHdr->EtherType) != RTNET_ETHERTYPE_IPV6)
|
---|
2936 | return false;
|
---|
2937 | /*
|
---|
2938 | * Check the minimum size and get a linear copy of the thing to work on,
|
---|
2939 | * using the temporary buffer if necessary.
|
---|
2940 | */
|
---|
2941 | if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
|
---|
2942 | sizeof(RTNETNDP)))
|
---|
2943 | return false;
|
---|
2944 | uint8_t bTmp[sizeof(RTNETIPV6) + sizeof(RTNETNDP)];
|
---|
2945 | PRTNETIPV6 pIPv6 = (PRTNETIPV6)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
|
---|
2946 | if ( pSG->cSegsUsed != 1
|
---|
2947 | && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
|
---|
2948 | sizeof(RTNETNDP))
|
---|
2949 | {
|
---|
2950 | Log6(("fw: Copying IPv6 pkt %u\n", sizeof(RTNETIPV6)));
|
---|
2951 | if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETIPV6)
|
---|
2952 | + sizeof(RTNETNDP), bTmp))
|
---|
2953 | return false;
|
---|
2954 | pIPv6 = (PRTNETIPV6)bTmp;
|
---|
2955 | }
|
---|
2956 |
|
---|
2957 | PCRTNETNDP pNd = (PCRTNETNDP) (pIPv6 + 1);
|
---|
2958 |
|
---|
2959 | /* Check IPv6 destination address if it is a multicast address. */
|
---|
2960 | static uint8_t auSolicitedNodeMulticastPrefix[] =
|
---|
2961 | {
|
---|
2962 | 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
2963 | 0x00, 0x00, 0x00, 0x01, 0xff
|
---|
2964 | };
|
---|
2965 | if (memcmp(pIPv6->ip6_dst.au8, auSolicitedNodeMulticastPrefix,
|
---|
2966 | sizeof(auSolicitedNodeMulticastPrefix)) == 0)
|
---|
2967 | {
|
---|
2968 | /*
|
---|
2969 | * The original must have been composed of 0x3333 followed by the last
|
---|
2970 | * four bytes of the solicited-node multicast address.
|
---|
2971 | */
|
---|
2972 | if (pSG->aSegs[0].cb < sizeof(RTNETETHERHDR))
|
---|
2973 | {
|
---|
2974 | RTMAC DstMac;
|
---|
2975 | DstMac.au16[0] = 0x3333;
|
---|
2976 | DstMac.au16[1] = pIPv6->ip6_dst.au16[6];
|
---|
2977 | DstMac.au16[2] = pIPv6->ip6_dst.au16[7];
|
---|
2978 | return intnetR0SgWritePart(pSG, RT_OFFSETOF(RTNETETHERHDR, DstMac), sizeof(RTMAC), &DstMac);
|
---|
2979 | }
|
---|
2980 | pEthHdr = (PRTNETETHERHDR)pSG->aSegs[0].pv;
|
---|
2981 | pEthHdr->DstMac.au16[0] = 0x3333;
|
---|
2982 | pEthHdr->DstMac.au16[1] = pIPv6->ip6_dst.au16[6];
|
---|
2983 | pEthHdr->DstMac.au16[2] = pIPv6->ip6_dst.au16[7];
|
---|
2984 | return true;
|
---|
2985 | }
|
---|
2986 |
|
---|
2987 | return false;
|
---|
2988 | }
|
---|
2989 |
|
---|
2990 |
|
---|
2991 | /**
|
---|
2992 | * Snoops a multicast ICMPv6 ND DAD from the wire via the trunk connection.
|
---|
2993 | *
|
---|
2994 | * @param pNetwork The network the frame is being sent to.
|
---|
2995 | * @param pSG Pointer to the gather list for the frame.
|
---|
2996 | * @param pEthHdr Pointer to the ethernet header.
|
---|
2997 | */
|
---|
2998 | static void intnetR0NetworkSnoopNAFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
|
---|
2999 | {
|
---|
3000 | /*
|
---|
3001 | * Check the minimum size and get a linear copy of the thing to work on,
|
---|
3002 | * using the temporary buffer if necessary.
|
---|
3003 | */
|
---|
3004 | if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
|
---|
3005 | sizeof(RTNETNDP)))
|
---|
3006 | return;
|
---|
3007 | PRTNETIPV6 pIPv6 = (PRTNETIPV6)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
|
---|
3008 | if ( pSG->cSegsUsed != 1
|
---|
3009 | && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
|
---|
3010 | sizeof(RTNETNDP))
|
---|
3011 | {
|
---|
3012 | Log6(("fw: Copying IPv6 pkt %u\n", sizeof(RTNETIPV6)));
|
---|
3013 | if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETIPV6)
|
---|
3014 | + sizeof(RTNETNDP), pNetwork->pbTmp))
|
---|
3015 | return;
|
---|
3016 | pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
|
---|
3017 | pIPv6 = (PRTNETIPV6)pNetwork->pbTmp;
|
---|
3018 | }
|
---|
3019 |
|
---|
3020 | PCRTNETNDP pNd = (PCRTNETNDP) (pIPv6 + 1);
|
---|
3021 |
|
---|
3022 | /*
|
---|
3023 | * a multicast NS with :: as source address means a DAD packet.
|
---|
3024 | * if it comes from the wire and we have the DAD'd address in our cache,
|
---|
3025 | * flush the entry as the address is being acquired by someone else on
|
---|
3026 | * the network.
|
---|
3027 | */
|
---|
3028 | if ( pIPv6->ip6_hlim == 0xff
|
---|
3029 | && pIPv6->ip6_nxt == RTNETIPV6_PROT_ICMPV6
|
---|
3030 | && pNd->icmp6_type == RTNETIPV6_ICMP_NS_TYPE
|
---|
3031 | && pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0
|
---|
3032 | && pIPv6->ip6_src.QWords.qw0 == 0
|
---|
3033 | && pIPv6->ip6_src.QWords.qw1 == 0)
|
---|
3034 | {
|
---|
3035 |
|
---|
3036 | intnetR0NetworkAddrCacheDelete(pNetwork, (PCRTNETADDRU) &pNd->target_address,
|
---|
3037 | kIntNetAddrType_IPv6, sizeof(RTNETADDRIPV6), "tif/ip6");
|
---|
3038 | }
|
---|
3039 | }
|
---|
3040 | /**
|
---|
3041 | * Edits an ARP packet arriving from the wire via the trunk connection.
|
---|
3042 | *
|
---|
3043 | * @param pNetwork The network the frame is being sent to.
|
---|
3044 | * @param pSG Pointer to the gather list for the frame.
|
---|
3045 | * The flags and data content may be updated.
|
---|
3046 | * @param pEthHdr Pointer to the ethernet header. This may also be
|
---|
3047 | * updated if it's a unicast...
|
---|
3048 | */
|
---|
3049 | static void intnetR0NetworkEditArpFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
|
---|
3050 | {
|
---|
3051 | /*
|
---|
3052 | * Check the minimum size and get a linear copy of the thing to work on,
|
---|
3053 | * using the temporary buffer if necessary.
|
---|
3054 | */
|
---|
3055 | if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
|
---|
3056 | return;
|
---|
3057 | PRTNETARPIPV4 pArpIPv4 = (PRTNETARPIPV4)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
|
---|
3058 | if ( pSG->cSegsUsed != 1
|
---|
3059 | && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4))
|
---|
3060 | {
|
---|
3061 | Log6(("fw: Copying ARP pkt %u\n", sizeof(RTNETARPIPV4)));
|
---|
3062 | if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETARPIPV4), pNetwork->pbTmp))
|
---|
3063 | return;
|
---|
3064 | pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
|
---|
3065 | pArpIPv4 = (PRTNETARPIPV4)pNetwork->pbTmp;
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | /*
|
---|
3069 | * Ignore packets which doesn't interest us or we perceive as malformed.
|
---|
3070 | */
|
---|
3071 | if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
|
---|
3072 | || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
|
---|
3073 | || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
|
---|
3074 | || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
|
---|
3075 | return;
|
---|
3076 | uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
|
---|
3077 | if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
|
---|
3078 | && ar_oper != RTNET_ARPOP_REPLY))
|
---|
3079 | {
|
---|
3080 | Log6(("ar_oper=%#x\n", ar_oper));
|
---|
3081 | return;
|
---|
3082 | }
|
---|
3083 |
|
---|
3084 | /* Tag it as ARP IPv4. */
|
---|
3085 | pSG->fFlags |= INTNETSG_FLAGS_ARP_IPV4;
|
---|
3086 |
|
---|
3087 | /*
|
---|
3088 | * The thing we're interested in here is a reply to a query made by a guest
|
---|
3089 | * since we modified the MAC in the initial request the guest made.
|
---|
3090 | */
|
---|
3091 | if ( ar_oper == RTNET_ARPOP_REPLY
|
---|
3092 | && !memcmp(&pArpIPv4->ar_tha, &pNetwork->MacTab.pTrunk->MacAddr, sizeof(RTMAC)))
|
---|
3093 | {
|
---|
3094 | PINTNETIF pIf = intnetR0NetworkAddrCacheLookupIf(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_tpa,
|
---|
3095 | kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_tpa));
|
---|
3096 | if (pIf)
|
---|
3097 | {
|
---|
3098 | Log6(("fw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArpIPv4->ar_tha, &pIf->MacAddr));
|
---|
3099 | pArpIPv4->ar_tha = pIf->MacAddr;
|
---|
3100 | if (!memcmp(&pEthHdr->DstMac, &pNetwork->MacTab.pTrunk->MacAddr, sizeof(RTMAC)))
|
---|
3101 | {
|
---|
3102 | Log6(("fw: DstMac %.6Rhxs -> %.6Rhxs\n", &pEthHdr->DstMac, &pIf->MacAddr));
|
---|
3103 | pEthHdr->DstMac = pIf->MacAddr;
|
---|
3104 | if ((void *)pEthHdr != pSG->aSegs[0].pv)
|
---|
3105 | intnetR0SgWritePart(pSG, RT_OFFSETOF(RTNETETHERHDR, DstMac), sizeof(RTMAC), &pIf->MacAddr);
|
---|
3106 | }
|
---|
3107 | intnetR0BusyDecIf(pIf);
|
---|
3108 |
|
---|
3109 | /* Write back the packet if we've been making changes to a buffered copy. */
|
---|
3110 | if (pSG->fFlags & INTNETSG_FLAGS_PKT_CP_IN_TMP)
|
---|
3111 | intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR), sizeof(PRTNETARPIPV4), pArpIPv4);
|
---|
3112 | }
|
---|
3113 | }
|
---|
3114 | }
|
---|
3115 |
|
---|
3116 |
|
---|
3117 | /**
|
---|
3118 | * Detects and edits an DHCP packet arriving from the internal net.
|
---|
3119 | *
|
---|
3120 | * @param pNetwork The network the frame is being sent to.
|
---|
3121 | * @param pSG Pointer to the gather list for the frame.
|
---|
3122 | * The flags and data content may be updated.
|
---|
3123 | * @param pEthHdr Pointer to the ethernet header. This may also be
|
---|
3124 | * updated if it's a unicast...
|
---|
3125 | */
|
---|
3126 | static void intnetR0NetworkEditDhcpFromIntNet(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
|
---|
3127 | {
|
---|
3128 | NOREF(pEthHdr);
|
---|
3129 |
|
---|
3130 | /*
|
---|
3131 | * Check the minimum size and get a linear copy of the thing to work on,
|
---|
3132 | * using the temporary buffer if necessary.
|
---|
3133 | */
|
---|
3134 | if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN))
|
---|
3135 | return;
|
---|
3136 | /*
|
---|
3137 | * Get a pointer to a linear copy of the full packet, using the
|
---|
3138 | * temporary buffer if necessary.
|
---|
3139 | */
|
---|
3140 | PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((PCRTNETETHERHDR)pSG->aSegs[0].pv + 1);
|
---|
3141 | uint32_t cbPacket = pSG->cbTotal - sizeof(RTNETETHERHDR);
|
---|
3142 | if (pSG->cSegsUsed > 1)
|
---|
3143 | {
|
---|
3144 | cbPacket = RT_MIN(cbPacket, INTNETNETWORK_TMP_SIZE);
|
---|
3145 | Log6(("intnetR0NetworkEditDhcpFromIntNet: Copying IPv4/UDP/DHCP pkt %u\n", cbPacket));
|
---|
3146 | if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
|
---|
3147 | return;
|
---|
3148 | //pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
|
---|
3149 | pIpHdr = (PCRTNETIPV4)pNetwork->pbTmp;
|
---|
3150 | }
|
---|
3151 |
|
---|
3152 | /*
|
---|
3153 | * Validate the IP header and find the UDP packet.
|
---|
3154 | */
|
---|
3155 | if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fCheckSum*/))
|
---|
3156 | {
|
---|
3157 | Log6(("intnetR0NetworkEditDhcpFromIntNet: bad ip header\n"));
|
---|
3158 | return;
|
---|
3159 | }
|
---|
3160 | size_t cbIpHdr = pIpHdr->ip_hl * 4;
|
---|
3161 | if ( pIpHdr->ip_p != RTNETIPV4_PROT_UDP /* DHCP is UDP. */
|
---|
3162 | || cbPacket < cbIpHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN) /* Min DHCP packet len */
|
---|
3163 | return;
|
---|
3164 |
|
---|
3165 | size_t cbUdpPkt = cbPacket - cbIpHdr;
|
---|
3166 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr);
|
---|
3167 | /* We are only interested in DHCP packets coming from client to server. */
|
---|
3168 | if ( RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPS
|
---|
3169 | || RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPC)
|
---|
3170 | return;
|
---|
3171 |
|
---|
3172 | /*
|
---|
3173 | * Check if the DHCP message is valid and get the type.
|
---|
3174 | */
|
---|
3175 | if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbUdpPkt, true /*fCheckSum*/))
|
---|
3176 | {
|
---|
3177 | Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad UDP packet\n"));
|
---|
3178 | return;
|
---|
3179 | }
|
---|
3180 | PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)(pUdpHdr + 1);
|
---|
3181 | uint8_t bMsgType;
|
---|
3182 | if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbUdpPkt - sizeof(*pUdpHdr), &bMsgType))
|
---|
3183 | {
|
---|
3184 | Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad DHCP packet\n"));
|
---|
3185 | return;
|
---|
3186 | }
|
---|
3187 |
|
---|
3188 | switch (bMsgType)
|
---|
3189 | {
|
---|
3190 | case RTNET_DHCP_MT_DISCOVER:
|
---|
3191 | case RTNET_DHCP_MT_REQUEST:
|
---|
3192 | /*
|
---|
3193 | * Must set the broadcast flag or we won't catch the respons.
|
---|
3194 | */
|
---|
3195 | if (!(pDhcp->bp_flags & RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST)))
|
---|
3196 | {
|
---|
3197 | Log6(("intnetR0NetworkEditDhcpFromIntNet: Setting broadcast flag in DHCP %#x, previously %x\n",
|
---|
3198 | bMsgType, pDhcp->bp_flags));
|
---|
3199 |
|
---|
3200 | /* Patch flags */
|
---|
3201 | uint16_t uFlags = pDhcp->bp_flags | RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST);
|
---|
3202 | intnetR0SgWritePart(pSG, (uintptr_t)&pDhcp->bp_flags - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(uFlags), &uFlags);
|
---|
3203 |
|
---|
3204 | /* Patch UDP checksum */
|
---|
3205 | uint32_t uChecksum = (uint32_t)~pUdpHdr->uh_sum + RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST);
|
---|
3206 | while (uChecksum >> 16)
|
---|
3207 | uChecksum = (uChecksum >> 16) + (uChecksum & 0xFFFF);
|
---|
3208 | uChecksum = ~uChecksum;
|
---|
3209 | intnetR0SgWritePart(pSG, (uintptr_t)&pUdpHdr->uh_sum - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(pUdpHdr->uh_sum), &uChecksum);
|
---|
3210 | }
|
---|
3211 |
|
---|
3212 | #ifdef RT_OS_DARWIN
|
---|
3213 | /*
|
---|
3214 | * Work around little endian checksum issue in mac os x 10.7.0 GM.
|
---|
3215 | */
|
---|
3216 | if ( pIpHdr->ip_tos
|
---|
3217 | && (pNetwork->fFlags & INTNET_OPEN_FLAGS_WORKAROUND_1))
|
---|
3218 | {
|
---|
3219 | /* Patch it. */
|
---|
3220 | uint8_t uTos = pIpHdr->ip_tos;
|
---|
3221 | uint8_t uZero = 0;
|
---|
3222 | intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR) + 1, sizeof(uZero), &uZero);
|
---|
3223 |
|
---|
3224 | /* Patch the IP header checksum. */
|
---|
3225 | uint32_t uChecksum = (uint32_t)~pIpHdr->ip_sum - (uTos << 8);
|
---|
3226 | while (uChecksum >> 16)
|
---|
3227 | uChecksum = (uChecksum >> 16) + (uChecksum & 0xFFFF);
|
---|
3228 | uChecksum = ~uChecksum;
|
---|
3229 |
|
---|
3230 | Log(("intnetR0NetworkEditDhcpFromIntNet: cleared ip_tos (was %#04x); ip_sum=%#06x -> %#06x\n",
|
---|
3231 | uTos, RT_BE2H_U16(pIpHdr->ip_sum), RT_BE2H_U16(uChecksum) ));
|
---|
3232 | intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_sum),
|
---|
3233 | sizeof(pIpHdr->ip_sum), &uChecksum);
|
---|
3234 | }
|
---|
3235 | #endif
|
---|
3236 | break;
|
---|
3237 | }
|
---|
3238 | }
|
---|
3239 |
|
---|
3240 |
|
---|
3241 | /**
|
---|
3242 | * Checks if the callers context is okay for sending to the specified
|
---|
3243 | * destinations.
|
---|
3244 | *
|
---|
3245 | * @returns true if it's okay, false if it isn't.
|
---|
3246 | * @param pNetwork The network.
|
---|
3247 | * @param pIfSender The interface sending or NULL if it's the trunk.
|
---|
3248 | * @param pDstTab The destination table.
|
---|
3249 | */
|
---|
3250 | DECLINLINE(bool) intnetR0NetworkIsContextOk(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCINTNETDSTTAB pDstTab)
|
---|
3251 | {
|
---|
3252 | NOREF(pNetwork);
|
---|
3253 |
|
---|
3254 | /* Sending to the trunk is the problematic path. If the trunk is the
|
---|
3255 | sender we won't be sending to it, so no problem..
|
---|
3256 | Note! fTrunkDst may be set event if if the trunk is the sender. */
|
---|
3257 | if (!pIfSender)
|
---|
3258 | return true;
|
---|
3259 |
|
---|
3260 | uint32_t const fTrunkDst = pDstTab->fTrunkDst;
|
---|
3261 | if (!fTrunkDst)
|
---|
3262 | return true;
|
---|
3263 |
|
---|
3264 | /* ASSUMES: that the trunk won't change its report while we're checking. */
|
---|
3265 | PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
|
---|
3266 | if ((fTrunkDst & pTrunk->fNoPreemptDsts) == fTrunkDst)
|
---|
3267 | return true;
|
---|
3268 |
|
---|
3269 | /* ASSUMES: That a preemption test detects HM contexts. (Will work on
|
---|
3270 | non-preemptive systems as well.) */
|
---|
3271 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
3272 | return true;
|
---|
3273 | return false;
|
---|
3274 | }
|
---|
3275 |
|
---|
3276 |
|
---|
3277 | /**
|
---|
3278 | * Checks if the callers context is okay for doing a broadcast given the
|
---|
3279 | * specified source.
|
---|
3280 | *
|
---|
3281 | * @returns true if it's okay, false if it isn't.
|
---|
3282 | * @param pNetwork The network.
|
---|
3283 | * @param fSrc The source of the packet. (0 (intnet),
|
---|
3284 | * INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
|
---|
3285 | */
|
---|
3286 | DECLINLINE(bool) intnetR0NetworkIsContextOkForBroadcast(PINTNETNETWORK pNetwork, uint32_t fSrc)
|
---|
3287 | {
|
---|
3288 | /* Sending to the trunk is the problematic path. If the trunk is the
|
---|
3289 | sender we won't be sending to it, so no problem. */
|
---|
3290 | if (fSrc)
|
---|
3291 | return true;
|
---|
3292 |
|
---|
3293 | /* ASSUMES: That a preemption test detects HM contexts. (Will work on
|
---|
3294 | non-preemptive systems as well.) */
|
---|
3295 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
3296 | return true;
|
---|
3297 |
|
---|
3298 | /* PARANOIA: Grab the spinlock to make sure the trunk structure cannot be
|
---|
3299 | freed while we're touching it. */
|
---|
3300 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
3301 | PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
|
---|
3302 |
|
---|
3303 | bool fRc = !pTrunk
|
---|
3304 | || pTrunk->fNoPreemptDsts == (INTNETTRUNKDIR_HOST | INTNETTRUNKDIR_WIRE)
|
---|
3305 | || ( (!pNetwork->MacTab.fHostActive || (pTrunk->fNoPreemptDsts & INTNETTRUNKDIR_HOST) )
|
---|
3306 | && (!pNetwork->MacTab.fWireActive || (pTrunk->fNoPreemptDsts & INTNETTRUNKDIR_WIRE) ) );
|
---|
3307 |
|
---|
3308 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
3309 |
|
---|
3310 | return fRc;
|
---|
3311 | }
|
---|
3312 |
|
---|
3313 |
|
---|
3314 | /**
|
---|
3315 | * Check context, edit, snoop and switch a broadcast frame when sharing MAC
|
---|
3316 | * address on the wire.
|
---|
3317 | *
|
---|
3318 | * The caller must hold at least one interface on the network busy to prevent it
|
---|
3319 | * from destructing beath us.
|
---|
3320 | *
|
---|
3321 | * @param pNetwork The network the frame is being sent to.
|
---|
3322 | * @param fSrc The source of the packet. (0 (intnet),
|
---|
3323 | * INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
|
---|
3324 | * @param pIfSender The sender interface, NULL if trunk. Used to
|
---|
3325 | * prevent sending an echo to the sender.
|
---|
3326 | * @param pSG Pointer to the gather list.
|
---|
3327 | * @param pEthHdr Pointer to the ethernet header.
|
---|
3328 | * @param pDstTab The destination output table.
|
---|
3329 | */
|
---|
3330 | static INTNETSWDECISION intnetR0NetworkSharedMacFixAndSwitchBroadcast(PINTNETNETWORK pNetwork,
|
---|
3331 | uint32_t fSrc, PINTNETIF pIfSender,
|
---|
3332 | PINTNETSG pSG, PRTNETETHERHDR pEthHdr,
|
---|
3333 | PINTNETDSTTAB pDstTab)
|
---|
3334 | {
|
---|
3335 | /*
|
---|
3336 | * Before doing any work here, we need to figure out if we can handle it
|
---|
3337 | * in the current context. The restrictions are solely on the trunk.
|
---|
3338 | *
|
---|
3339 | * Note! Since at least one interface is busy, there won't be any changes
|
---|
3340 | * to the parameters here (unless the trunk changes its capability
|
---|
3341 | * report, which it shouldn't).
|
---|
3342 | */
|
---|
3343 | if (!intnetR0NetworkIsContextOkForBroadcast(pNetwork, fSrc))
|
---|
3344 | return INTNETSWDECISION_BAD_CONTEXT;
|
---|
3345 |
|
---|
3346 | /*
|
---|
3347 | * Check for ICMPv6 Neighbor Advertisements coming from the trunk.
|
---|
3348 | * If we see an advertisement for an IP in our cache, we can safely remove
|
---|
3349 | * it as the IP has probably moved.
|
---|
3350 | */
|
---|
3351 | if ( (fSrc & INTNETTRUNKDIR_WIRE)
|
---|
3352 | && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_IPV6
|
---|
3353 | && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
|
---|
3354 | intnetR0NetworkSnoopNAFromWire(pNetwork, pSG, pEthHdr);
|
---|
3355 |
|
---|
3356 |
|
---|
3357 | /*
|
---|
3358 | * Check for ARP packets from the wire since we'll have to make
|
---|
3359 | * modification to them if we're sharing the MAC address with the host.
|
---|
3360 | */
|
---|
3361 | if ( (fSrc & INTNETTRUNKDIR_WIRE)
|
---|
3362 | && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_ARP
|
---|
3363 | && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
|
---|
3364 | intnetR0NetworkEditArpFromWire(pNetwork, pSG, pEthHdr);
|
---|
3365 |
|
---|
3366 | /*
|
---|
3367 | * Check for DHCP packets from the internal net since we'll have to set
|
---|
3368 | * broadcast flag in DHCP requests if we're sharing the MAC address with
|
---|
3369 | * the host. GSO is not applicable to DHCP traffic.
|
---|
3370 | */
|
---|
3371 | if ( !fSrc
|
---|
3372 | && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_IPV4
|
---|
3373 | && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
|
---|
3374 | intnetR0NetworkEditDhcpFromIntNet(pNetwork, pSG, pEthHdr);
|
---|
3375 |
|
---|
3376 | /*
|
---|
3377 | * Snoop address info from packet originating from the trunk connection.
|
---|
3378 | */
|
---|
3379 | if (fSrc)
|
---|
3380 | {
|
---|
3381 | #ifdef INTNET_WITH_DHCP_SNOOPING
|
---|
3382 | uint16_t EtherType = RT_BE2H_U16(pEthHdr->EtherType);
|
---|
3383 | if ( ( EtherType == RTNET_ETHERTYPE_IPV4 /* for DHCP */
|
---|
3384 | && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
|
---|
3385 | && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID )
|
---|
3386 | || (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4) )
|
---|
3387 | intnetR0TrunkIfSnoopAddr(pNetwork, pSG, EtherType);
|
---|
3388 | #else
|
---|
3389 | if (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4)
|
---|
3390 | intnetR0TrunkIfSnoopArp(pNetwork, pSG);
|
---|
3391 | #endif
|
---|
3392 | }
|
---|
3393 |
|
---|
3394 | /*
|
---|
3395 | * Create the broadcast destination table.
|
---|
3396 | */
|
---|
3397 | return intnetR0NetworkSwitchBroadcast(pNetwork, fSrc, pIfSender, pDstTab);
|
---|
3398 | }
|
---|
3399 |
|
---|
3400 |
|
---|
3401 | /**
|
---|
3402 | * Check context, snoop and switch a unicast frame using the network layer
|
---|
3403 | * address of the link layer one (when sharing MAC address on the wire).
|
---|
3404 | *
|
---|
3405 | * This function is only used for frames coming from the wire (trunk).
|
---|
3406 | *
|
---|
3407 | * @returns true if it's addressed to someone on the network, otherwise false.
|
---|
3408 | * @param pNetwork The network the frame is being sent to.
|
---|
3409 | * @param pSG Pointer to the gather list.
|
---|
3410 | * @param pEthHdr Pointer to the ethernet header.
|
---|
3411 | * @param pDstTab The destination output table.
|
---|
3412 | */
|
---|
3413 | static INTNETSWDECISION intnetR0NetworkSharedMacFixAndSwitchUnicast(PINTNETNETWORK pNetwork, PINTNETSG pSG,
|
---|
3414 | PRTNETETHERHDR pEthHdr, PINTNETDSTTAB pDstTab)
|
---|
3415 | {
|
---|
3416 | /*
|
---|
3417 | * Extract the network address from the packet.
|
---|
3418 | */
|
---|
3419 | RTNETADDRU Addr;
|
---|
3420 | INTNETADDRTYPE enmAddrType;
|
---|
3421 | uint8_t cbAddr;
|
---|
3422 | switch (RT_BE2H_U16(pEthHdr->EtherType))
|
---|
3423 | {
|
---|
3424 | case RTNET_ETHERTYPE_IPV4:
|
---|
3425 | if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_dst), sizeof(Addr.IPv4), &Addr)))
|
---|
3426 | {
|
---|
3427 | Log(("intnetshareduni: failed to read ip_dst! cbTotal=%#x\n", pSG->cbTotal));
|
---|
3428 | return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
|
---|
3429 | }
|
---|
3430 | enmAddrType = kIntNetAddrType_IPv4;
|
---|
3431 | cbAddr = sizeof(Addr.IPv4);
|
---|
3432 | Log6(("intnetshareduni: IPv4 %d.%d.%d.%d\n", Addr.au8[0], Addr.au8[1], Addr.au8[2], Addr.au8[3]));
|
---|
3433 | break;
|
---|
3434 |
|
---|
3435 | case RTNET_ETHERTYPE_IPV6:
|
---|
3436 | if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV6, ip6_dst), sizeof(Addr.IPv6), &Addr)))
|
---|
3437 | {
|
---|
3438 | Log(("intnetshareduni: failed to read ip6_dst! cbTotal=%#x\n", pSG->cbTotal));
|
---|
3439 | return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
|
---|
3440 | }
|
---|
3441 | enmAddrType = kIntNetAddrType_IPv6;
|
---|
3442 | cbAddr = sizeof(Addr.IPv6);
|
---|
3443 | break;
|
---|
3444 | #if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
|
---|
3445 | case RTNET_ETHERTYPE_IPX_1:
|
---|
3446 | case RTNET_ETHERTYPE_IPX_2:
|
---|
3447 | case RTNET_ETHERTYPE_IPX_3:
|
---|
3448 | if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPX, ipx_dstnet), sizeof(Addr.IPX), &Addr)))
|
---|
3449 | {
|
---|
3450 | Log(("intnetshareduni: failed to read ipx_dstnet! cbTotal=%#x\n", pSG->cbTotal));
|
---|
3451 | return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
|
---|
3452 | }
|
---|
3453 | enmAddrType = kIntNetAddrType_IPX;
|
---|
3454 | cbAddr = sizeof(Addr.IPX);
|
---|
3455 | break;
|
---|
3456 | #endif
|
---|
3457 |
|
---|
3458 | /*
|
---|
3459 | * Treat ARP as broadcast (it shouldn't end up here normally,
|
---|
3460 | * so it goes last in the switch).
|
---|
3461 | */
|
---|
3462 | case RTNET_ETHERTYPE_ARP:
|
---|
3463 | Log6(("intnetshareduni: ARP\n"));
|
---|
3464 | /** @todo revisit this broadcasting of unicast ARP frames! */
|
---|
3465 | return intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, INTNETTRUNKDIR_WIRE, NULL, pSG, pEthHdr, pDstTab);
|
---|
3466 |
|
---|
3467 | /*
|
---|
3468 | * Unknown packets are sent to the trunk and any promiscuous interfaces.
|
---|
3469 | */
|
---|
3470 | default:
|
---|
3471 | {
|
---|
3472 | Log6(("intnetshareduni: unknown ethertype=%#x\n", RT_BE2H_U16(pEthHdr->EtherType)));
|
---|
3473 | return intnetR0NetworkSwitchTrunkAndPromisc(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
|
---|
3474 | }
|
---|
3475 | }
|
---|
3476 |
|
---|
3477 | /*
|
---|
3478 | * Do level-3 switching.
|
---|
3479 | */
|
---|
3480 | INTNETSWDECISION enmSwDecision = intnetR0NetworkSwitchLevel3(pNetwork, &pEthHdr->DstMac,
|
---|
3481 | enmAddrType, &Addr, cbAddr,
|
---|
3482 | INTNETTRUNKDIR_WIRE, pDstTab);
|
---|
3483 |
|
---|
3484 | #ifdef INTNET_WITH_DHCP_SNOOPING
|
---|
3485 | /*
|
---|
3486 | * Perform DHCP snooping. GSO is not applicable to DHCP traffic
|
---|
3487 | */
|
---|
3488 | if ( enmAddrType == kIntNetAddrType_IPv4
|
---|
3489 | && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
|
---|
3490 | && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
|
---|
3491 | intnetR0TrunkIfSnoopAddr(pNetwork, pSG, RT_BE2H_U16(pEthHdr->EtherType));
|
---|
3492 | #endif /* INTNET_WITH_DHCP_SNOOPING */
|
---|
3493 |
|
---|
3494 | return enmSwDecision;
|
---|
3495 | }
|
---|
3496 |
|
---|
3497 |
|
---|
3498 | /**
|
---|
3499 | * Release all the interfaces in the destination table when we realize that
|
---|
3500 | * we're in a context where we cannot get the job done.
|
---|
3501 | *
|
---|
3502 | * @param pNetwork The network.
|
---|
3503 | * @param pDstTab The destination table.
|
---|
3504 | */
|
---|
3505 | static void intnetR0NetworkReleaseDstTab(PINTNETNETWORK pNetwork, PINTNETDSTTAB pDstTab)
|
---|
3506 | {
|
---|
3507 | /* The trunk interface. */
|
---|
3508 | if (pDstTab->fTrunkDst)
|
---|
3509 | {
|
---|
3510 | PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
|
---|
3511 | intnetR0BusyDec(pNetwork, &pTrunk->cBusy);
|
---|
3512 | pDstTab->pTrunk = NULL;
|
---|
3513 | pDstTab->fTrunkDst = 0;
|
---|
3514 | }
|
---|
3515 |
|
---|
3516 | /* Regular interfaces. */
|
---|
3517 | uint32_t iIf = pDstTab->cIfs;
|
---|
3518 | while (iIf-- > 0)
|
---|
3519 | {
|
---|
3520 | PINTNETIF pIf = pDstTab->aIfs[iIf].pIf;
|
---|
3521 | intnetR0BusyDecIf(pIf);
|
---|
3522 | pDstTab->aIfs[iIf].pIf = NULL;
|
---|
3523 | }
|
---|
3524 | pDstTab->cIfs = 0;
|
---|
3525 | }
|
---|
3526 |
|
---|
3527 |
|
---|
3528 | /**
|
---|
3529 | * Deliver the frame to the interfaces specified in the destination table.
|
---|
3530 | *
|
---|
3531 | * @param pNetwork The network.
|
---|
3532 | * @param pDstTab The destination table.
|
---|
3533 | * @param pSG The frame to send.
|
---|
3534 | * @param pIfSender The sender interface. NULL if it originated via
|
---|
3535 | * the trunk.
|
---|
3536 | */
|
---|
3537 | static void intnetR0NetworkDeliver(PINTNETNETWORK pNetwork, PINTNETDSTTAB pDstTab, PINTNETSG pSG, PINTNETIF pIfSender)
|
---|
3538 | {
|
---|
3539 | /*
|
---|
3540 | * Do the interfaces first before sending it to the wire and risk having to
|
---|
3541 | * modify it.
|
---|
3542 | */
|
---|
3543 | uint32_t iIf = pDstTab->cIfs;
|
---|
3544 | while (iIf-- > 0)
|
---|
3545 | {
|
---|
3546 | PINTNETIF pIf = pDstTab->aIfs[iIf].pIf;
|
---|
3547 | intnetR0IfSend(pIf, pIfSender, pSG,
|
---|
3548 | pDstTab->aIfs[iIf].fReplaceDstMac ? &pIf->MacAddr: NULL);
|
---|
3549 | intnetR0BusyDecIf(pIf);
|
---|
3550 | pDstTab->aIfs[iIf].pIf = NULL;
|
---|
3551 | }
|
---|
3552 | pDstTab->cIfs = 0;
|
---|
3553 |
|
---|
3554 | /*
|
---|
3555 | * Send to the trunk.
|
---|
3556 | *
|
---|
3557 | * Note! The switching functions will include the trunk even when the frame
|
---|
3558 | * source is the trunk. This is because we need it to figure out
|
---|
3559 | * whether the other half of the trunk should see the frame or not
|
---|
3560 | * and let the caller know.
|
---|
3561 | *
|
---|
3562 | * So, we'll ignore trunk sends here if the frame origin is
|
---|
3563 | * INTNETTRUNKSWPORT::pfnRecv.
|
---|
3564 | */
|
---|
3565 | if (pDstTab->fTrunkDst)
|
---|
3566 | {
|
---|
3567 | PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
|
---|
3568 | if (pIfSender)
|
---|
3569 | intnetR0TrunkIfSend(pTrunk, pNetwork, pIfSender, pDstTab->fTrunkDst, pSG);
|
---|
3570 | intnetR0BusyDec(pNetwork, &pTrunk->cBusy);
|
---|
3571 | pDstTab->pTrunk = NULL;
|
---|
3572 | pDstTab->fTrunkDst = 0;
|
---|
3573 | }
|
---|
3574 | }
|
---|
3575 |
|
---|
3576 |
|
---|
3577 | /**
|
---|
3578 | * Sends a frame.
|
---|
3579 | *
|
---|
3580 | * This function will distribute the frame to the interfaces it is addressed to.
|
---|
3581 | * It will also update the MAC address of the sender.
|
---|
3582 | *
|
---|
3583 | * The caller must own the network mutex.
|
---|
3584 | *
|
---|
3585 | * @returns The switching decision.
|
---|
3586 | * @param pNetwork The network the frame is being sent to.
|
---|
3587 | * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
|
---|
3588 | * @param fSrc The source flags. This 0 if it's not from the trunk.
|
---|
3589 | * @param pSG Pointer to the gather list.
|
---|
3590 | * @param pDstTab The destination table to use.
|
---|
3591 | */
|
---|
3592 | static INTNETSWDECISION intnetR0NetworkSend(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc,
|
---|
3593 | PINTNETSG pSG, PINTNETDSTTAB pDstTab)
|
---|
3594 | {
|
---|
3595 | /*
|
---|
3596 | * Assert reality.
|
---|
3597 | */
|
---|
3598 | AssertPtr(pNetwork);
|
---|
3599 | AssertPtrNull(pIfSender);
|
---|
3600 | Assert(pIfSender ? fSrc == 0 : fSrc != 0);
|
---|
3601 | Assert(!pIfSender || pNetwork == pIfSender->pNetwork);
|
---|
3602 | AssertPtr(pSG);
|
---|
3603 | Assert(pSG->cSegsUsed >= 1);
|
---|
3604 | Assert(pSG->cSegsUsed <= pSG->cSegsAlloc);
|
---|
3605 | if (pSG->cbTotal < sizeof(RTNETETHERHDR))
|
---|
3606 | return INTNETSWDECISION_INVALID;
|
---|
3607 |
|
---|
3608 | /*
|
---|
3609 | * Get the ethernet header (might theoretically involve multiple segments).
|
---|
3610 | */
|
---|
3611 | RTNETETHERHDR EthHdr;
|
---|
3612 | if (pSG->aSegs[0].cb >= sizeof(EthHdr))
|
---|
3613 | EthHdr = *(PCRTNETETHERHDR)pSG->aSegs[0].pv;
|
---|
3614 | else if (!intnetR0SgReadPart(pSG, 0, sizeof(EthHdr), &EthHdr))
|
---|
3615 | return INTNETSWDECISION_INVALID;
|
---|
3616 | if ( (EthHdr.DstMac.au8[0] == 0x08 && EthHdr.DstMac.au8[1] == 0x00 && EthHdr.DstMac.au8[2] == 0x27)
|
---|
3617 | || (EthHdr.SrcMac.au8[0] == 0x08 && EthHdr.SrcMac.au8[1] == 0x00 && EthHdr.SrcMac.au8[2] == 0x27)
|
---|
3618 | || (EthHdr.DstMac.au8[0] == 0x00 && EthHdr.DstMac.au8[1] == 0x16 && EthHdr.DstMac.au8[2] == 0xcb)
|
---|
3619 | || (EthHdr.SrcMac.au8[0] == 0x00 && EthHdr.SrcMac.au8[1] == 0x16 && EthHdr.SrcMac.au8[2] == 0xcb)
|
---|
3620 | || EthHdr.DstMac.au8[0] == 0xff
|
---|
3621 | || EthHdr.SrcMac.au8[0] == 0xff)
|
---|
3622 | Log2(("D=%.6Rhxs S=%.6Rhxs T=%04x f=%x z=%x\n",
|
---|
3623 | &EthHdr.DstMac, &EthHdr.SrcMac, RT_BE2H_U16(EthHdr.EtherType), fSrc, pSG->cbTotal));
|
---|
3624 |
|
---|
3625 | /*
|
---|
3626 | * Learn the MAC address of the sender. No re-learning as the interface
|
---|
3627 | * user will normally tell us the right MAC address.
|
---|
3628 | *
|
---|
3629 | * Note! We don't notify the trunk about these mainly because of the
|
---|
3630 | * problematic contexts we might be called in.
|
---|
3631 | */
|
---|
3632 | if (RT_UNLIKELY( pIfSender
|
---|
3633 | && !pIfSender->fMacSet
|
---|
3634 | && memcmp(&EthHdr.SrcMac, &pIfSender->MacAddr, sizeof(pIfSender->MacAddr))
|
---|
3635 | && !intnetR0IsMacAddrMulticast(&EthHdr.SrcMac)
|
---|
3636 | ))
|
---|
3637 | {
|
---|
3638 | Log2(("IF MAC: %.6Rhxs -> %.6Rhxs\n", &pIfSender->MacAddr, &EthHdr.SrcMac));
|
---|
3639 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
3640 |
|
---|
3641 | PINTNETMACTABENTRY pIfEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIfSender);
|
---|
3642 | if (pIfEntry)
|
---|
3643 | pIfEntry->MacAddr = EthHdr.SrcMac;
|
---|
3644 | pIfSender->MacAddr = EthHdr.SrcMac;
|
---|
3645 |
|
---|
3646 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
3647 | }
|
---|
3648 |
|
---|
3649 | /*
|
---|
3650 | * Deal with MAC address sharing as that may required editing of the
|
---|
3651 | * packets before we dispatch them anywhere.
|
---|
3652 | */
|
---|
3653 | INTNETSWDECISION enmSwDecision;
|
---|
3654 | if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
3655 | {
|
---|
3656 | if (intnetR0IsMacAddrMulticast(&EthHdr.DstMac))
|
---|
3657 | enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, fSrc, pIfSender, pSG, &EthHdr, pDstTab);
|
---|
3658 | else if (fSrc & INTNETTRUNKDIR_WIRE)
|
---|
3659 | {
|
---|
3660 | if (intnetR0NetworkDetectAndFixNdBroadcast(pNetwork, pSG, &EthHdr))
|
---|
3661 | enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, fSrc, pIfSender, pSG, &EthHdr, pDstTab);
|
---|
3662 | else
|
---|
3663 | enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchUnicast(pNetwork, pSG, &EthHdr, pDstTab);
|
---|
3664 | }
|
---|
3665 | else
|
---|
3666 | enmSwDecision = intnetR0NetworkSwitchUnicast(pNetwork, fSrc, pIfSender, &EthHdr.DstMac, pDstTab);
|
---|
3667 | }
|
---|
3668 | else if (intnetR0IsMacAddrMulticast(&EthHdr.DstMac))
|
---|
3669 | enmSwDecision = intnetR0NetworkSwitchBroadcast(pNetwork, fSrc, pIfSender, pDstTab);
|
---|
3670 | else
|
---|
3671 | enmSwDecision = intnetR0NetworkSwitchUnicast(pNetwork, fSrc, pIfSender, &EthHdr.DstMac, pDstTab);
|
---|
3672 |
|
---|
3673 | /*
|
---|
3674 | * Deliver to the destinations if we can.
|
---|
3675 | */
|
---|
3676 | if (enmSwDecision != INTNETSWDECISION_BAD_CONTEXT)
|
---|
3677 | {
|
---|
3678 | if (intnetR0NetworkIsContextOk(pNetwork, pIfSender, pDstTab))
|
---|
3679 | intnetR0NetworkDeliver(pNetwork, pDstTab, pSG, pIfSender);
|
---|
3680 | else
|
---|
3681 | {
|
---|
3682 | intnetR0NetworkReleaseDstTab(pNetwork, pDstTab);
|
---|
3683 | enmSwDecision = INTNETSWDECISION_BAD_CONTEXT;
|
---|
3684 | }
|
---|
3685 | }
|
---|
3686 |
|
---|
3687 | return enmSwDecision;
|
---|
3688 | }
|
---|
3689 |
|
---|
3690 |
|
---|
3691 | /**
|
---|
3692 | * Sends one or more frames.
|
---|
3693 | *
|
---|
3694 | * The function will first the frame which is passed as the optional arguments
|
---|
3695 | * pvFrame and cbFrame. These are optional since it also possible to chain
|
---|
3696 | * together one or more frames in the send buffer which the function will
|
---|
3697 | * process after considering it's arguments.
|
---|
3698 | *
|
---|
3699 | * The caller is responsible for making sure that there are no concurrent calls
|
---|
3700 | * to this method (with the same handle).
|
---|
3701 | *
|
---|
3702 | * @returns VBox status code.
|
---|
3703 | * @param hIf The interface handle.
|
---|
3704 | * @param pSession The caller's session.
|
---|
3705 | */
|
---|
3706 | INTNETR0DECL(int) IntNetR0IfSend(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession)
|
---|
3707 | {
|
---|
3708 | Log5(("IntNetR0IfSend: hIf=%RX32\n", hIf));
|
---|
3709 |
|
---|
3710 | /*
|
---|
3711 | * Validate input and translate the handle.
|
---|
3712 | */
|
---|
3713 | PINTNET pIntNet = g_pIntNet;
|
---|
3714 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
3715 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
3716 |
|
---|
3717 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
3718 | if (!pIf)
|
---|
3719 | return VERR_INVALID_HANDLE;
|
---|
3720 | STAM_REL_PROFILE_START(&pIf->pIntBuf->StatSend1, a);
|
---|
3721 |
|
---|
3722 | /*
|
---|
3723 | * Make sure we've got a network.
|
---|
3724 | */
|
---|
3725 | int rc = VINF_SUCCESS;
|
---|
3726 | intnetR0BusyIncIf(pIf);
|
---|
3727 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
3728 | if (RT_LIKELY(pNetwork))
|
---|
3729 | {
|
---|
3730 | /*
|
---|
3731 | * Grab the destination table.
|
---|
3732 | */
|
---|
3733 | PINTNETDSTTAB pDstTab = ASMAtomicXchgPtrT(&pIf->pDstTab, NULL, PINTNETDSTTAB);
|
---|
3734 | if (RT_LIKELY(pDstTab))
|
---|
3735 | {
|
---|
3736 | /*
|
---|
3737 | * Process the send buffer.
|
---|
3738 | */
|
---|
3739 | INTNETSWDECISION enmSwDecision = INTNETSWDECISION_BROADCAST;
|
---|
3740 | INTNETSG Sg; /** @todo this will have to be changed if we're going to use async sending
|
---|
3741 | * with buffer sharing for some OS or service. Darwin copies everything so
|
---|
3742 | * I won't bother allocating and managing SGs right now. Sorry. */
|
---|
3743 | PINTNETHDR pHdr;
|
---|
3744 | while ((pHdr = IntNetRingGetNextFrameToRead(&pIf->pIntBuf->Send)) != NULL)
|
---|
3745 | {
|
---|
3746 | uint16_t const u16Type = pHdr->u16Type;
|
---|
3747 | if (u16Type == INTNETHDR_TYPE_FRAME)
|
---|
3748 | {
|
---|
3749 | /* Send regular frame. */
|
---|
3750 | void *pvCurFrame = IntNetHdrGetFramePtr(pHdr, pIf->pIntBuf);
|
---|
3751 | IntNetSgInitTemp(&Sg, pvCurFrame, pHdr->cbFrame);
|
---|
3752 | if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
3753 | intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, pHdr->cbFrame, false /*fGso*/, (uint16_t *)&Sg.fFlags);
|
---|
3754 | enmSwDecision = intnetR0NetworkSend(pNetwork, pIf, 0 /*fSrc*/, &Sg, pDstTab);
|
---|
3755 | }
|
---|
3756 | else if (u16Type == INTNETHDR_TYPE_GSO)
|
---|
3757 | {
|
---|
3758 | /* Send GSO frame if sane. */
|
---|
3759 | PPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, pIf->pIntBuf);
|
---|
3760 | uint32_t cbFrame = pHdr->cbFrame - sizeof(*pGso);
|
---|
3761 | if (RT_LIKELY(PDMNetGsoIsValid(pGso, pHdr->cbFrame, cbFrame)))
|
---|
3762 | {
|
---|
3763 | void *pvCurFrame = pGso + 1;
|
---|
3764 | IntNetSgInitTempGso(&Sg, pvCurFrame, cbFrame, pGso);
|
---|
3765 | if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
3766 | intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, cbFrame, true /*fGso*/, (uint16_t *)&Sg.fFlags);
|
---|
3767 | enmSwDecision = intnetR0NetworkSend(pNetwork, pIf, 0 /*fSrc*/, &Sg, pDstTab);
|
---|
3768 | }
|
---|
3769 | else
|
---|
3770 | {
|
---|
3771 | STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatBadFrames); /* ignore */
|
---|
3772 | enmSwDecision = INTNETSWDECISION_DROP;
|
---|
3773 | }
|
---|
3774 | }
|
---|
3775 | /* Unless it's a padding frame, we're getting babble from the producer. */
|
---|
3776 | else
|
---|
3777 | {
|
---|
3778 | if (u16Type != INTNETHDR_TYPE_PADDING)
|
---|
3779 | STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatBadFrames); /* ignore */
|
---|
3780 | enmSwDecision = INTNETSWDECISION_DROP;
|
---|
3781 | }
|
---|
3782 | if (enmSwDecision == INTNETSWDECISION_BAD_CONTEXT)
|
---|
3783 | {
|
---|
3784 | rc = VERR_TRY_AGAIN;
|
---|
3785 | break;
|
---|
3786 | }
|
---|
3787 |
|
---|
3788 | /* Skip to the next frame. */
|
---|
3789 | IntNetRingSkipFrame(&pIf->pIntBuf->Send);
|
---|
3790 | }
|
---|
3791 |
|
---|
3792 | /*
|
---|
3793 | * Put back the destination table.
|
---|
3794 | */
|
---|
3795 | Assert(!pIf->pDstTab);
|
---|
3796 | ASMAtomicWritePtr(&pIf->pDstTab, pDstTab);
|
---|
3797 | }
|
---|
3798 | else
|
---|
3799 | rc = VERR_INTERNAL_ERROR_4;
|
---|
3800 | }
|
---|
3801 | else
|
---|
3802 | rc = VERR_INTERNAL_ERROR_3;
|
---|
3803 |
|
---|
3804 | /*
|
---|
3805 | * Release the interface.
|
---|
3806 | */
|
---|
3807 | intnetR0BusyDecIf(pIf);
|
---|
3808 | STAM_REL_PROFILE_STOP(&pIf->pIntBuf->StatSend1, a);
|
---|
3809 | intnetR0IfRelease(pIf, pSession);
|
---|
3810 | return rc;
|
---|
3811 | }
|
---|
3812 |
|
---|
3813 |
|
---|
3814 | /**
|
---|
3815 | * VMMR0 request wrapper for IntNetR0IfSend.
|
---|
3816 | *
|
---|
3817 | * @returns see IntNetR0IfSend.
|
---|
3818 | * @param pSession The caller's session.
|
---|
3819 | * @param pReq The request packet.
|
---|
3820 | */
|
---|
3821 | INTNETR0DECL(int) IntNetR0IfSendReq(PSUPDRVSESSION pSession, PINTNETIFSENDREQ pReq)
|
---|
3822 | {
|
---|
3823 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
3824 | return VERR_INVALID_PARAMETER;
|
---|
3825 | return IntNetR0IfSend(pReq->hIf, pSession);
|
---|
3826 | }
|
---|
3827 |
|
---|
3828 |
|
---|
3829 | /**
|
---|
3830 | * Maps the default buffer into ring 3.
|
---|
3831 | *
|
---|
3832 | * @returns VBox status code.
|
---|
3833 | * @param hIf The interface handle.
|
---|
3834 | * @param pSession The caller's session.
|
---|
3835 | * @param ppRing3Buf Where to store the address of the ring-3 mapping
|
---|
3836 | * (optional).
|
---|
3837 | * @param ppRing0Buf Where to store the address of the ring-0 mapping
|
---|
3838 | * (optional).
|
---|
3839 | */
|
---|
3840 | INTNETR0DECL(int) IntNetR0IfGetBufferPtrs(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession,
|
---|
3841 | R3PTRTYPE(PINTNETBUF) *ppRing3Buf, R0PTRTYPE(PINTNETBUF) *ppRing0Buf)
|
---|
3842 | {
|
---|
3843 | LogFlow(("IntNetR0IfGetBufferPtrs: hIf=%RX32 ppRing3Buf=%p ppRing0Buf=%p\n", hIf, ppRing3Buf, ppRing0Buf));
|
---|
3844 |
|
---|
3845 | /*
|
---|
3846 | * Validate input.
|
---|
3847 | */
|
---|
3848 | PINTNET pIntNet = g_pIntNet;
|
---|
3849 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
3850 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
3851 |
|
---|
3852 | AssertPtrNullReturn(ppRing3Buf, VERR_INVALID_PARAMETER);
|
---|
3853 | AssertPtrNullReturn(ppRing0Buf, VERR_INVALID_PARAMETER);
|
---|
3854 | if (ppRing3Buf)
|
---|
3855 | *ppRing3Buf = 0;
|
---|
3856 | if (ppRing0Buf)
|
---|
3857 | *ppRing0Buf = 0;
|
---|
3858 |
|
---|
3859 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
3860 | if (!pIf)
|
---|
3861 | return VERR_INVALID_HANDLE;
|
---|
3862 |
|
---|
3863 | /*
|
---|
3864 | * ASSUMES that only the process that created an interface can use it.
|
---|
3865 | * ASSUMES that we created the ring-3 mapping when selecting or
|
---|
3866 | * allocating the buffer.
|
---|
3867 | */
|
---|
3868 | int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
3869 | if (RT_SUCCESS(rc))
|
---|
3870 | {
|
---|
3871 | if (ppRing3Buf)
|
---|
3872 | *ppRing3Buf = pIf->pIntBufR3;
|
---|
3873 | if (ppRing0Buf)
|
---|
3874 | *ppRing0Buf = (R0PTRTYPE(PINTNETBUF))pIf->pIntBuf; /* tstIntNetR0 mess */
|
---|
3875 |
|
---|
3876 | rc = RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
3877 | }
|
---|
3878 |
|
---|
3879 | intnetR0IfRelease(pIf, pSession);
|
---|
3880 | LogFlow(("IntNetR0IfGetBufferPtrs: returns %Rrc *ppRing3Buf=%p *ppRing0Buf=%p\n",
|
---|
3881 | rc, ppRing3Buf ? *ppRing3Buf : NIL_RTR3PTR, ppRing0Buf ? *ppRing0Buf : NIL_RTR0PTR));
|
---|
3882 | return rc;
|
---|
3883 | }
|
---|
3884 |
|
---|
3885 |
|
---|
3886 | /**
|
---|
3887 | * VMMR0 request wrapper for IntNetR0IfGetBufferPtrs.
|
---|
3888 | *
|
---|
3889 | * @returns see IntNetR0IfGetRing3Buffer.
|
---|
3890 | * @param pSession The caller's session.
|
---|
3891 | * @param pReq The request packet.
|
---|
3892 | */
|
---|
3893 | INTNETR0DECL(int) IntNetR0IfGetBufferPtrsReq(PSUPDRVSESSION pSession, PINTNETIFGETBUFFERPTRSREQ pReq)
|
---|
3894 | {
|
---|
3895 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
3896 | return VERR_INVALID_PARAMETER;
|
---|
3897 | return IntNetR0IfGetBufferPtrs(pReq->hIf, pSession, &pReq->pRing3Buf, &pReq->pRing0Buf);
|
---|
3898 | }
|
---|
3899 |
|
---|
3900 |
|
---|
3901 | #if 0
|
---|
3902 | /**
|
---|
3903 | * Gets the physical addresses of the default interface buffer.
|
---|
3904 | *
|
---|
3905 | * @returns VBox status code.
|
---|
3906 | * @param hIF The interface handle.
|
---|
3907 | * @param paPages Where to store the addresses. (The reserved fields will be set to zero.)
|
---|
3908 | * @param cPages
|
---|
3909 | */
|
---|
3910 | INTNETR0DECL(int) IntNetR0IfGetPhysBuffer(INTNETIFHANDLE hIf, PSUPPAGE paPages, unsigned cPages)
|
---|
3911 | {
|
---|
3912 | /*
|
---|
3913 | * Validate input.
|
---|
3914 | */
|
---|
3915 | PINTNET pIntNet = g_pIntNet;
|
---|
3916 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
3917 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
3918 |
|
---|
3919 | AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
|
---|
3920 | AssertPtrReturn((uint8_t *)&paPages[cPages] - 1, VERR_INVALID_PARAMETER);
|
---|
3921 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
3922 | if (!pIf)
|
---|
3923 | return VERR_INVALID_HANDLE;
|
---|
3924 |
|
---|
3925 | /*
|
---|
3926 | * Grab the lock and get the data.
|
---|
3927 | * ASSUMES that the handle isn't closed while we're here.
|
---|
3928 | */
|
---|
3929 | int rc = RTSemFastMutexRequest(pIf->pNetwork->FastMutex);
|
---|
3930 | if (RT_SUCCESS(rc))
|
---|
3931 | {
|
---|
3932 | /** @todo make a SUPR0 api for obtaining the array. SUPR0/IPRT is keeping track of everything, there
|
---|
3933 | * is no need for any extra bookkeeping here.. */
|
---|
3934 |
|
---|
3935 | rc = RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
|
---|
3936 | }
|
---|
3937 | intnetR0IfRelease(pIf, pSession);
|
---|
3938 | return VERR_NOT_IMPLEMENTED;
|
---|
3939 | }
|
---|
3940 | #endif
|
---|
3941 |
|
---|
3942 |
|
---|
3943 | /**
|
---|
3944 | * Sets the promiscuous mode property of an interface.
|
---|
3945 | *
|
---|
3946 | * @returns VBox status code.
|
---|
3947 | * @param hIf The interface handle.
|
---|
3948 | * @param pSession The caller's session.
|
---|
3949 | * @param fPromiscuous Set if the interface should be in promiscuous mode, clear if not.
|
---|
3950 | */
|
---|
3951 | INTNETR0DECL(int) IntNetR0IfSetPromiscuousMode(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fPromiscuous)
|
---|
3952 | {
|
---|
3953 | LogFlow(("IntNetR0IfSetPromiscuousMode: hIf=%RX32 fPromiscuous=%d\n", hIf, fPromiscuous));
|
---|
3954 |
|
---|
3955 | /*
|
---|
3956 | * Validate & translate input.
|
---|
3957 | */
|
---|
3958 | PINTNET pIntNet = g_pIntNet;
|
---|
3959 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
3960 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
3961 |
|
---|
3962 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
3963 | if (!pIf)
|
---|
3964 | {
|
---|
3965 | Log(("IntNetR0IfSetPromiscuousMode: returns VERR_INVALID_HANDLE\n"));
|
---|
3966 | return VERR_INVALID_HANDLE;
|
---|
3967 | }
|
---|
3968 |
|
---|
3969 | /*
|
---|
3970 | * Get the network, take the address spinlock, and make the change.
|
---|
3971 | * Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
|
---|
3972 | */
|
---|
3973 | int rc = VINF_SUCCESS;
|
---|
3974 | intnetR0BusyIncIf(pIf);
|
---|
3975 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
3976 | if (pNetwork)
|
---|
3977 | {
|
---|
3978 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
3979 |
|
---|
3980 | if (pIf->fPromiscuousReal != fPromiscuous)
|
---|
3981 | {
|
---|
3982 | const bool fPromiscuousEff = fPromiscuous
|
---|
3983 | && (pIf->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW)
|
---|
3984 | && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS);
|
---|
3985 | Log(("IntNetR0IfSetPromiscuousMode: hIf=%RX32: Changed from %d -> %d (%d)\n",
|
---|
3986 | hIf, !fPromiscuous, !!fPromiscuous, fPromiscuousEff));
|
---|
3987 |
|
---|
3988 | pIf->fPromiscuousReal = fPromiscuous;
|
---|
3989 |
|
---|
3990 | PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
|
---|
3991 | if (RT_LIKELY(pEntry))
|
---|
3992 | {
|
---|
3993 | if (pEntry->fPromiscuousEff)
|
---|
3994 | {
|
---|
3995 | pNetwork->MacTab.cPromiscuousEntries--;
|
---|
3996 | if (!pEntry->fPromiscuousSeeTrunk)
|
---|
3997 | pNetwork->MacTab.cPromiscuousNoTrunkEntries--;
|
---|
3998 | Assert(pNetwork->MacTab.cPromiscuousEntries < pNetwork->MacTab.cEntries);
|
---|
3999 | Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries < pNetwork->MacTab.cEntries);
|
---|
4000 | }
|
---|
4001 |
|
---|
4002 | pEntry->fPromiscuousEff = fPromiscuousEff;
|
---|
4003 | pEntry->fPromiscuousSeeTrunk = fPromiscuousEff
|
---|
4004 | && (pIf->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK);
|
---|
4005 |
|
---|
4006 | if (pEntry->fPromiscuousEff)
|
---|
4007 | {
|
---|
4008 | pNetwork->MacTab.cPromiscuousEntries++;
|
---|
4009 | if (!pEntry->fPromiscuousSeeTrunk)
|
---|
4010 | pNetwork->MacTab.cPromiscuousNoTrunkEntries++;
|
---|
4011 | }
|
---|
4012 | Assert(pNetwork->MacTab.cPromiscuousEntries <= pNetwork->MacTab.cEntries);
|
---|
4013 | Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries <= pNetwork->MacTab.cEntries);
|
---|
4014 | }
|
---|
4015 | }
|
---|
4016 |
|
---|
4017 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4018 | }
|
---|
4019 | else
|
---|
4020 | rc = VERR_WRONG_ORDER;
|
---|
4021 |
|
---|
4022 | intnetR0BusyDecIf(pIf);
|
---|
4023 | intnetR0IfRelease(pIf, pSession);
|
---|
4024 | return rc;
|
---|
4025 | }
|
---|
4026 |
|
---|
4027 |
|
---|
4028 | /**
|
---|
4029 | * VMMR0 request wrapper for IntNetR0IfSetPromiscuousMode.
|
---|
4030 | *
|
---|
4031 | * @returns see IntNetR0IfSetPromiscuousMode.
|
---|
4032 | * @param pSession The caller's session.
|
---|
4033 | * @param pReq The request packet.
|
---|
4034 | */
|
---|
4035 | INTNETR0DECL(int) IntNetR0IfSetPromiscuousModeReq(PSUPDRVSESSION pSession, PINTNETIFSETPROMISCUOUSMODEREQ pReq)
|
---|
4036 | {
|
---|
4037 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4038 | return VERR_INVALID_PARAMETER;
|
---|
4039 | return IntNetR0IfSetPromiscuousMode(pReq->hIf, pSession, pReq->fPromiscuous);
|
---|
4040 | }
|
---|
4041 |
|
---|
4042 |
|
---|
4043 | /**
|
---|
4044 | * Sets the MAC address of an interface.
|
---|
4045 | *
|
---|
4046 | * @returns VBox status code.
|
---|
4047 | * @param hIf The interface handle.
|
---|
4048 | * @param pSession The caller's session.
|
---|
4049 | * @param pMAC The new MAC address.
|
---|
4050 | */
|
---|
4051 | INTNETR0DECL(int) IntNetR0IfSetMacAddress(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PCRTMAC pMac)
|
---|
4052 | {
|
---|
4053 | LogFlow(("IntNetR0IfSetMacAddress: hIf=%RX32 pMac=%p:{%.6Rhxs}\n", hIf, pMac, pMac));
|
---|
4054 |
|
---|
4055 | /*
|
---|
4056 | * Validate & translate input.
|
---|
4057 | */
|
---|
4058 | PINTNET pIntNet = g_pIntNet;
|
---|
4059 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
4060 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
4061 |
|
---|
4062 | AssertPtrReturn(pMac, VERR_INVALID_PARAMETER);
|
---|
4063 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
4064 | if (!pIf)
|
---|
4065 | {
|
---|
4066 | Log(("IntNetR0IfSetMacAddress: returns VERR_INVALID_HANDLE\n"));
|
---|
4067 | return VERR_INVALID_HANDLE;
|
---|
4068 | }
|
---|
4069 |
|
---|
4070 | /*
|
---|
4071 | * Get the network, take the address spinlock, and make the change.
|
---|
4072 | * Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
|
---|
4073 | */
|
---|
4074 | int rc = VINF_SUCCESS;
|
---|
4075 | intnetR0BusyIncIf(pIf);
|
---|
4076 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
4077 | if (pNetwork)
|
---|
4078 | {
|
---|
4079 | PINTNETTRUNKIF pTrunk = NULL;
|
---|
4080 |
|
---|
4081 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4082 |
|
---|
4083 | if (memcmp(&pIf->MacAddr, pMac, sizeof(pIf->MacAddr)))
|
---|
4084 | {
|
---|
4085 | Log(("IntNetR0IfSetMacAddress: hIf=%RX32: Changed from %.6Rhxs -> %.6Rhxs\n",
|
---|
4086 | hIf, &pIf->MacAddr, pMac));
|
---|
4087 |
|
---|
4088 | /* Update the two copies. */
|
---|
4089 | PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
|
---|
4090 | if (RT_LIKELY(pEntry))
|
---|
4091 | pEntry->MacAddr = *pMac;
|
---|
4092 | pIf->MacAddr = *pMac;
|
---|
4093 | pIf->fMacSet = true;
|
---|
4094 |
|
---|
4095 | /* Grab a busy reference to the trunk so we release the lock before notifying it. */
|
---|
4096 | pTrunk = pNetwork->MacTab.pTrunk;
|
---|
4097 | if (pTrunk)
|
---|
4098 | intnetR0BusyIncTrunk(pTrunk);
|
---|
4099 | }
|
---|
4100 |
|
---|
4101 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4102 |
|
---|
4103 | if (pTrunk)
|
---|
4104 | {
|
---|
4105 | Log(("IntNetR0IfSetMacAddress: pfnNotifyMacAddress hIf=%RX32\n", hIf));
|
---|
4106 | PINTNETTRUNKIFPORT pIfPort = pTrunk->pIfPort;
|
---|
4107 | if (pIfPort)
|
---|
4108 | pIfPort->pfnNotifyMacAddress(pIfPort, pIf->pvIfData, pMac);
|
---|
4109 | intnetR0BusyDecTrunk(pTrunk);
|
---|
4110 | }
|
---|
4111 | }
|
---|
4112 | else
|
---|
4113 | rc = VERR_WRONG_ORDER;
|
---|
4114 |
|
---|
4115 | intnetR0BusyDecIf(pIf);
|
---|
4116 | intnetR0IfRelease(pIf, pSession);
|
---|
4117 | return rc;
|
---|
4118 | }
|
---|
4119 |
|
---|
4120 |
|
---|
4121 | /**
|
---|
4122 | * VMMR0 request wrapper for IntNetR0IfSetMacAddress.
|
---|
4123 | *
|
---|
4124 | * @returns see IntNetR0IfSetMacAddress.
|
---|
4125 | * @param pSession The caller's session.
|
---|
4126 | * @param pReq The request packet.
|
---|
4127 | */
|
---|
4128 | INTNETR0DECL(int) IntNetR0IfSetMacAddressReq(PSUPDRVSESSION pSession, PINTNETIFSETMACADDRESSREQ pReq)
|
---|
4129 | {
|
---|
4130 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4131 | return VERR_INVALID_PARAMETER;
|
---|
4132 | return IntNetR0IfSetMacAddress(pReq->hIf, pSession, &pReq->Mac);
|
---|
4133 | }
|
---|
4134 |
|
---|
4135 |
|
---|
4136 | /**
|
---|
4137 | * Worker for intnetR0IfSetActive and intnetR0IfDestruct.
|
---|
4138 | *
|
---|
4139 | * This function will update the active interface count on the network and
|
---|
4140 | * activate or deactivate the trunk connection if necessary.
|
---|
4141 | *
|
---|
4142 | * The call must own the giant lock (we cannot take it here).
|
---|
4143 | *
|
---|
4144 | * @returns VBox status code.
|
---|
4145 | * @param pNetwork The network.
|
---|
4146 | * @param fIf The interface.
|
---|
4147 | * @param fActive What to do.
|
---|
4148 | */
|
---|
4149 | static int intnetR0NetworkSetIfActive(PINTNETNETWORK pNetwork, PINTNETIF pIf, bool fActive)
|
---|
4150 | {
|
---|
4151 | /* quick sanity check */
|
---|
4152 | AssertPtr(pNetwork);
|
---|
4153 | AssertPtr(pIf);
|
---|
4154 |
|
---|
4155 | /*
|
---|
4156 | * The address spinlock of the network protects the variables, while the
|
---|
4157 | * big lock protects the calling of pfnSetState. Grab both lock at once
|
---|
4158 | * to save us the extra hassle.
|
---|
4159 | */
|
---|
4160 | PINTNETTRUNKIF pTrunk = NULL;
|
---|
4161 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4162 |
|
---|
4163 | /*
|
---|
4164 | * Do the update.
|
---|
4165 | */
|
---|
4166 | if (pIf->fActive != fActive)
|
---|
4167 | {
|
---|
4168 | PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
|
---|
4169 | if (RT_LIKELY(pEntry))
|
---|
4170 | {
|
---|
4171 | pEntry->fActive = fActive;
|
---|
4172 | pIf->fActive = fActive;
|
---|
4173 |
|
---|
4174 | if (fActive)
|
---|
4175 | {
|
---|
4176 | pNetwork->cActiveIFs++;
|
---|
4177 | if (pNetwork->cActiveIFs == 1)
|
---|
4178 | {
|
---|
4179 | pTrunk = pNetwork->MacTab.pTrunk;
|
---|
4180 | if (pTrunk)
|
---|
4181 | {
|
---|
4182 | pNetwork->MacTab.fHostActive = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
|
---|
4183 | pNetwork->MacTab.fWireActive = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED);
|
---|
4184 | }
|
---|
4185 | }
|
---|
4186 | }
|
---|
4187 | else
|
---|
4188 | {
|
---|
4189 | pNetwork->cActiveIFs--;
|
---|
4190 | if (pNetwork->cActiveIFs == 0)
|
---|
4191 | {
|
---|
4192 | pTrunk = pNetwork->MacTab.pTrunk;
|
---|
4193 | pNetwork->MacTab.fHostActive = false;
|
---|
4194 | pNetwork->MacTab.fWireActive = false;
|
---|
4195 | }
|
---|
4196 | }
|
---|
4197 | }
|
---|
4198 | }
|
---|
4199 |
|
---|
4200 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4201 |
|
---|
4202 | /*
|
---|
4203 | * Tell the trunk if necessary.
|
---|
4204 | * The wait for !busy is for the Solaris streams trunk driver (mostly).
|
---|
4205 | */
|
---|
4206 | if (pTrunk && pTrunk->pIfPort)
|
---|
4207 | {
|
---|
4208 | if (!fActive)
|
---|
4209 | intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
|
---|
4210 |
|
---|
4211 | pTrunk->pIfPort->pfnSetState(pTrunk->pIfPort, fActive ? INTNETTRUNKIFSTATE_ACTIVE : INTNETTRUNKIFSTATE_INACTIVE);
|
---|
4212 | }
|
---|
4213 |
|
---|
4214 | return VINF_SUCCESS;
|
---|
4215 | }
|
---|
4216 |
|
---|
4217 |
|
---|
4218 | /**
|
---|
4219 | * Sets the active property of an interface.
|
---|
4220 | *
|
---|
4221 | * @returns VBox status code.
|
---|
4222 | * @param hIf The interface handle.
|
---|
4223 | * @param pSession The caller's session.
|
---|
4224 | * @param fActive The new state.
|
---|
4225 | */
|
---|
4226 | INTNETR0DECL(int) IntNetR0IfSetActive(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fActive)
|
---|
4227 | {
|
---|
4228 | LogFlow(("IntNetR0IfSetActive: hIf=%RX32 fActive=%RTbool\n", hIf, fActive));
|
---|
4229 |
|
---|
4230 | /*
|
---|
4231 | * Validate & translate input.
|
---|
4232 | */
|
---|
4233 | PINTNET pIntNet = g_pIntNet;
|
---|
4234 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
4235 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
4236 |
|
---|
4237 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
4238 | if (!pIf)
|
---|
4239 | {
|
---|
4240 | Log(("IntNetR0IfSetActive: returns VERR_INVALID_HANDLE\n"));
|
---|
4241 | return VERR_INVALID_HANDLE;
|
---|
4242 | }
|
---|
4243 |
|
---|
4244 | /*
|
---|
4245 | * Hand it to the network since it might involve the trunk and things are
|
---|
4246 | * tricky there wrt to locking order.
|
---|
4247 | *
|
---|
4248 | * 1. We take the giant lock here. This makes sure nobody is re-enabling
|
---|
4249 | * the network while we're pausing it and vice versa. This also enables
|
---|
4250 | * us to wait for the network to become idle before telling the trunk.
|
---|
4251 | * (Important on Solaris.)
|
---|
4252 | *
|
---|
4253 | * 2. For paranoid reasons, we grab a busy reference to the calling
|
---|
4254 | * interface. This is totally unnecessary but should hurt (when done
|
---|
4255 | * after grabbing the giant lock).
|
---|
4256 | */
|
---|
4257 | int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
4258 | if (RT_SUCCESS(rc))
|
---|
4259 | {
|
---|
4260 | intnetR0BusyIncIf(pIf);
|
---|
4261 |
|
---|
4262 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
4263 | if (pNetwork)
|
---|
4264 | rc = intnetR0NetworkSetIfActive(pNetwork, pIf, fActive);
|
---|
4265 | else
|
---|
4266 | rc = VERR_WRONG_ORDER;
|
---|
4267 |
|
---|
4268 | intnetR0BusyDecIf(pIf);
|
---|
4269 | RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
4270 | }
|
---|
4271 |
|
---|
4272 | intnetR0IfRelease(pIf, pSession);
|
---|
4273 | LogFlow(("IntNetR0IfSetActive: returns %Rrc\n", rc));
|
---|
4274 | return rc;
|
---|
4275 | }
|
---|
4276 |
|
---|
4277 |
|
---|
4278 | /**
|
---|
4279 | * VMMR0 request wrapper for IntNetR0IfSetActive.
|
---|
4280 | *
|
---|
4281 | * @returns see IntNetR0IfSetActive.
|
---|
4282 | * @param pIntNet The internal networking instance.
|
---|
4283 | * @param pSession The caller's session.
|
---|
4284 | * @param pReq The request packet.
|
---|
4285 | */
|
---|
4286 | INTNETR0DECL(int) IntNetR0IfSetActiveReq(PSUPDRVSESSION pSession, PINTNETIFSETACTIVEREQ pReq)
|
---|
4287 | {
|
---|
4288 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4289 | return VERR_INVALID_PARAMETER;
|
---|
4290 | return IntNetR0IfSetActive(pReq->hIf, pSession, pReq->fActive);
|
---|
4291 | }
|
---|
4292 |
|
---|
4293 |
|
---|
4294 | /**
|
---|
4295 | * Wait for the interface to get signaled.
|
---|
4296 | * The interface will be signaled when is put into the receive buffer.
|
---|
4297 | *
|
---|
4298 | * @returns VBox status code.
|
---|
4299 | * @param hIf The interface handle.
|
---|
4300 | * @param pSession The caller's session.
|
---|
4301 | * @param cMillies Number of milliseconds to wait. RT_INDEFINITE_WAIT should be
|
---|
4302 | * used if indefinite wait is desired.
|
---|
4303 | */
|
---|
4304 | INTNETR0DECL(int) IntNetR0IfWait(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, uint32_t cMillies)
|
---|
4305 | {
|
---|
4306 | Log4(("IntNetR0IfWait: hIf=%RX32 cMillies=%u\n", hIf, cMillies));
|
---|
4307 |
|
---|
4308 | /*
|
---|
4309 | * Get and validate essential handles.
|
---|
4310 | */
|
---|
4311 | PINTNET pIntNet = g_pIntNet;
|
---|
4312 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
4313 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
4314 |
|
---|
4315 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
4316 | if (!pIf)
|
---|
4317 | {
|
---|
4318 | Log(("IntNetR0IfWait: returns VERR_INVALID_HANDLE\n"));
|
---|
4319 | return VERR_INVALID_HANDLE;
|
---|
4320 | }
|
---|
4321 |
|
---|
4322 | const INTNETIFHANDLE hIfSelf = pIf->hIf;
|
---|
4323 | const RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
|
---|
4324 | const bool fDestroying = ASMAtomicReadBool(&pIf->fDestroying);
|
---|
4325 | if ( hIfSelf != hIf /* paranoia */
|
---|
4326 | || hRecvEvent == NIL_RTSEMEVENT
|
---|
4327 | || fDestroying
|
---|
4328 | )
|
---|
4329 | {
|
---|
4330 | Log(("IntNetR0IfWait: returns VERR_SEM_DESTROYED\n"));
|
---|
4331 | return VERR_SEM_DESTROYED;
|
---|
4332 | }
|
---|
4333 |
|
---|
4334 | /*
|
---|
4335 | * It is tempting to check if there is data to be read here,
|
---|
4336 | * but the problem with such an approach is that it will cause
|
---|
4337 | * one unnecessary supervisor->user->supervisor trip. There is
|
---|
4338 | * already a slight risk for such, so no need to increase it.
|
---|
4339 | */
|
---|
4340 |
|
---|
4341 | /*
|
---|
4342 | * Increment the number of waiters before starting the wait.
|
---|
4343 | * Upon wakeup we must assert reality, checking that we're not
|
---|
4344 | * already destroyed or in the process of being destroyed. This
|
---|
4345 | * code must be aligned with the waiting code in intnetR0IfDestruct.
|
---|
4346 | */
|
---|
4347 | ASMAtomicIncU32(&pIf->cSleepers);
|
---|
4348 | int rc = RTSemEventWaitNoResume(hRecvEvent, cMillies);
|
---|
4349 | if (pIf->hRecvEvent == hRecvEvent)
|
---|
4350 | {
|
---|
4351 | ASMAtomicDecU32(&pIf->cSleepers);
|
---|
4352 | if (!pIf->fDestroying)
|
---|
4353 | {
|
---|
4354 | if (intnetR0IfRelease(pIf, pSession))
|
---|
4355 | rc = VERR_SEM_DESTROYED;
|
---|
4356 | }
|
---|
4357 | else
|
---|
4358 | rc = VERR_SEM_DESTROYED;
|
---|
4359 | }
|
---|
4360 | else
|
---|
4361 | rc = VERR_SEM_DESTROYED;
|
---|
4362 | Log4(("IntNetR0IfWait: returns %Rrc\n", rc));
|
---|
4363 | return rc;
|
---|
4364 | }
|
---|
4365 |
|
---|
4366 |
|
---|
4367 | /**
|
---|
4368 | * VMMR0 request wrapper for IntNetR0IfWait.
|
---|
4369 | *
|
---|
4370 | * @returns see IntNetR0IfWait.
|
---|
4371 | * @param pSession The caller's session.
|
---|
4372 | * @param pReq The request packet.
|
---|
4373 | */
|
---|
4374 | INTNETR0DECL(int) IntNetR0IfWaitReq(PSUPDRVSESSION pSession, PINTNETIFWAITREQ pReq)
|
---|
4375 | {
|
---|
4376 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4377 | return VERR_INVALID_PARAMETER;
|
---|
4378 | return IntNetR0IfWait(pReq->hIf, pSession, pReq->cMillies);
|
---|
4379 | }
|
---|
4380 |
|
---|
4381 |
|
---|
4382 | /**
|
---|
4383 | * Wake up any threads waiting on the interface.
|
---|
4384 | *
|
---|
4385 | * @returns VBox status code.
|
---|
4386 | * @param hIf The interface handle.
|
---|
4387 | * @param pSession The caller's session.
|
---|
4388 | * @param fNoMoreWaits When set, no more waits are permitted.
|
---|
4389 | */
|
---|
4390 | INTNETR0DECL(int) IntNetR0IfAbortWait(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fNoMoreWaits)
|
---|
4391 | {
|
---|
4392 | Log4(("IntNetR0IfAbortWait: hIf=%RX32 fNoMoreWaits=%RTbool\n", hIf, fNoMoreWaits));
|
---|
4393 |
|
---|
4394 | /*
|
---|
4395 | * Get and validate essential handles.
|
---|
4396 | */
|
---|
4397 | PINTNET pIntNet = g_pIntNet;
|
---|
4398 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
4399 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
4400 |
|
---|
4401 | PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
4402 | if (!pIf)
|
---|
4403 | {
|
---|
4404 | Log(("IntNetR0IfAbortWait: returns VERR_INVALID_HANDLE\n"));
|
---|
4405 | return VERR_INVALID_HANDLE;
|
---|
4406 | }
|
---|
4407 |
|
---|
4408 | const INTNETIFHANDLE hIfSelf = pIf->hIf;
|
---|
4409 | const RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
|
---|
4410 | const bool fDestroying = ASMAtomicReadBool(&pIf->fDestroying);
|
---|
4411 | if ( hIfSelf != hIf /* paranoia */
|
---|
4412 | || hRecvEvent == NIL_RTSEMEVENT
|
---|
4413 | || fDestroying
|
---|
4414 | )
|
---|
4415 | {
|
---|
4416 | Log(("IntNetR0IfAbortWait: returns VERR_SEM_DESTROYED\n"));
|
---|
4417 | return VERR_SEM_DESTROYED;
|
---|
4418 | }
|
---|
4419 |
|
---|
4420 | /*
|
---|
4421 | * Set fDestroying if requested to do so and then wake up all the sleeping
|
---|
4422 | * threads (usually just one). We leave the semaphore in the signalled
|
---|
4423 | * state so the next caller will return immediately.
|
---|
4424 | */
|
---|
4425 | if (fNoMoreWaits)
|
---|
4426 | ASMAtomicWriteBool(&pIf->fDestroying, true);
|
---|
4427 |
|
---|
4428 | uint32_t cSleepers = ASMAtomicReadU32(&pIf->cSleepers) + 1;
|
---|
4429 | while (cSleepers-- > 0)
|
---|
4430 | {
|
---|
4431 | int rc = RTSemEventSignal(pIf->hRecvEvent);
|
---|
4432 | AssertRC(rc);
|
---|
4433 | }
|
---|
4434 |
|
---|
4435 | Log4(("IntNetR0IfWait: returns %Rrc\n", VINF_SUCCESS));
|
---|
4436 | return VINF_SUCCESS;
|
---|
4437 | }
|
---|
4438 |
|
---|
4439 |
|
---|
4440 | /**
|
---|
4441 | * VMMR0 request wrapper for IntNetR0IfAbortWait.
|
---|
4442 | *
|
---|
4443 | * @returns see IntNetR0IfWait.
|
---|
4444 | * @param pSession The caller's session.
|
---|
4445 | * @param pReq The request packet.
|
---|
4446 | */
|
---|
4447 | INTNETR0DECL(int) IntNetR0IfAbortWaitReq(PSUPDRVSESSION pSession, PINTNETIFABORTWAITREQ pReq)
|
---|
4448 | {
|
---|
4449 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4450 | return VERR_INVALID_PARAMETER;
|
---|
4451 | return IntNetR0IfAbortWait(pReq->hIf, pSession, pReq->fNoMoreWaits);
|
---|
4452 | }
|
---|
4453 |
|
---|
4454 |
|
---|
4455 | /**
|
---|
4456 | * Close an interface.
|
---|
4457 | *
|
---|
4458 | * @returns VBox status code.
|
---|
4459 | * @param pIntNet The instance handle.
|
---|
4460 | * @param hIf The interface handle.
|
---|
4461 | * @param pSession The caller's session.
|
---|
4462 | */
|
---|
4463 | INTNETR0DECL(int) IntNetR0IfClose(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession)
|
---|
4464 | {
|
---|
4465 | LogFlow(("IntNetR0IfClose: hIf=%RX32\n", hIf));
|
---|
4466 |
|
---|
4467 | /*
|
---|
4468 | * Validate and free the handle.
|
---|
4469 | */
|
---|
4470 | PINTNET pIntNet = g_pIntNet;
|
---|
4471 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
4472 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
4473 |
|
---|
4474 | PINTNETIF pIf = (PINTNETIF)RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pSession);
|
---|
4475 | if (!pIf)
|
---|
4476 | return VERR_INVALID_HANDLE;
|
---|
4477 |
|
---|
4478 | /* Mark the handle as freed so intnetR0IfDestruct won't free it again. */
|
---|
4479 | ASMAtomicWriteU32(&pIf->hIf, INTNET_HANDLE_INVALID);
|
---|
4480 |
|
---|
4481 | /*
|
---|
4482 | * Signal the event semaphore to wake up any threads in IntNetR0IfWait
|
---|
4483 | * and give them a moment to get out and release the interface.
|
---|
4484 | */
|
---|
4485 | uint32_t i = pIf->cSleepers;
|
---|
4486 | while (i-- > 0)
|
---|
4487 | {
|
---|
4488 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
4489 | RTThreadYield();
|
---|
4490 | }
|
---|
4491 | RTSemEventSignal(pIf->hRecvEvent);
|
---|
4492 |
|
---|
4493 | /*
|
---|
4494 | * Release the references to the interface object (handle + free lookup).
|
---|
4495 | */
|
---|
4496 | void *pvObj = pIf->pvObj;
|
---|
4497 | intnetR0IfRelease(pIf, pSession); /* (RTHandleTableFreeWithCtx) */
|
---|
4498 |
|
---|
4499 | int rc = SUPR0ObjRelease(pvObj, pSession);
|
---|
4500 | LogFlow(("IntNetR0IfClose: returns %Rrc\n", rc));
|
---|
4501 | return rc;
|
---|
4502 | }
|
---|
4503 |
|
---|
4504 |
|
---|
4505 | /**
|
---|
4506 | * VMMR0 request wrapper for IntNetR0IfCloseReq.
|
---|
4507 | *
|
---|
4508 | * @returns see IntNetR0IfClose.
|
---|
4509 | * @param pSession The caller's session.
|
---|
4510 | * @param pReq The request packet.
|
---|
4511 | */
|
---|
4512 | INTNETR0DECL(int) IntNetR0IfCloseReq(PSUPDRVSESSION pSession, PINTNETIFCLOSEREQ pReq)
|
---|
4513 | {
|
---|
4514 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
4515 | return VERR_INVALID_PARAMETER;
|
---|
4516 | return IntNetR0IfClose(pReq->hIf, pSession);
|
---|
4517 | }
|
---|
4518 |
|
---|
4519 |
|
---|
4520 | /**
|
---|
4521 | * Interface destructor callback.
|
---|
4522 | * This is called for reference counted objectes when the count reaches 0.
|
---|
4523 | *
|
---|
4524 | * @param pvObj The object pointer.
|
---|
4525 | * @param pvUser1 Pointer to the interface.
|
---|
4526 | * @param pvUser2 Pointer to the INTNET instance data.
|
---|
4527 | */
|
---|
4528 | static DECLCALLBACK(void) intnetR0IfDestruct(void *pvObj, void *pvUser1, void *pvUser2)
|
---|
4529 | {
|
---|
4530 | PINTNETIF pIf = (PINTNETIF)pvUser1;
|
---|
4531 | PINTNET pIntNet = (PINTNET)pvUser2;
|
---|
4532 | Log(("intnetR0IfDestruct: pvObj=%p pIf=%p pIntNet=%p hIf=%RX32\n", pvObj, pIf, pIntNet, pIf->hIf));
|
---|
4533 |
|
---|
4534 | /*
|
---|
4535 | * We grab the INTNET create/open/destroy semaphore to make sure nobody is
|
---|
4536 | * adding or removing interface while we're in here. For paranoid reasons
|
---|
4537 | * we also mark the interface as destroyed here so any waiting threads can
|
---|
4538 | * take evasive action (theoretical case).
|
---|
4539 | */
|
---|
4540 | RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
4541 | ASMAtomicWriteBool(&pIf->fDestroying, true);
|
---|
4542 |
|
---|
4543 | /*
|
---|
4544 | * Delete the interface handle so the object no longer can be used.
|
---|
4545 | * (Can happen if the client didn't close its session.)
|
---|
4546 | */
|
---|
4547 | INTNETIFHANDLE hIf = ASMAtomicXchgU32(&pIf->hIf, INTNET_HANDLE_INVALID);
|
---|
4548 | if (hIf != INTNET_HANDLE_INVALID)
|
---|
4549 | {
|
---|
4550 | void *pvObj2 = RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pIf->pSession); NOREF(pvObj2);
|
---|
4551 | AssertMsg(pvObj2 == pIf, ("%p, %p, hIf=%RX32 pSession=%p\n", pvObj2, pIf, hIf, pIf->pSession));
|
---|
4552 | }
|
---|
4553 |
|
---|
4554 | /*
|
---|
4555 | * If we've got a network deactivate and detach ourselves from it. Because
|
---|
4556 | * of cleanup order we might have been orphaned by the network destructor.
|
---|
4557 | */
|
---|
4558 | PINTNETNETWORK pNetwork = pIf->pNetwork;
|
---|
4559 | if (pNetwork)
|
---|
4560 | {
|
---|
4561 | /* set inactive. */
|
---|
4562 | intnetR0NetworkSetIfActive(pNetwork, pIf, false /*fActive*/);
|
---|
4563 |
|
---|
4564 | /* remove ourselves from the switch table. */
|
---|
4565 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4566 |
|
---|
4567 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
4568 | while (iIf-- > 0)
|
---|
4569 | if (pNetwork->MacTab.paEntries[iIf].pIf == pIf)
|
---|
4570 | {
|
---|
4571 | if (pNetwork->MacTab.paEntries[iIf].fPromiscuousEff)
|
---|
4572 | {
|
---|
4573 | pNetwork->MacTab.cPromiscuousEntries--;
|
---|
4574 | if (!pNetwork->MacTab.paEntries[iIf].fPromiscuousSeeTrunk)
|
---|
4575 | pNetwork->MacTab.cPromiscuousNoTrunkEntries--;
|
---|
4576 | }
|
---|
4577 | Assert(pNetwork->MacTab.cPromiscuousEntries < pNetwork->MacTab.cEntries);
|
---|
4578 | Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries < pNetwork->MacTab.cEntries);
|
---|
4579 |
|
---|
4580 | if (iIf + 1 < pNetwork->MacTab.cEntries)
|
---|
4581 | memmove(&pNetwork->MacTab.paEntries[iIf],
|
---|
4582 | &pNetwork->MacTab.paEntries[iIf + 1],
|
---|
4583 | (pNetwork->MacTab.cEntries - iIf - 1) * sizeof(pNetwork->MacTab.paEntries[0]));
|
---|
4584 | pNetwork->MacTab.cEntries--;
|
---|
4585 | break;
|
---|
4586 | }
|
---|
4587 |
|
---|
4588 | /* recalc the min flags. */
|
---|
4589 | if (pIf->fOpenFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
|
---|
4590 | {
|
---|
4591 | uint32_t fMinFlags = 0;
|
---|
4592 | iIf = pNetwork->MacTab.cEntries;
|
---|
4593 | while (iIf-- > 0)
|
---|
4594 | {
|
---|
4595 | PINTNETIF pIf2 = pNetwork->MacTab.paEntries[iIf].pIf;
|
---|
4596 | if ( pIf2 /* paranoia */
|
---|
4597 | && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES))
|
---|
4598 | fMinFlags |= pIf2->fOpenFlags & INTNET_OPEN_FLAGS_STRICT_MASK;
|
---|
4599 | }
|
---|
4600 | pNetwork->fMinFlags = fMinFlags;
|
---|
4601 | }
|
---|
4602 |
|
---|
4603 | PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
|
---|
4604 |
|
---|
4605 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4606 |
|
---|
4607 | /* Notify the trunk about the interface being destroyed. */
|
---|
4608 | if (pTrunk && pTrunk->pIfPort)
|
---|
4609 | pTrunk->pIfPort->pfnDisconnectInterface(pTrunk->pIfPort, pIf->pvIfData);
|
---|
4610 |
|
---|
4611 | /* Wait for the interface to quiesce while we still can. */
|
---|
4612 | intnetR0BusyWait(pNetwork, &pIf->cBusy);
|
---|
4613 |
|
---|
4614 | /* Release our reference to the network. */
|
---|
4615 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4616 | pIf->pNetwork = NULL;
|
---|
4617 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4618 |
|
---|
4619 | SUPR0ObjRelease(pNetwork->pvObj, pIf->pSession);
|
---|
4620 | }
|
---|
4621 |
|
---|
4622 | RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
4623 |
|
---|
4624 | /*
|
---|
4625 | * Wakeup anyone waiting on this interface.
|
---|
4626 | *
|
---|
4627 | * We *must* make sure they have woken up properly and realized
|
---|
4628 | * that the interface is no longer valid.
|
---|
4629 | */
|
---|
4630 | if (pIf->hRecvEvent != NIL_RTSEMEVENT)
|
---|
4631 | {
|
---|
4632 | RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
|
---|
4633 | unsigned cMaxWait = 0x1000;
|
---|
4634 | while (pIf->cSleepers && cMaxWait-- > 0)
|
---|
4635 | {
|
---|
4636 | RTSemEventSignal(hRecvEvent);
|
---|
4637 | RTThreadYield();
|
---|
4638 | }
|
---|
4639 | if (pIf->cSleepers)
|
---|
4640 | {
|
---|
4641 | RTThreadSleep(1);
|
---|
4642 |
|
---|
4643 | cMaxWait = pIf->cSleepers;
|
---|
4644 | while (pIf->cSleepers && cMaxWait-- > 0)
|
---|
4645 | {
|
---|
4646 | RTSemEventSignal(hRecvEvent);
|
---|
4647 | RTThreadSleep(10);
|
---|
4648 | }
|
---|
4649 | }
|
---|
4650 |
|
---|
4651 | RTSemEventDestroy(hRecvEvent);
|
---|
4652 | pIf->hRecvEvent = NIL_RTSEMEVENT;
|
---|
4653 | }
|
---|
4654 |
|
---|
4655 | /*
|
---|
4656 | * Unmap user buffer.
|
---|
4657 | */
|
---|
4658 | if (pIf->pIntBuf != pIf->pIntBufDefault)
|
---|
4659 | {
|
---|
4660 | /** @todo user buffer */
|
---|
4661 | }
|
---|
4662 |
|
---|
4663 | /*
|
---|
4664 | * Unmap and Free the default buffer.
|
---|
4665 | */
|
---|
4666 | if (pIf->pIntBufDefault)
|
---|
4667 | {
|
---|
4668 | SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
|
---|
4669 | pIf->pIntBufDefault = NULL;
|
---|
4670 | pIf->pIntBufDefaultR3 = 0;
|
---|
4671 | pIf->pIntBuf = NULL;
|
---|
4672 | pIf->pIntBufR3 = 0;
|
---|
4673 | }
|
---|
4674 |
|
---|
4675 | /*
|
---|
4676 | * Free remaining resources
|
---|
4677 | */
|
---|
4678 | RTSpinlockDestroy(pIf->hRecvInSpinlock);
|
---|
4679 | pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
|
---|
4680 |
|
---|
4681 | RTMemFree(pIf->pDstTab);
|
---|
4682 | pIf->pDstTab = NULL;
|
---|
4683 |
|
---|
4684 | for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End; i++)
|
---|
4685 | intnetR0IfAddrCacheDestroy(&pIf->aAddrCache[i]);
|
---|
4686 |
|
---|
4687 | pIf->pvObj = NULL;
|
---|
4688 | RTMemFree(pIf);
|
---|
4689 | }
|
---|
4690 |
|
---|
4691 |
|
---|
4692 | /**
|
---|
4693 | * Creates a new network interface.
|
---|
4694 | *
|
---|
4695 | * The call must have opened the network for the new interface and is
|
---|
4696 | * responsible for closing it on failure. On success it must leave the network
|
---|
4697 | * opened so the interface destructor can close it.
|
---|
4698 | *
|
---|
4699 | * @returns VBox status code.
|
---|
4700 | * @param pNetwork The network, referenced. The reference is consumed on
|
---|
4701 | * success.
|
---|
4702 | * @param pSession The session handle.
|
---|
4703 | * @param cbSend The size of the send buffer.
|
---|
4704 | * @param cbRecv The size of the receive buffer.
|
---|
4705 | * @param fFlags The open network flags.
|
---|
4706 | * @param phIf Where to store the interface handle.
|
---|
4707 | */
|
---|
4708 | static int intnetR0NetworkCreateIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession,
|
---|
4709 | unsigned cbSend, unsigned cbRecv, uint32_t fFlags,
|
---|
4710 | PINTNETIFHANDLE phIf)
|
---|
4711 | {
|
---|
4712 | LogFlow(("intnetR0NetworkCreateIf: pNetwork=%p pSession=%p cbSend=%u cbRecv=%u fFlags=%#x phIf=%p\n",
|
---|
4713 | pNetwork, pSession, cbSend, cbRecv, fFlags, phIf));
|
---|
4714 |
|
---|
4715 | /*
|
---|
4716 | * Assert input.
|
---|
4717 | */
|
---|
4718 | AssertPtr(pNetwork);
|
---|
4719 | AssertPtr(phIf);
|
---|
4720 |
|
---|
4721 | /*
|
---|
4722 | * Adjust the flags with defaults for the interface policies.
|
---|
4723 | * Note: Main restricts promiscuous mode per interface.
|
---|
4724 | */
|
---|
4725 | uint32_t const fDefFlags = INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW
|
---|
4726 | | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK;
|
---|
4727 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkIfFlags); i++)
|
---|
4728 | if (!(fFlags & g_afIntNetOpenNetworkIfFlags[i].fPair))
|
---|
4729 | fFlags |= g_afIntNetOpenNetworkIfFlags[i].fPair & fDefFlags;
|
---|
4730 |
|
---|
4731 | /*
|
---|
4732 | * Make sure that all destination tables as well as the have space of
|
---|
4733 | */
|
---|
4734 | int rc = intnetR0NetworkEnsureTabSpace(pNetwork);
|
---|
4735 | if (RT_FAILURE(rc))
|
---|
4736 | return rc;
|
---|
4737 |
|
---|
4738 | /*
|
---|
4739 | * Allocate the interface and initialize it.
|
---|
4740 | */
|
---|
4741 | PINTNETIF pIf = (PINTNETIF)RTMemAllocZ(sizeof(*pIf));
|
---|
4742 | if (!pIf)
|
---|
4743 | return VERR_NO_MEMORY;
|
---|
4744 |
|
---|
4745 | memset(&pIf->MacAddr, 0xff, sizeof(pIf->MacAddr)); /* broadcast */
|
---|
4746 | //pIf->fMacSet = false;
|
---|
4747 | //pIf->fPromiscuousReal = false;
|
---|
4748 | //pIf->fActive = false;
|
---|
4749 | //pIf->fDestroying = false;
|
---|
4750 | pIf->fOpenFlags = fFlags;
|
---|
4751 | //pIf->cYields = 0;
|
---|
4752 | //pIf->pIntBuf = 0;
|
---|
4753 | //pIf->pIntBufR3 = NIL_RTR3PTR;
|
---|
4754 | //pIf->pIntBufDefault = 0;
|
---|
4755 | //pIf->pIntBufDefaultR3 = NIL_RTR3PTR;
|
---|
4756 | pIf->hRecvEvent = NIL_RTSEMEVENT;
|
---|
4757 | //pIf->cSleepers = 0;
|
---|
4758 | pIf->hIf = INTNET_HANDLE_INVALID;
|
---|
4759 | pIf->pNetwork = pNetwork;
|
---|
4760 | pIf->pSession = pSession;
|
---|
4761 | //pIf->pvObj = NULL;
|
---|
4762 | //pIf->aAddrCache = {0};
|
---|
4763 | pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
|
---|
4764 | pIf->cBusy = 0;
|
---|
4765 | //pIf->pDstTab = NULL;
|
---|
4766 | //pIf->pvIfData = NULL;
|
---|
4767 |
|
---|
4768 | for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End && RT_SUCCESS(rc); i++)
|
---|
4769 | rc = intnetR0IfAddrCacheInit(&pIf->aAddrCache[i], (INTNETADDRTYPE)i,
|
---|
4770 | !!(pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE));
|
---|
4771 | if (RT_SUCCESS(rc))
|
---|
4772 | rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, (PINTNETDSTTAB *)&pIf->pDstTab);
|
---|
4773 | if (RT_SUCCESS(rc))
|
---|
4774 | rc = RTSemEventCreate((PRTSEMEVENT)&pIf->hRecvEvent);
|
---|
4775 | if (RT_SUCCESS(rc))
|
---|
4776 | rc = RTSpinlockCreate(&pIf->hRecvInSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hRecvInSpinlock");
|
---|
4777 | if (RT_SUCCESS(rc))
|
---|
4778 | {
|
---|
4779 | /*
|
---|
4780 | * Create the default buffer.
|
---|
4781 | */
|
---|
4782 | /** @todo adjust with minimums and apply defaults here. */
|
---|
4783 | cbRecv = RT_ALIGN(RT_MAX(cbRecv, sizeof(INTNETHDR) * 4), INTNETRINGBUF_ALIGNMENT);
|
---|
4784 | cbSend = RT_ALIGN(RT_MAX(cbSend, sizeof(INTNETHDR) * 4), INTNETRINGBUF_ALIGNMENT);
|
---|
4785 | const unsigned cbBuf = RT_ALIGN(sizeof(*pIf->pIntBuf), INTNETRINGBUF_ALIGNMENT) + cbRecv + cbSend;
|
---|
4786 | rc = SUPR0MemAlloc(pIf->pSession, cbBuf, (PRTR0PTR)&pIf->pIntBufDefault, (PRTR3PTR)&pIf->pIntBufDefaultR3);
|
---|
4787 | if (RT_SUCCESS(rc))
|
---|
4788 | {
|
---|
4789 | ASMMemZero32(pIf->pIntBufDefault, cbBuf); /** @todo I thought I specified these buggers as clearing the memory... */
|
---|
4790 |
|
---|
4791 | pIf->pIntBuf = pIf->pIntBufDefault;
|
---|
4792 | pIf->pIntBufR3 = pIf->pIntBufDefaultR3;
|
---|
4793 | IntNetBufInit(pIf->pIntBuf, cbBuf, cbRecv, cbSend);
|
---|
4794 |
|
---|
4795 | /*
|
---|
4796 | * Register the interface with the session and create a handle for it.
|
---|
4797 | */
|
---|
4798 | pIf->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
|
---|
4799 | intnetR0IfDestruct, pIf, pNetwork->pIntNet);
|
---|
4800 | if (pIf->pvObj)
|
---|
4801 | {
|
---|
4802 | rc = RTHandleTableAllocWithCtx(pNetwork->pIntNet->hHtIfs, pIf, pSession, (uint32_t *)&pIf->hIf);
|
---|
4803 | if (RT_SUCCESS(rc))
|
---|
4804 | {
|
---|
4805 | /*
|
---|
4806 | * Finally add the interface to the network, consuming the
|
---|
4807 | * network reference of the caller.
|
---|
4808 | */
|
---|
4809 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4810 |
|
---|
4811 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
4812 | Assert(iIf + 1 <= pNetwork->MacTab.cEntriesAllocated);
|
---|
4813 |
|
---|
4814 | pNetwork->MacTab.paEntries[iIf].MacAddr = pIf->MacAddr;
|
---|
4815 | pNetwork->MacTab.paEntries[iIf].fActive = false;
|
---|
4816 | pNetwork->MacTab.paEntries[iIf].fPromiscuousEff = false;
|
---|
4817 | pNetwork->MacTab.paEntries[iIf].fPromiscuousSeeTrunk = false;
|
---|
4818 | pNetwork->MacTab.paEntries[iIf].pIf = pIf;
|
---|
4819 |
|
---|
4820 | pNetwork->MacTab.cEntries = iIf + 1;
|
---|
4821 | pIf->pNetwork = pNetwork;
|
---|
4822 |
|
---|
4823 | /*
|
---|
4824 | * Grab a busy reference (paranoia) to the trunk before releasing
|
---|
4825 | * the spinlock and then notify it about the new interface.
|
---|
4826 | */
|
---|
4827 | PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
|
---|
4828 | if (pTrunk)
|
---|
4829 | intnetR0BusyIncTrunk(pTrunk);
|
---|
4830 |
|
---|
4831 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4832 |
|
---|
4833 | if (pTrunk)
|
---|
4834 | {
|
---|
4835 | Log(("intnetR0NetworkCreateIf: pfnConnectInterface hIf=%RX32\n", pIf->hIf));
|
---|
4836 | if (pTrunk->pIfPort)
|
---|
4837 | rc = pTrunk->pIfPort->pfnConnectInterface(pTrunk->pIfPort, pIf, &pIf->pvIfData);
|
---|
4838 | intnetR0BusyDecTrunk(pTrunk);
|
---|
4839 | }
|
---|
4840 | if (RT_SUCCESS(rc))
|
---|
4841 | {
|
---|
4842 | /*
|
---|
4843 | * We're good!
|
---|
4844 | */
|
---|
4845 | *phIf = pIf->hIf;
|
---|
4846 | Log(("intnetR0NetworkCreateIf: returns VINF_SUCCESS *phIf=%RX32 cbSend=%u cbRecv=%u cbBuf=%u\n",
|
---|
4847 | *phIf, pIf->pIntBufDefault->cbSend, pIf->pIntBufDefault->cbRecv, pIf->pIntBufDefault->cbBuf));
|
---|
4848 | return VINF_SUCCESS;
|
---|
4849 | }
|
---|
4850 | }
|
---|
4851 |
|
---|
4852 | SUPR0ObjRelease(pIf->pvObj, pSession);
|
---|
4853 | LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
|
---|
4854 | return rc;
|
---|
4855 | }
|
---|
4856 |
|
---|
4857 | /* clean up */
|
---|
4858 | SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
|
---|
4859 | pIf->pIntBufDefault = NULL;
|
---|
4860 | pIf->pIntBuf = NULL;
|
---|
4861 | }
|
---|
4862 | }
|
---|
4863 |
|
---|
4864 | RTSpinlockDestroy(pIf->hRecvInSpinlock);
|
---|
4865 | pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
|
---|
4866 | RTSemEventDestroy(pIf->hRecvEvent);
|
---|
4867 | pIf->hRecvEvent = NIL_RTSEMEVENT;
|
---|
4868 | RTMemFree(pIf->pDstTab);
|
---|
4869 | for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End; i++)
|
---|
4870 | intnetR0IfAddrCacheDestroy(&pIf->aAddrCache[i]);
|
---|
4871 | RTMemFree(pIf);
|
---|
4872 | LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
|
---|
4873 | return rc;
|
---|
4874 | }
|
---|
4875 |
|
---|
4876 |
|
---|
4877 | /** @copydoc INTNETTRUNKSWPORT::pfnSetSGPhys */
|
---|
4878 | static DECLCALLBACK(bool) intnetR0TrunkIfPortSetSGPhys(PINTNETTRUNKSWPORT pSwitchPort, bool fEnable)
|
---|
4879 | {
|
---|
4880 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4881 | AssertMsgFailed(("Not implemented because it wasn't required on Darwin\n"));
|
---|
4882 | return ASMAtomicXchgBool(&pThis->fPhysSG, fEnable);
|
---|
4883 | }
|
---|
4884 |
|
---|
4885 |
|
---|
4886 | /** @copydoc INTNETTRUNKSWPORT::pfnReportMacAddress */
|
---|
4887 | static DECLCALLBACK(void) intnetR0TrunkIfPortReportMacAddress(PINTNETTRUNKSWPORT pSwitchPort, PCRTMAC pMacAddr)
|
---|
4888 | {
|
---|
4889 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4890 |
|
---|
4891 | /*
|
---|
4892 | * Get the network instance and grab the address spinlock before making
|
---|
4893 | * any changes.
|
---|
4894 | */
|
---|
4895 | intnetR0BusyIncTrunk(pThis);
|
---|
4896 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
4897 | if (pNetwork)
|
---|
4898 | {
|
---|
4899 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4900 |
|
---|
4901 | pNetwork->MacTab.HostMac = *pMacAddr;
|
---|
4902 | pThis->MacAddr = *pMacAddr;
|
---|
4903 |
|
---|
4904 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4905 | }
|
---|
4906 | else
|
---|
4907 | pThis->MacAddr = *pMacAddr;
|
---|
4908 | intnetR0BusyDecTrunk(pThis);
|
---|
4909 | }
|
---|
4910 |
|
---|
4911 |
|
---|
4912 | /** @copydoc INTNETTRUNKSWPORT::pfnReportPromiscuousMode */
|
---|
4913 | static DECLCALLBACK(void) intnetR0TrunkIfPortReportPromiscuousMode(PINTNETTRUNKSWPORT pSwitchPort, bool fPromiscuous)
|
---|
4914 | {
|
---|
4915 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4916 |
|
---|
4917 | /*
|
---|
4918 | * Get the network instance and grab the address spinlock before making
|
---|
4919 | * any changes.
|
---|
4920 | */
|
---|
4921 | intnetR0BusyIncTrunk(pThis);
|
---|
4922 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
4923 | if (pNetwork)
|
---|
4924 | {
|
---|
4925 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
4926 |
|
---|
4927 | pNetwork->MacTab.fHostPromiscuousReal = fPromiscuous
|
---|
4928 | || (pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE);
|
---|
4929 | pNetwork->MacTab.fHostPromiscuousEff = pNetwork->MacTab.fHostPromiscuousReal
|
---|
4930 | && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
|
---|
4931 |
|
---|
4932 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
4933 | }
|
---|
4934 | intnetR0BusyDecTrunk(pThis);
|
---|
4935 | }
|
---|
4936 |
|
---|
4937 |
|
---|
4938 | /** @copydoc INTNETTRUNKSWPORT::pfnReportGsoCapabilities */
|
---|
4939 | static DECLCALLBACK(void) intnetR0TrunkIfPortReportGsoCapabilities(PINTNETTRUNKSWPORT pSwitchPort,
|
---|
4940 | uint32_t fGsoCapabilities, uint32_t fDst)
|
---|
4941 | {
|
---|
4942 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4943 |
|
---|
4944 | for (unsigned iBit = PDMNETWORKGSOTYPE_END; iBit < 32; iBit++)
|
---|
4945 | Assert(!(fGsoCapabilities & RT_BIT_32(iBit)));
|
---|
4946 | Assert(!(fDst & ~INTNETTRUNKDIR_VALID_MASK));
|
---|
4947 | Assert(fDst);
|
---|
4948 |
|
---|
4949 | if (fDst & INTNETTRUNKDIR_HOST)
|
---|
4950 | pThis->fHostGsoCapabilites = fGsoCapabilities;
|
---|
4951 |
|
---|
4952 | if (fDst & INTNETTRUNKDIR_WIRE)
|
---|
4953 | pThis->fWireGsoCapabilites = fGsoCapabilities;
|
---|
4954 | }
|
---|
4955 |
|
---|
4956 |
|
---|
4957 | /** @copydoc INTNETTRUNKSWPORT::pfnReportNoPreemptDsts */
|
---|
4958 | static DECLCALLBACK(void) intnetR0TrunkIfPortReportNoPreemptDsts(PINTNETTRUNKSWPORT pSwitchPort, uint32_t fNoPreemptDsts)
|
---|
4959 | {
|
---|
4960 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4961 | Assert(!(fNoPreemptDsts & ~INTNETTRUNKDIR_VALID_MASK));
|
---|
4962 |
|
---|
4963 | pThis->fNoPreemptDsts = fNoPreemptDsts;
|
---|
4964 | }
|
---|
4965 |
|
---|
4966 |
|
---|
4967 | /** @copydoc INTNETTRUNKSWPORT::pfnPreRecv */
|
---|
4968 | static DECLCALLBACK(INTNETSWDECISION) intnetR0TrunkIfPortPreRecv(PINTNETTRUNKSWPORT pSwitchPort,
|
---|
4969 | void const *pvSrc, size_t cbSrc, uint32_t fSrc)
|
---|
4970 | {
|
---|
4971 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
4972 |
|
---|
4973 | /* assert some sanity */
|
---|
4974 | AssertPtr(pvSrc);
|
---|
4975 | AssertReturn(cbSrc >= 6, INTNETSWDECISION_BROADCAST);
|
---|
4976 | Assert(fSrc);
|
---|
4977 |
|
---|
4978 | /*
|
---|
4979 | * Mark the trunk as busy, make sure we've got a network and that there are
|
---|
4980 | * some active interfaces around.
|
---|
4981 | */
|
---|
4982 | INTNETSWDECISION enmSwDecision = INTNETSWDECISION_TRUNK;
|
---|
4983 | intnetR0BusyIncTrunk(pThis);
|
---|
4984 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
4985 | if (RT_LIKELY( pNetwork
|
---|
4986 | && pNetwork->cActiveIFs > 0 ))
|
---|
4987 | {
|
---|
4988 | /*
|
---|
4989 | * Lazy bird! No pre-switching of multicast and shared-MAC-on-wire.
|
---|
4990 | */
|
---|
4991 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvSrc;
|
---|
4992 | if (intnetR0IsMacAddrMulticast(&pEthHdr->DstMac))
|
---|
4993 | enmSwDecision = INTNETSWDECISION_BROADCAST;
|
---|
4994 | else if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
4995 | enmSwDecision = INTNETSWDECISION_BROADCAST;
|
---|
4996 | else
|
---|
4997 | enmSwDecision = intnetR0NetworkPreSwitchUnicast(pNetwork,
|
---|
4998 | fSrc,
|
---|
4999 | cbSrc >= 12 ? &pEthHdr->SrcMac : NULL,
|
---|
5000 | &pEthHdr->DstMac);
|
---|
5001 | }
|
---|
5002 |
|
---|
5003 | intnetR0BusyDecTrunk(pThis);
|
---|
5004 | return enmSwDecision;
|
---|
5005 | }
|
---|
5006 |
|
---|
5007 |
|
---|
5008 | /** @copydoc INTNETTRUNKSWPORT::pfnRecv */
|
---|
5009 | static DECLCALLBACK(bool) intnetR0TrunkIfPortRecv(PINTNETTRUNKSWPORT pSwitchPort, void *pvIf, PINTNETSG pSG, uint32_t fSrc)
|
---|
5010 | {
|
---|
5011 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
5012 |
|
---|
5013 | /* assert some sanity */
|
---|
5014 | AssertPtr(pSG);
|
---|
5015 | Assert(fSrc);
|
---|
5016 | NOREF(pvIf); /* later */
|
---|
5017 |
|
---|
5018 | /*
|
---|
5019 | * Mark the trunk as busy, make sure we've got a network and that there are
|
---|
5020 | * some active interfaces around.
|
---|
5021 | */
|
---|
5022 | bool fRc = false /* don't drop it */;
|
---|
5023 | intnetR0BusyIncTrunk(pThis);
|
---|
5024 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
5025 | if (RT_LIKELY( pNetwork
|
---|
5026 | && pNetwork->cActiveIFs > 0 ))
|
---|
5027 | {
|
---|
5028 | /*
|
---|
5029 | * Grab or allocate a destination table.
|
---|
5030 | */
|
---|
5031 | bool const fIntCtx = RTThreadPreemptIsEnabled(NIL_RTTHREAD) || RTThreadIsInInterrupt(NIL_RTTHREAD);
|
---|
5032 | unsigned iDstTab = 0;
|
---|
5033 | PINTNETDSTTAB pDstTab = NULL;
|
---|
5034 | RTSpinlockAcquire(pThis->hDstTabSpinlock);
|
---|
5035 | if (fIntCtx)
|
---|
5036 | {
|
---|
5037 | /* Interrupt or restricted context. */
|
---|
5038 | iDstTab = RTMpCpuIdToSetIndex(RTMpCpuId());
|
---|
5039 | iDstTab %= pThis->cIntDstTabs;
|
---|
5040 | pDstTab = pThis->apIntDstTabs[iDstTab];
|
---|
5041 | if (RT_LIKELY(pDstTab))
|
---|
5042 | pThis->apIntDstTabs[iDstTab] = NULL;
|
---|
5043 | else
|
---|
5044 | {
|
---|
5045 | iDstTab = pThis->cIntDstTabs;
|
---|
5046 | while (iDstTab-- > 0)
|
---|
5047 | {
|
---|
5048 | pDstTab = pThis->apIntDstTabs[iDstTab];
|
---|
5049 | if (pDstTab)
|
---|
5050 | {
|
---|
5051 | pThis->apIntDstTabs[iDstTab] = NULL;
|
---|
5052 | break;
|
---|
5053 | }
|
---|
5054 | }
|
---|
5055 | }
|
---|
5056 | RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
|
---|
5057 | Assert(!pDstTab || iDstTab < pThis->cIntDstTabs);
|
---|
5058 | }
|
---|
5059 | else
|
---|
5060 | {
|
---|
5061 | /* Task context, fallback is to allocate a table. */
|
---|
5062 | AssertCompile(RT_ELEMENTS(pThis->apTaskDstTabs) == 2); /* for loop rollout */
|
---|
5063 | pDstTab = pThis->apIntDstTabs[iDstTab = 0];
|
---|
5064 | if (!pDstTab)
|
---|
5065 | pDstTab = pThis->apIntDstTabs[iDstTab = 1];
|
---|
5066 | if (pDstTab)
|
---|
5067 | {
|
---|
5068 | pThis->apIntDstTabs[iDstTab] = NULL;
|
---|
5069 | RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
|
---|
5070 | Assert(iDstTab < RT_ELEMENTS(pThis->apTaskDstTabs));
|
---|
5071 | }
|
---|
5072 | else
|
---|
5073 | {
|
---|
5074 | RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
|
---|
5075 | intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pDstTab);
|
---|
5076 | iDstTab = 65535;
|
---|
5077 | }
|
---|
5078 | }
|
---|
5079 | if (RT_LIKELY(pDstTab))
|
---|
5080 | {
|
---|
5081 | /*
|
---|
5082 | * Finally, get down to business of sending the frame.
|
---|
5083 | */
|
---|
5084 | INTNETSWDECISION enmSwDecision = intnetR0NetworkSend(pNetwork, NULL, fSrc, pSG, pDstTab);
|
---|
5085 | AssertMsg(enmSwDecision != INTNETSWDECISION_BAD_CONTEXT, ("fSrc=%#x fTrunkDst=%#x hdr=%.14Rhxs\n", fSrc, pDstTab->fTrunkDst, pSG->aSegs[0].pv));
|
---|
5086 | if (enmSwDecision == INTNETSWDECISION_INTNET)
|
---|
5087 | fRc = true; /* drop it */
|
---|
5088 |
|
---|
5089 | /*
|
---|
5090 | * Free the destination table.
|
---|
5091 | */
|
---|
5092 | if (iDstTab == 65535)
|
---|
5093 | RTMemFree(pDstTab);
|
---|
5094 | else
|
---|
5095 | {
|
---|
5096 | RTSpinlockAcquire(pThis->hDstTabSpinlock);
|
---|
5097 | if (fIntCtx && !pThis->apIntDstTabs[iDstTab])
|
---|
5098 | pThis->apIntDstTabs[iDstTab] = pDstTab;
|
---|
5099 | else if (!fIntCtx && !pThis->apTaskDstTabs[iDstTab])
|
---|
5100 | pThis->apTaskDstTabs[iDstTab] = pDstTab;
|
---|
5101 | else
|
---|
5102 | {
|
---|
5103 | /* this shouldn't happen! */
|
---|
5104 | PINTNETDSTTAB *papDstTabs = fIntCtx ? &pThis->apIntDstTabs[0] : &pThis->apTaskDstTabs[0];
|
---|
5105 | iDstTab = fIntCtx ? pThis->cIntDstTabs : RT_ELEMENTS(pThis->apTaskDstTabs);
|
---|
5106 | while (iDstTab-- > 0)
|
---|
5107 | if (!papDstTabs[iDstTab])
|
---|
5108 | {
|
---|
5109 | papDstTabs[iDstTab] = pDstTab;
|
---|
5110 | break;
|
---|
5111 | }
|
---|
5112 | }
|
---|
5113 | RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
|
---|
5114 | Assert(iDstTab < RT_MAX(RT_ELEMENTS(pThis->apTaskDstTabs), pThis->cIntDstTabs));
|
---|
5115 | }
|
---|
5116 | }
|
---|
5117 | }
|
---|
5118 |
|
---|
5119 | intnetR0BusyDecTrunk(pThis);
|
---|
5120 | return fRc;
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 |
|
---|
5124 | /** @copydoc INTNETTRUNKSWPORT::pfnSGRetain */
|
---|
5125 | static DECLCALLBACK(void) intnetR0TrunkIfPortSGRetain(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
|
---|
5126 | {
|
---|
5127 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
5128 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
5129 |
|
---|
5130 | /* assert some sanity */
|
---|
5131 | AssertPtrReturnVoid(pNetwork);
|
---|
5132 | AssertReturnVoid(pNetwork->hEvtBusyIf != NIL_RTSEMEVENT);
|
---|
5133 | AssertPtr(pSG);
|
---|
5134 | Assert(pSG->cUsers > 0 && pSG->cUsers < 256);
|
---|
5135 |
|
---|
5136 | /* do it. */
|
---|
5137 | ++pSG->cUsers;
|
---|
5138 | }
|
---|
5139 |
|
---|
5140 |
|
---|
5141 | /** @copydoc INTNETTRUNKSWPORT::pfnSGRelease */
|
---|
5142 | static DECLCALLBACK(void) intnetR0TrunkIfPortSGRelease(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
|
---|
5143 | {
|
---|
5144 | PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
|
---|
5145 | PINTNETNETWORK pNetwork = pThis->pNetwork;
|
---|
5146 |
|
---|
5147 | /* assert some sanity */
|
---|
5148 | AssertPtrReturnVoid(pNetwork);
|
---|
5149 | AssertReturnVoid(pNetwork->hEvtBusyIf != NIL_RTSEMEVENT);
|
---|
5150 | AssertPtr(pSG);
|
---|
5151 | Assert(pSG->cUsers > 0);
|
---|
5152 |
|
---|
5153 | /*
|
---|
5154 | * Free it?
|
---|
5155 | */
|
---|
5156 | if (!--pSG->cUsers)
|
---|
5157 | {
|
---|
5158 | /** @todo later */
|
---|
5159 | }
|
---|
5160 | }
|
---|
5161 |
|
---|
5162 |
|
---|
5163 | /**
|
---|
5164 | * Shutdown the trunk interface.
|
---|
5165 | *
|
---|
5166 | * @param pThis The trunk.
|
---|
5167 | * @param pNetworks The network.
|
---|
5168 | *
|
---|
5169 | * @remarks The caller must hold the global lock.
|
---|
5170 | */
|
---|
5171 | static void intnetR0TrunkIfDestroy(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork)
|
---|
5172 | {
|
---|
5173 | /* assert sanity */
|
---|
5174 | if (!pThis)
|
---|
5175 | return;
|
---|
5176 | AssertPtr(pThis);
|
---|
5177 | Assert(pThis->pNetwork == pNetwork);
|
---|
5178 | AssertPtrNull(pThis->pIfPort);
|
---|
5179 |
|
---|
5180 | /*
|
---|
5181 | * The interface has already been deactivated, we just to wait for
|
---|
5182 | * it to become idle before we can disconnect and release it.
|
---|
5183 | */
|
---|
5184 | PINTNETTRUNKIFPORT pIfPort = pThis->pIfPort;
|
---|
5185 | if (pIfPort)
|
---|
5186 | {
|
---|
5187 | /* unset it */
|
---|
5188 | pThis->pIfPort = NULL;
|
---|
5189 |
|
---|
5190 | /* wait in portions so we can complain ever now an then. */
|
---|
5191 | uint64_t StartTS = RTTimeSystemNanoTS();
|
---|
5192 | int rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
|
---|
5193 | if (RT_FAILURE(rc))
|
---|
5194 | {
|
---|
5195 | LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc).\n",
|
---|
5196 | pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
|
---|
5197 | Assert(rc == VERR_TIMEOUT);
|
---|
5198 | while ( RT_FAILURE(rc)
|
---|
5199 | && RTTimeSystemNanoTS() - StartTS < UINT64_C(30000000000)) /* 30 sec */
|
---|
5200 | rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
|
---|
5201 | if (rc == VERR_TIMEOUT)
|
---|
5202 | {
|
---|
5203 | LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc).\n",
|
---|
5204 | pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
|
---|
5205 | while ( rc == VERR_TIMEOUT
|
---|
5206 | && RTTimeSystemNanoTS() - StartTS < UINT64_C(360000000000)) /* 360 sec */
|
---|
5207 | rc = pIfPort->pfnWaitForIdle(pIfPort, 30*1000);
|
---|
5208 | if (RT_FAILURE(rc))
|
---|
5209 | {
|
---|
5210 | LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc), giving up.\n",
|
---|
5211 | pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
|
---|
5212 | AssertRC(rc);
|
---|
5213 | }
|
---|
5214 | }
|
---|
5215 | }
|
---|
5216 |
|
---|
5217 | /* disconnect & release it. */
|
---|
5218 | pIfPort->pfnDisconnectAndRelease(pIfPort);
|
---|
5219 | }
|
---|
5220 |
|
---|
5221 | /*
|
---|
5222 | * Free up the resources.
|
---|
5223 | */
|
---|
5224 | pThis->pNetwork = NULL;
|
---|
5225 | RTSpinlockDestroy(pThis->hDstTabSpinlock);
|
---|
5226 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->apTaskDstTabs); i++)
|
---|
5227 | {
|
---|
5228 | Assert(pThis->apTaskDstTabs[i]);
|
---|
5229 | RTMemFree(pThis->apTaskDstTabs[i]);
|
---|
5230 | pThis->apTaskDstTabs[i] = NULL;
|
---|
5231 | }
|
---|
5232 | for (unsigned i = 0; i < pThis->cIntDstTabs; i++)
|
---|
5233 | {
|
---|
5234 | Assert(pThis->apIntDstTabs[i]);
|
---|
5235 | RTMemFree(pThis->apIntDstTabs[i]);
|
---|
5236 | pThis->apIntDstTabs[i] = NULL;
|
---|
5237 | }
|
---|
5238 | RTMemFree(pThis);
|
---|
5239 | }
|
---|
5240 |
|
---|
5241 |
|
---|
5242 | /**
|
---|
5243 | * Creates the trunk connection (if any).
|
---|
5244 | *
|
---|
5245 | * @returns VBox status code.
|
---|
5246 | *
|
---|
5247 | * @param pNetwork The newly created network.
|
---|
5248 | * @param pSession The session handle.
|
---|
5249 | */
|
---|
5250 | static int intnetR0NetworkCreateTrunkIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession)
|
---|
5251 | {
|
---|
5252 | const char *pszName;
|
---|
5253 | switch (pNetwork->enmTrunkType)
|
---|
5254 | {
|
---|
5255 | /*
|
---|
5256 | * The 'None' case, simple.
|
---|
5257 | */
|
---|
5258 | case kIntNetTrunkType_None:
|
---|
5259 | case kIntNetTrunkType_WhateverNone:
|
---|
5260 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5261 | /*
|
---|
5262 | * Well, here we don't want load anything special,
|
---|
5263 | * just communicate between processes via internal network.
|
---|
5264 | */
|
---|
5265 | case kIntNetTrunkType_SrvNat:
|
---|
5266 | #endif
|
---|
5267 | return VINF_SUCCESS;
|
---|
5268 |
|
---|
5269 | /* Can't happen, but makes GCC happy. */
|
---|
5270 | default:
|
---|
5271 | return VERR_NOT_IMPLEMENTED;
|
---|
5272 |
|
---|
5273 | /*
|
---|
5274 | * Translate enum to component factory name.
|
---|
5275 | */
|
---|
5276 | case kIntNetTrunkType_NetFlt:
|
---|
5277 | pszName = "VBoxNetFlt";
|
---|
5278 | break;
|
---|
5279 | case kIntNetTrunkType_NetAdp:
|
---|
5280 | #if defined(RT_OS_DARWIN) && !defined(VBOXNETADP_DO_NOT_USE_NETFLT)
|
---|
5281 | pszName = "VBoxNetFlt";
|
---|
5282 | #else /* VBOXNETADP_DO_NOT_USE_NETFLT */
|
---|
5283 | pszName = "VBoxNetAdp";
|
---|
5284 | #endif /* VBOXNETADP_DO_NOT_USE_NETFLT */
|
---|
5285 | break;
|
---|
5286 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
5287 | case kIntNetTrunkType_SrvNat:
|
---|
5288 | pszName = "VBoxSrvNat";
|
---|
5289 | break;
|
---|
5290 | #endif
|
---|
5291 | }
|
---|
5292 |
|
---|
5293 | /*
|
---|
5294 | * Allocate the trunk interface and associated destination tables.
|
---|
5295 | *
|
---|
5296 | * We take a very optimistic view on the parallelism of the host
|
---|
5297 | * network stack and NIC driver. So, we allocate one table for each
|
---|
5298 | * possible CPU to deal with interrupt time requests and one for task
|
---|
5299 | * time calls.
|
---|
5300 | */
|
---|
5301 | RTCPUID cCpus = RTMpGetCount(); Assert(cCpus > 0);
|
---|
5302 | PINTNETTRUNKIF pTrunk = (PINTNETTRUNKIF)RTMemAllocZ(RT_OFFSETOF(INTNETTRUNKIF, apIntDstTabs[cCpus]));
|
---|
5303 | if (!pTrunk)
|
---|
5304 | return VERR_NO_MEMORY;
|
---|
5305 |
|
---|
5306 | Assert(pNetwork->MacTab.cEntriesAllocated > 0);
|
---|
5307 | int rc = VINF_SUCCESS;
|
---|
5308 | pTrunk->cIntDstTabs = cCpus;
|
---|
5309 | for (unsigned i = 0; i < cCpus && RT_SUCCESS(rc); i++)
|
---|
5310 | rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pTrunk->apIntDstTabs[i]);
|
---|
5311 | for (unsigned i = 0; i < RT_ELEMENTS(pTrunk->apTaskDstTabs) && RT_SUCCESS(rc); i++)
|
---|
5312 | rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pTrunk->apTaskDstTabs[i]);
|
---|
5313 |
|
---|
5314 | if (RT_SUCCESS(rc))
|
---|
5315 | {
|
---|
5316 | pTrunk->SwitchPort.u32Version = INTNETTRUNKSWPORT_VERSION;
|
---|
5317 | pTrunk->SwitchPort.pfnPreRecv = intnetR0TrunkIfPortPreRecv;
|
---|
5318 | pTrunk->SwitchPort.pfnRecv = intnetR0TrunkIfPortRecv;
|
---|
5319 | pTrunk->SwitchPort.pfnSGRetain = intnetR0TrunkIfPortSGRetain;
|
---|
5320 | pTrunk->SwitchPort.pfnSGRelease = intnetR0TrunkIfPortSGRelease;
|
---|
5321 | pTrunk->SwitchPort.pfnSetSGPhys = intnetR0TrunkIfPortSetSGPhys;
|
---|
5322 | pTrunk->SwitchPort.pfnReportMacAddress = intnetR0TrunkIfPortReportMacAddress;
|
---|
5323 | pTrunk->SwitchPort.pfnReportPromiscuousMode = intnetR0TrunkIfPortReportPromiscuousMode;
|
---|
5324 | pTrunk->SwitchPort.pfnReportGsoCapabilities = intnetR0TrunkIfPortReportGsoCapabilities;
|
---|
5325 | pTrunk->SwitchPort.pfnReportNoPreemptDsts = intnetR0TrunkIfPortReportNoPreemptDsts;
|
---|
5326 | pTrunk->SwitchPort.u32VersionEnd = INTNETTRUNKSWPORT_VERSION;
|
---|
5327 | //pTrunk->pIfPort = NULL;
|
---|
5328 | pTrunk->pNetwork = pNetwork;
|
---|
5329 | pTrunk->MacAddr.au8[0] = 0xff;
|
---|
5330 | pTrunk->MacAddr.au8[1] = 0xff;
|
---|
5331 | pTrunk->MacAddr.au8[2] = 0xff;
|
---|
5332 | pTrunk->MacAddr.au8[3] = 0xff;
|
---|
5333 | pTrunk->MacAddr.au8[4] = 0xff;
|
---|
5334 | pTrunk->MacAddr.au8[5] = 0xff;
|
---|
5335 | //pTrunk->fPhysSG = false;
|
---|
5336 | //pTrunk->fUnused = false;
|
---|
5337 | //pTrunk->cBusy = 0;
|
---|
5338 | //pTrunk->fNoPreemptDsts = 0;
|
---|
5339 | //pTrunk->fWireGsoCapabilites = 0;
|
---|
5340 | //pTrunk->fHostGsoCapabilites = 0;
|
---|
5341 | //pTrunk->abGsoHdrs = {0};
|
---|
5342 | pTrunk->hDstTabSpinlock = NIL_RTSPINLOCK;
|
---|
5343 | //pTrunk->apTaskDstTabs = above;
|
---|
5344 | //pTrunk->cIntDstTabs = above;
|
---|
5345 | //pTrunk->apIntDstTabs = above;
|
---|
5346 |
|
---|
5347 | /*
|
---|
5348 | * Create the lock (we've NIL'ed the members above to simplify cleanup).
|
---|
5349 | */
|
---|
5350 | rc = RTSpinlockCreate(&pTrunk->hDstTabSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hDstTabSpinlock");
|
---|
5351 | if (RT_SUCCESS(rc))
|
---|
5352 | {
|
---|
5353 | /*
|
---|
5354 | * There are a couple of bits in MacTab as well pertaining to the
|
---|
5355 | * trunk. We have to set this before it's reported.
|
---|
5356 | *
|
---|
5357 | * Note! We don't need to lock the MacTab here - creation time.
|
---|
5358 | */
|
---|
5359 | pNetwork->MacTab.pTrunk = pTrunk;
|
---|
5360 | pNetwork->MacTab.HostMac = pTrunk->MacAddr;
|
---|
5361 | pNetwork->MacTab.fHostPromiscuousReal = false;
|
---|
5362 | pNetwork->MacTab.fHostPromiscuousEff = (pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE)
|
---|
5363 | && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
|
---|
5364 | pNetwork->MacTab.fHostActive = false;
|
---|
5365 | pNetwork->MacTab.fWirePromiscuousReal = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE);
|
---|
5366 | pNetwork->MacTab.fWirePromiscuousEff = pNetwork->MacTab.fWirePromiscuousReal
|
---|
5367 | && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE);
|
---|
5368 | pNetwork->MacTab.fWireActive = false;
|
---|
5369 |
|
---|
5370 | #ifdef IN_RING0 /* (testcase is ring-3) */
|
---|
5371 | /*
|
---|
5372 | * Query the factory we want, then use it create and connect the trunk.
|
---|
5373 | */
|
---|
5374 | PINTNETTRUNKFACTORY pTrunkFactory = NULL;
|
---|
5375 | rc = SUPR0ComponentQueryFactory(pSession, pszName, INTNETTRUNKFACTORY_UUID_STR, (void **)&pTrunkFactory);
|
---|
5376 | if (RT_SUCCESS(rc))
|
---|
5377 | {
|
---|
5378 | rc = pTrunkFactory->pfnCreateAndConnect(pTrunkFactory,
|
---|
5379 | pNetwork->szTrunk,
|
---|
5380 | &pTrunk->SwitchPort,
|
---|
5381 | pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE
|
---|
5382 | ? INTNETTRUNKFACTORY_FLAG_NO_PROMISC
|
---|
5383 | : 0,
|
---|
5384 | &pTrunk->pIfPort);
|
---|
5385 | pTrunkFactory->pfnRelease(pTrunkFactory);
|
---|
5386 | if (RT_SUCCESS(rc))
|
---|
5387 | {
|
---|
5388 | Assert(pTrunk->pIfPort);
|
---|
5389 |
|
---|
5390 | Log(("intnetR0NetworkCreateTrunkIf: VINF_SUCCESS - pszName=%s szTrunk=%s%s Network=%s\n",
|
---|
5391 | pszName, pNetwork->szTrunk, pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE ? " shared-mac" : "", pNetwork->szName));
|
---|
5392 | return VINF_SUCCESS;
|
---|
5393 | }
|
---|
5394 | }
|
---|
5395 | #else /* IN_RING3 */
|
---|
5396 | NOREF(pSession);
|
---|
5397 | rc = VERR_NOT_SUPPORTED;
|
---|
5398 | #endif /* IN_RING3 */
|
---|
5399 |
|
---|
5400 | pNetwork->MacTab.pTrunk = NULL;
|
---|
5401 | }
|
---|
5402 |
|
---|
5403 | /* bail out and clean up. */
|
---|
5404 | RTSpinlockDestroy(pTrunk->hDstTabSpinlock);
|
---|
5405 | }
|
---|
5406 |
|
---|
5407 | for (unsigned i = 0; i < RT_ELEMENTS(pTrunk->apTaskDstTabs); i++)
|
---|
5408 | RTMemFree(pTrunk->apTaskDstTabs[i]);
|
---|
5409 | for (unsigned i = 0; i < pTrunk->cIntDstTabs; i++)
|
---|
5410 | RTMemFree(pTrunk->apIntDstTabs[i]);
|
---|
5411 | RTMemFree(pTrunk);
|
---|
5412 |
|
---|
5413 | LogFlow(("intnetR0NetworkCreateTrunkIf: %Rrc - pszName=%s szTrunk=%s Network=%s\n",
|
---|
5414 | rc, pszName, pNetwork->szTrunk, pNetwork->szName));
|
---|
5415 | return rc;
|
---|
5416 | }
|
---|
5417 |
|
---|
5418 |
|
---|
5419 |
|
---|
5420 | /**
|
---|
5421 | * Object destructor callback.
|
---|
5422 | * This is called for reference counted objectes when the count reaches 0.
|
---|
5423 | *
|
---|
5424 | * @param pvObj The object pointer.
|
---|
5425 | * @param pvUser1 Pointer to the network.
|
---|
5426 | * @param pvUser2 Pointer to the INTNET instance data.
|
---|
5427 | */
|
---|
5428 | static DECLCALLBACK(void) intnetR0NetworkDestruct(void *pvObj, void *pvUser1, void *pvUser2)
|
---|
5429 | {
|
---|
5430 | PINTNETNETWORK pNetwork = (PINTNETNETWORK)pvUser1;
|
---|
5431 | PINTNET pIntNet = (PINTNET)pvUser2;
|
---|
5432 | Log(("intnetR0NetworkDestruct: pvObj=%p pNetwork=%p pIntNet=%p %s\n", pvObj, pNetwork, pIntNet, pNetwork->szName));
|
---|
5433 | Assert(pNetwork->pIntNet == pIntNet);
|
---|
5434 |
|
---|
5435 | /* Take the big create/open/destroy sem. */
|
---|
5436 | RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
5437 |
|
---|
5438 | /*
|
---|
5439 | * Tell the trunk, if present, that we're about to disconnect it and wish
|
---|
5440 | * no further calls from it.
|
---|
5441 | */
|
---|
5442 | PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
|
---|
5443 | if (pTrunk)
|
---|
5444 | pTrunk->pIfPort->pfnSetState(pTrunk->pIfPort, INTNETTRUNKIFSTATE_DISCONNECTING);
|
---|
5445 |
|
---|
5446 | /*
|
---|
5447 | * Deactivate and orphan any remaining interfaces and wait for them to idle.
|
---|
5448 | *
|
---|
5449 | * Note! Normally there are no more interfaces at this point, however, when
|
---|
5450 | * supdrvCloseSession / supdrvCleanupSession release the objects the
|
---|
5451 | * order is undefined. So, it's quite possible that the network will
|
---|
5452 | * be dereference and destroyed before the interfaces.
|
---|
5453 | */
|
---|
5454 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
5455 |
|
---|
5456 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
5457 | while (iIf-- > 0)
|
---|
5458 | {
|
---|
5459 | pNetwork->MacTab.paEntries[iIf].fActive = false;
|
---|
5460 | pNetwork->MacTab.paEntries[iIf].pIf->fActive = false;
|
---|
5461 | }
|
---|
5462 |
|
---|
5463 | pNetwork->MacTab.fHostActive = false;
|
---|
5464 | pNetwork->MacTab.fWireActive = false;
|
---|
5465 |
|
---|
5466 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
5467 |
|
---|
5468 | /* Wait for all the interfaces to quiesce. (Interfaces cannot be
|
---|
5469 | removed / added since we're holding the big lock.) */
|
---|
5470 | if (pTrunk)
|
---|
5471 | intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
|
---|
5472 |
|
---|
5473 | iIf = pNetwork->MacTab.cEntries;
|
---|
5474 | while (iIf-- > 0)
|
---|
5475 | intnetR0BusyWait(pNetwork, &pNetwork->MacTab.paEntries[iIf].pIf->cBusy);
|
---|
5476 |
|
---|
5477 | /* Orphan the interfaces (not trunk). Don't bother with calling
|
---|
5478 | pfnDisconnectInterface here since the networking is going away. */
|
---|
5479 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
5480 | while ((iIf = pNetwork->MacTab.cEntries) > 0)
|
---|
5481 | {
|
---|
5482 | PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf - 1].pIf;
|
---|
5483 | RTSpinlockRelease(pNetwork->hAddrSpinlock);
|
---|
5484 |
|
---|
5485 | intnetR0BusyWait(pNetwork, &pIf->cBusy);
|
---|
5486 |
|
---|
5487 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
5488 | if ( iIf == pNetwork->MacTab.cEntries /* paranoia */
|
---|
5489 | && pIf->cBusy)
|
---|
5490 | {
|
---|
5491 | pIf->pNetwork = NULL;
|
---|
5492 | pNetwork->MacTab.cEntries--;
|
---|
5493 | }
|
---|
5494 | }
|
---|
5495 |
|
---|
5496 | /*
|
---|
5497 | * Zap the trunk pointer while we still own the spinlock, destroy the
|
---|
5498 | * trunk after we've left it. Note that this might take a while...
|
---|
5499 | */
|
---|
5500 | pNetwork->MacTab.pTrunk = NULL;
|
---|
5501 |
|
---|
5502 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
5503 |
|
---|
5504 | if (pTrunk)
|
---|
5505 | intnetR0TrunkIfDestroy(pTrunk, pNetwork);
|
---|
5506 |
|
---|
5507 | /*
|
---|
5508 | * Unlink the network.
|
---|
5509 | * Note that it needn't be in the list if we failed during creation.
|
---|
5510 | */
|
---|
5511 | PINTNETNETWORK pPrev = pIntNet->pNetworks;
|
---|
5512 | if (pPrev == pNetwork)
|
---|
5513 | pIntNet->pNetworks = pNetwork->pNext;
|
---|
5514 | else
|
---|
5515 | {
|
---|
5516 | for (; pPrev; pPrev = pPrev->pNext)
|
---|
5517 | if (pPrev->pNext == pNetwork)
|
---|
5518 | {
|
---|
5519 | pPrev->pNext = pNetwork->pNext;
|
---|
5520 | break;
|
---|
5521 | }
|
---|
5522 | }
|
---|
5523 | pNetwork->pNext = NULL;
|
---|
5524 | pNetwork->pvObj = NULL;
|
---|
5525 |
|
---|
5526 | /*
|
---|
5527 | * Free resources.
|
---|
5528 | */
|
---|
5529 | RTSemEventDestroy(pNetwork->hEvtBusyIf);
|
---|
5530 | pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
|
---|
5531 | RTSpinlockDestroy(pNetwork->hAddrSpinlock);
|
---|
5532 | pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
|
---|
5533 | RTMemFree(pNetwork->MacTab.paEntries);
|
---|
5534 | pNetwork->MacTab.paEntries = NULL;
|
---|
5535 | RTMemFree(pNetwork);
|
---|
5536 |
|
---|
5537 | /* Release the create/destroy sem. */
|
---|
5538 | RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
5539 | }
|
---|
5540 |
|
---|
5541 |
|
---|
5542 | /**
|
---|
5543 | * Checks if the open network flags are compatible.
|
---|
5544 | *
|
---|
5545 | * @returns VBox status code.
|
---|
5546 | * @param pNetwork The network.
|
---|
5547 | * @param fFlags The open network flags.
|
---|
5548 | */
|
---|
5549 | static int intnetR0CheckOpenNetworkFlags(PINTNETNETWORK pNetwork, uint32_t fFlags)
|
---|
5550 | {
|
---|
5551 | uint32_t const fNetFlags = pNetwork->fFlags;
|
---|
5552 |
|
---|
5553 | if ( (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
5554 | ^ (fNetFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE))
|
---|
5555 | return VERR_INTNET_INCOMPATIBLE_FLAGS;
|
---|
5556 |
|
---|
5557 | if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_EXACT)
|
---|
5558 | {
|
---|
5559 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
5560 | if ( (fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair)
|
---|
5561 | && (fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair)
|
---|
5562 | != (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair) )
|
---|
5563 | return VERR_INTNET_INCOMPATIBLE_FLAGS;
|
---|
5564 | }
|
---|
5565 |
|
---|
5566 | if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
|
---|
5567 | {
|
---|
5568 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
5569 | if ( (fFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
|
---|
5570 | && !(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
|
---|
5571 | && (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed) )
|
---|
5572 | return VERR_INTNET_INCOMPATIBLE_FLAGS;
|
---|
5573 | }
|
---|
5574 |
|
---|
5575 | return VINF_SUCCESS;
|
---|
5576 | }
|
---|
5577 |
|
---|
5578 |
|
---|
5579 | /**
|
---|
5580 | * Adapts flag changes on network opening.
|
---|
5581 | *
|
---|
5582 | * @returns VBox status code.
|
---|
5583 | * @param pNetwork The network.
|
---|
5584 | * @param fFlags The open network flags.
|
---|
5585 | */
|
---|
5586 | static int intnetR0AdaptOpenNetworkFlags(PINTNETNETWORK pNetwork, uint32_t fFlags)
|
---|
5587 | {
|
---|
5588 | /*
|
---|
5589 | * Upgrade the minimum policy flags.
|
---|
5590 | */
|
---|
5591 | uint32_t fNetMinFlags = pNetwork->fMinFlags;
|
---|
5592 | Assert(!(fNetMinFlags & INTNET_OPEN_FLAGS_RELAXED_MASK));
|
---|
5593 | if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
|
---|
5594 | {
|
---|
5595 | fNetMinFlags |= fFlags & INTNET_OPEN_FLAGS_STRICT_MASK;
|
---|
5596 | if (fNetMinFlags != pNetwork->fMinFlags)
|
---|
5597 | {
|
---|
5598 | LogRel(("INTNET: %s - min flags changed %#x -> %#x\n", pNetwork->szName, pNetwork->fMinFlags, fNetMinFlags));
|
---|
5599 | pNetwork->fMinFlags = fNetMinFlags;
|
---|
5600 | }
|
---|
5601 | }
|
---|
5602 |
|
---|
5603 | /*
|
---|
5604 | * Calculate the new network flags.
|
---|
5605 | * (Depends on fNetMinFlags being recalculated first.)
|
---|
5606 | */
|
---|
5607 | uint32_t fNetFlags = pNetwork->fFlags;
|
---|
5608 |
|
---|
5609 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
5610 | {
|
---|
5611 | Assert(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair);
|
---|
5612 | Assert(!(fNetMinFlags & g_afIntNetOpenNetworkNetFlags[i].fRelaxed));
|
---|
5613 |
|
---|
5614 | if (!(fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair))
|
---|
5615 | continue;
|
---|
5616 | if (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed)
|
---|
5617 | continue;
|
---|
5618 |
|
---|
5619 | if ( (fNetMinFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
|
---|
5620 | || (fFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive) )
|
---|
5621 | {
|
---|
5622 | fNetFlags &= ~g_afIntNetOpenNetworkNetFlags[i].fPair;
|
---|
5623 | fNetFlags |= g_afIntNetOpenNetworkNetFlags[i].fRestrictive;
|
---|
5624 | }
|
---|
5625 | else if (!(fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES))
|
---|
5626 | {
|
---|
5627 | fNetFlags &= ~g_afIntNetOpenNetworkNetFlags[i].fPair;
|
---|
5628 | fNetFlags |= g_afIntNetOpenNetworkNetFlags[i].fRelaxed;
|
---|
5629 | }
|
---|
5630 | }
|
---|
5631 |
|
---|
5632 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
5633 | {
|
---|
5634 | Assert(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair);
|
---|
5635 | fNetFlags |= fFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed;
|
---|
5636 | }
|
---|
5637 |
|
---|
5638 | /*
|
---|
5639 | * Apply the flags if they changed.
|
---|
5640 | */
|
---|
5641 | uint32_t const fOldNetFlags = pNetwork->fFlags;
|
---|
5642 | if (fOldNetFlags != fNetFlags)
|
---|
5643 | {
|
---|
5644 | LogRel(("INTNET: %s - flags changed %#x -> %#x\n", pNetwork->szName, fOldNetFlags, fNetFlags));
|
---|
5645 |
|
---|
5646 | RTSpinlockAcquire(pNetwork->hAddrSpinlock);
|
---|
5647 |
|
---|
5648 | pNetwork->fFlags = fNetFlags;
|
---|
5649 |
|
---|
5650 | /* Recalculate some derived switcher variables. */
|
---|
5651 | bool fActiveTrunk = pNetwork->MacTab.pTrunk
|
---|
5652 | && pNetwork->cActiveIFs > 0;
|
---|
5653 | pNetwork->MacTab.fHostActive = fActiveTrunk
|
---|
5654 | && (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
|
---|
5655 | pNetwork->MacTab.fHostPromiscuousEff = ( pNetwork->MacTab.fHostPromiscuousReal
|
---|
5656 | || (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE))
|
---|
5657 | && (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
|
---|
5658 |
|
---|
5659 | pNetwork->MacTab.fWireActive = fActiveTrunk
|
---|
5660 | && (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
|
---|
5661 | pNetwork->MacTab.fWirePromiscuousReal= RT_BOOL(fNetFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE);
|
---|
5662 | pNetwork->MacTab.fWirePromiscuousEff = pNetwork->MacTab.fWirePromiscuousReal
|
---|
5663 | && (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE);
|
---|
5664 |
|
---|
5665 | if ((fOldNetFlags ^ fNetFlags) & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS)
|
---|
5666 | {
|
---|
5667 | pNetwork->MacTab.cPromiscuousEntries = 0;
|
---|
5668 | pNetwork->MacTab.cPromiscuousNoTrunkEntries = 0;
|
---|
5669 |
|
---|
5670 | uint32_t iIf = pNetwork->MacTab.cEntries;
|
---|
5671 | while (iIf-- > 0)
|
---|
5672 | {
|
---|
5673 | PINTNETMACTABENTRY pEntry = &pNetwork->MacTab.paEntries[iIf];
|
---|
5674 | PINTNETIF pIf2 = pEntry->pIf;
|
---|
5675 | if ( pIf2 /* paranoia */
|
---|
5676 | && pIf2->fPromiscuousReal)
|
---|
5677 | {
|
---|
5678 | bool fPromiscuousEff = (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS)
|
---|
5679 | && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW);
|
---|
5680 | pEntry->fPromiscuousEff = fPromiscuousEff;
|
---|
5681 | pEntry->fPromiscuousSeeTrunk = fPromiscuousEff
|
---|
5682 | && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK);
|
---|
5683 |
|
---|
5684 | if (pEntry->fPromiscuousEff)
|
---|
5685 | {
|
---|
5686 | pNetwork->MacTab.cPromiscuousEntries++;
|
---|
5687 | if (!pEntry->fPromiscuousSeeTrunk)
|
---|
5688 | pNetwork->MacTab.cPromiscuousNoTrunkEntries++;
|
---|
5689 | }
|
---|
5690 | }
|
---|
5691 | }
|
---|
5692 | }
|
---|
5693 |
|
---|
5694 | RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
|
---|
5695 | }
|
---|
5696 |
|
---|
5697 | return VINF_SUCCESS;
|
---|
5698 | }
|
---|
5699 |
|
---|
5700 |
|
---|
5701 | /**
|
---|
5702 | * Opens an existing network.
|
---|
5703 | *
|
---|
5704 | * The call must own the INTNET::hMtxCreateOpenDestroy.
|
---|
5705 | *
|
---|
5706 | * @returns VBox status code.
|
---|
5707 | * @param pIntNet The instance data.
|
---|
5708 | * @param pSession The current session.
|
---|
5709 | * @param pszNetwork The network name. This has a valid length.
|
---|
5710 | * @param enmTrunkType The trunk type.
|
---|
5711 | * @param pszTrunk The trunk name. Its meaning is specific to the type.
|
---|
5712 | * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
|
---|
5713 | * @param ppNetwork Where to store the pointer to the network on success.
|
---|
5714 | */
|
---|
5715 | static int intnetR0OpenNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
|
---|
5716 | const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
|
---|
5717 | {
|
---|
5718 | LogFlow(("intnetR0OpenNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
|
---|
5719 | pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
|
---|
5720 |
|
---|
5721 | /* just pro forma validation, the caller is internal. */
|
---|
5722 | AssertPtr(pIntNet);
|
---|
5723 | AssertPtr(pSession);
|
---|
5724 | AssertPtr(pszNetwork);
|
---|
5725 | Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
|
---|
5726 | AssertPtr(pszTrunk);
|
---|
5727 | Assert(!(fFlags & ~INTNET_OPEN_FLAGS_MASK));
|
---|
5728 | AssertPtr(ppNetwork);
|
---|
5729 | *ppNetwork = NULL;
|
---|
5730 |
|
---|
5731 | /*
|
---|
5732 | * Search networks by name.
|
---|
5733 | */
|
---|
5734 | PINTNETNETWORK pCur;
|
---|
5735 | uint8_t cchName = (uint8_t)strlen(pszNetwork);
|
---|
5736 | Assert(cchName && cchName < sizeof(pCur->szName)); /* caller ensures this */
|
---|
5737 |
|
---|
5738 | pCur = pIntNet->pNetworks;
|
---|
5739 | while (pCur)
|
---|
5740 | {
|
---|
5741 | if ( pCur->cchName == cchName
|
---|
5742 | && !memcmp(pCur->szName, pszNetwork, cchName))
|
---|
5743 | {
|
---|
5744 | /*
|
---|
5745 | * Found the network, now check that we have the same ideas
|
---|
5746 | * about the trunk setup and security.
|
---|
5747 | */
|
---|
5748 | int rc;
|
---|
5749 | if ( enmTrunkType == kIntNetTrunkType_WhateverNone
|
---|
5750 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5751 | || enmTrunkType == kIntNetTrunkType_SrvNat /* @todo: what does it mean */
|
---|
5752 | #endif
|
---|
5753 | || ( pCur->enmTrunkType == enmTrunkType
|
---|
5754 | && !strcmp(pCur->szTrunk, pszTrunk)))
|
---|
5755 | {
|
---|
5756 | rc = intnetR0CheckOpenNetworkFlags(pCur, fFlags);
|
---|
5757 | if (RT_SUCCESS(rc))
|
---|
5758 | {
|
---|
5759 | /*
|
---|
5760 | * Increment the reference and check that the session
|
---|
5761 | * can access this network.
|
---|
5762 | */
|
---|
5763 | rc = SUPR0ObjAddRef(pCur->pvObj, pSession);
|
---|
5764 | if (RT_SUCCESS(rc))
|
---|
5765 | {
|
---|
5766 | if (pCur->fFlags & INTNET_OPEN_FLAGS_ACCESS_RESTRICTED)
|
---|
5767 | rc = SUPR0ObjVerifyAccess(pCur->pvObj, pSession, pCur->szName);
|
---|
5768 | if (RT_SUCCESS(rc))
|
---|
5769 | *ppNetwork = pCur;
|
---|
5770 | else
|
---|
5771 | SUPR0ObjRelease(pCur->pvObj, pSession);
|
---|
5772 | }
|
---|
5773 | else if (rc == VERR_WRONG_ORDER)
|
---|
5774 | rc = VERR_NOT_FOUND; /* destruction race, pretend the other isn't there. */
|
---|
5775 | }
|
---|
5776 | }
|
---|
5777 | else
|
---|
5778 | {
|
---|
5779 | rc = VERR_INTNET_INCOMPATIBLE_TRUNK;
|
---|
5780 | LogRel(("intnetR0OpenNetwork failed. rc=%Rrc pCur->szTrunk=%s pszTrunk=%s pCur->enmTrunkType=%d enmTrunkType=%d\n",
|
---|
5781 | rc, pCur->szTrunk, pszTrunk, pCur->enmTrunkType, enmTrunkType));
|
---|
5782 | }
|
---|
5783 |
|
---|
5784 | LogFlow(("intnetR0OpenNetwork: returns %Rrc *ppNetwork=%p\n", rc, *ppNetwork));
|
---|
5785 | return rc;
|
---|
5786 | }
|
---|
5787 |
|
---|
5788 | pCur = pCur->pNext;
|
---|
5789 | }
|
---|
5790 |
|
---|
5791 | LogFlow(("intnetR0OpenNetwork: returns VERR_NOT_FOUND\n"));
|
---|
5792 | return VERR_NOT_FOUND;
|
---|
5793 | }
|
---|
5794 |
|
---|
5795 |
|
---|
5796 | /**
|
---|
5797 | * Creates a new network.
|
---|
5798 | *
|
---|
5799 | * The call must own the INTNET::hMtxCreateOpenDestroy and has already attempted
|
---|
5800 | * opening the network and found it to be non-existing.
|
---|
5801 | *
|
---|
5802 | * @returns VBox status code.
|
---|
5803 | * @param pIntNet The instance data.
|
---|
5804 | * @param pSession The session handle.
|
---|
5805 | * @param pszNetwork The name of the network. This must be at least one character long and no longer
|
---|
5806 | * than the INTNETNETWORK::szName.
|
---|
5807 | * @param enmTrunkType The trunk type.
|
---|
5808 | * @param pszTrunk The trunk name. Its meaning is specific to the type.
|
---|
5809 | * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
|
---|
5810 | * @param ppNetwork Where to store the network. In the case of failure
|
---|
5811 | * whatever is returned here should be dereferenced
|
---|
5812 | * outside the INTNET::hMtxCreateOpenDestroy.
|
---|
5813 | */
|
---|
5814 | static int intnetR0CreateNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
|
---|
5815 | const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
|
---|
5816 | {
|
---|
5817 | LogFlow(("intnetR0CreateNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
|
---|
5818 | pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
|
---|
5819 |
|
---|
5820 | /* just pro forma validation, the caller is internal. */
|
---|
5821 | AssertPtr(pIntNet);
|
---|
5822 | AssertPtr(pSession);
|
---|
5823 | AssertPtr(pszNetwork);
|
---|
5824 | Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
|
---|
5825 | AssertPtr(pszTrunk);
|
---|
5826 | Assert(!(fFlags & ~INTNET_OPEN_FLAGS_MASK));
|
---|
5827 | AssertPtr(ppNetwork);
|
---|
5828 |
|
---|
5829 | *ppNetwork = NULL;
|
---|
5830 |
|
---|
5831 | /*
|
---|
5832 | * Adjust the flags with defaults for the network policies.
|
---|
5833 | * Note: Main restricts promiscuous mode on the per interface level.
|
---|
5834 | */
|
---|
5835 | fFlags &= ~( INTNET_OPEN_FLAGS_IF_FIXED
|
---|
5836 | | INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW
|
---|
5837 | | INTNET_OPEN_FLAGS_IF_PROMISC_DENY
|
---|
5838 | | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK
|
---|
5839 | | INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK
|
---|
5840 | | INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES
|
---|
5841 | | INTNET_OPEN_FLAGS_REQUIRE_EXACT);
|
---|
5842 | uint32_t fDefFlags = INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS
|
---|
5843 | | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST
|
---|
5844 | | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE
|
---|
5845 | | INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED
|
---|
5846 | | INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE
|
---|
5847 | | INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED
|
---|
5848 | | INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE;
|
---|
5849 | if ( enmTrunkType == kIntNetTrunkType_WhateverNone
|
---|
5850 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
5851 | || enmTrunkType == kIntNetTrunkType_SrvNat /* simialar security */
|
---|
5852 | #endif
|
---|
5853 | || enmTrunkType == kIntNetTrunkType_None)
|
---|
5854 | fDefFlags |= INTNET_OPEN_FLAGS_ACCESS_RESTRICTED;
|
---|
5855 | else
|
---|
5856 | fDefFlags |= INTNET_OPEN_FLAGS_ACCESS_PUBLIC;
|
---|
5857 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
5858 | if (!(fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair))
|
---|
5859 | fFlags |= g_afIntNetOpenNetworkNetFlags[i].fPair & fDefFlags;
|
---|
5860 |
|
---|
5861 | /*
|
---|
5862 | * Allocate and initialize.
|
---|
5863 | */
|
---|
5864 | size_t cb = sizeof(INTNETNETWORK);
|
---|
5865 | if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
5866 | cb += INTNETNETWORK_TMP_SIZE + 64;
|
---|
5867 | PINTNETNETWORK pNetwork = (PINTNETNETWORK)RTMemAllocZ(cb);
|
---|
5868 | if (!pNetwork)
|
---|
5869 | return VERR_NO_MEMORY;
|
---|
5870 | //pNetwork->pNext = NULL;
|
---|
5871 | //pNetwork->pIfs = NULL;
|
---|
5872 | pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
|
---|
5873 | pNetwork->MacTab.cEntries = 0;
|
---|
5874 | pNetwork->MacTab.cEntriesAllocated = INTNET_GROW_DSTTAB_SIZE;
|
---|
5875 | //pNetwork->MacTab.cPromiscuousEntries = 0;
|
---|
5876 | //pNetwork->MacTab.cPromiscuousNoTrunkEntries = 0;
|
---|
5877 | pNetwork->MacTab.paEntries = NULL;
|
---|
5878 | pNetwork->MacTab.fHostPromiscuousReal = false;
|
---|
5879 | pNetwork->MacTab.fHostPromiscuousEff = false;
|
---|
5880 | pNetwork->MacTab.fHostActive = false;
|
---|
5881 | pNetwork->MacTab.fWirePromiscuousReal = false;
|
---|
5882 | pNetwork->MacTab.fWirePromiscuousEff = false;
|
---|
5883 | pNetwork->MacTab.fWireActive = false;
|
---|
5884 | pNetwork->MacTab.pTrunk = NULL;
|
---|
5885 | pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
|
---|
5886 | pNetwork->pIntNet = pIntNet;
|
---|
5887 | //pNetwork->pvObj = NULL;
|
---|
5888 | if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
|
---|
5889 | pNetwork->pbTmp = RT_ALIGN_PT(pNetwork + 1, 64, uint8_t *);
|
---|
5890 | //else
|
---|
5891 | // pNetwork->pbTmp = NULL;
|
---|
5892 | pNetwork->fFlags = fFlags;
|
---|
5893 | //pNetwork->fMinFlags = 0;
|
---|
5894 | //pNetwork->cActiveIFs = 0;
|
---|
5895 | size_t cchName = strlen(pszNetwork);
|
---|
5896 | pNetwork->cchName = (uint8_t)cchName;
|
---|
5897 | Assert(cchName && cchName < sizeof(pNetwork->szName)); /* caller's responsibility. */
|
---|
5898 | memcpy(pNetwork->szName, pszNetwork, cchName); /* '\0' at courtesy of alloc. */
|
---|
5899 | pNetwork->enmTrunkType = enmTrunkType;
|
---|
5900 | Assert(strlen(pszTrunk) < sizeof(pNetwork->szTrunk)); /* caller's responsibility. */
|
---|
5901 | strcpy(pNetwork->szTrunk, pszTrunk);
|
---|
5902 |
|
---|
5903 | /*
|
---|
5904 | * Create the semaphore, spinlock and allocate the interface table.
|
---|
5905 | */
|
---|
5906 | int rc = RTSemEventCreate(&pNetwork->hEvtBusyIf);
|
---|
5907 | if (RT_SUCCESS(rc))
|
---|
5908 | rc = RTSpinlockCreate(&pNetwork->hAddrSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hAddrSpinlock");
|
---|
5909 | if (RT_SUCCESS(rc))
|
---|
5910 | {
|
---|
5911 | pNetwork->MacTab.paEntries = (PINTNETMACTABENTRY)RTMemAlloc(sizeof(INTNETMACTABENTRY) * pNetwork->MacTab.cEntriesAllocated);
|
---|
5912 | if (!pNetwork->MacTab.paEntries)
|
---|
5913 | rc = VERR_NO_MEMORY;
|
---|
5914 | }
|
---|
5915 | if (RT_SUCCESS(rc))
|
---|
5916 | {
|
---|
5917 | /*
|
---|
5918 | * Register the object in the current session and link it into the network list.
|
---|
5919 | */
|
---|
5920 | pNetwork->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK, intnetR0NetworkDestruct, pNetwork, pIntNet);
|
---|
5921 | if (pNetwork->pvObj)
|
---|
5922 | {
|
---|
5923 | pNetwork->pNext = pIntNet->pNetworks;
|
---|
5924 | pIntNet->pNetworks = pNetwork;
|
---|
5925 |
|
---|
5926 | /*
|
---|
5927 | * Check if the current session is actually allowed to create and
|
---|
5928 | * open the network. It is possible to implement network name
|
---|
5929 | * based policies and these must be checked now. SUPR0ObjRegister
|
---|
5930 | * does no such checks.
|
---|
5931 | */
|
---|
5932 | rc = SUPR0ObjVerifyAccess(pNetwork->pvObj, pSession, pNetwork->szName);
|
---|
5933 | if (RT_SUCCESS(rc))
|
---|
5934 | {
|
---|
5935 | /*
|
---|
5936 | * Connect the trunk.
|
---|
5937 | */
|
---|
5938 | rc = intnetR0NetworkCreateTrunkIf(pNetwork, pSession);
|
---|
5939 | if (RT_SUCCESS(rc))
|
---|
5940 | {
|
---|
5941 | *ppNetwork = pNetwork;
|
---|
5942 | LogFlow(("intnetR0CreateNetwork: returns VINF_SUCCESS *ppNetwork=%p\n", pNetwork));
|
---|
5943 | return VINF_SUCCESS;
|
---|
5944 | }
|
---|
5945 | }
|
---|
5946 |
|
---|
5947 | SUPR0ObjRelease(pNetwork->pvObj, pSession);
|
---|
5948 | LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
|
---|
5949 | return rc;
|
---|
5950 | }
|
---|
5951 |
|
---|
5952 | /* cleanup */
|
---|
5953 | rc = VERR_NO_MEMORY;
|
---|
5954 | }
|
---|
5955 |
|
---|
5956 | RTSemEventDestroy(pNetwork->hEvtBusyIf);
|
---|
5957 | pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
|
---|
5958 | RTSpinlockDestroy(pNetwork->hAddrSpinlock);
|
---|
5959 | pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
|
---|
5960 | RTMemFree(pNetwork->MacTab.paEntries);
|
---|
5961 | pNetwork->MacTab.paEntries = NULL;
|
---|
5962 | RTMemFree(pNetwork);
|
---|
5963 |
|
---|
5964 | LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
|
---|
5965 | return rc;
|
---|
5966 | }
|
---|
5967 |
|
---|
5968 |
|
---|
5969 | /**
|
---|
5970 | * Opens a network interface and connects it to the specified network.
|
---|
5971 | *
|
---|
5972 | * @returns VBox status code.
|
---|
5973 | * @param pSession The session handle.
|
---|
5974 | * @param pszNetwork The network name.
|
---|
5975 | * @param enmTrunkType The trunk type.
|
---|
5976 | * @param pszTrunk The trunk name. Its meaning is specific to the type.
|
---|
5977 | * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
|
---|
5978 | * @param fRestrictAccess Whether new participants should be subjected to access check or not.
|
---|
5979 | * @param cbSend The send buffer size.
|
---|
5980 | * @param cbRecv The receive buffer size.
|
---|
5981 | * @param phIf Where to store the handle to the network interface.
|
---|
5982 | */
|
---|
5983 | INTNETR0DECL(int) IntNetR0Open(PSUPDRVSESSION pSession, const char *pszNetwork,
|
---|
5984 | INTNETTRUNKTYPE enmTrunkType, const char *pszTrunk, uint32_t fFlags,
|
---|
5985 | uint32_t cbSend, uint32_t cbRecv, PINTNETIFHANDLE phIf)
|
---|
5986 | {
|
---|
5987 | LogFlow(("IntNetR0Open: pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x cbSend=%u cbRecv=%u phIf=%p\n",
|
---|
5988 | pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, cbSend, cbRecv, phIf));
|
---|
5989 |
|
---|
5990 | /*
|
---|
5991 | * Validate input.
|
---|
5992 | */
|
---|
5993 | PINTNET pIntNet = g_pIntNet;
|
---|
5994 | AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
|
---|
5995 | AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
|
---|
5996 |
|
---|
5997 | AssertPtrReturn(pszNetwork, VERR_INVALID_PARAMETER);
|
---|
5998 | const char *pszNetworkEnd = RTStrEnd(pszNetwork, INTNET_MAX_NETWORK_NAME);
|
---|
5999 | AssertReturn(pszNetworkEnd, VERR_INVALID_PARAMETER);
|
---|
6000 | size_t cchNetwork = pszNetworkEnd - pszNetwork;
|
---|
6001 | AssertReturn(cchNetwork, VERR_INVALID_PARAMETER);
|
---|
6002 |
|
---|
6003 | if (pszTrunk)
|
---|
6004 | {
|
---|
6005 | AssertPtrReturn(pszTrunk, VERR_INVALID_PARAMETER);
|
---|
6006 | const char *pszTrunkEnd = RTStrEnd(pszTrunk, INTNET_MAX_TRUNK_NAME);
|
---|
6007 | AssertReturn(pszTrunkEnd, VERR_INVALID_PARAMETER);
|
---|
6008 | }
|
---|
6009 | else
|
---|
6010 | pszTrunk = "";
|
---|
6011 |
|
---|
6012 | AssertMsgReturn(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End,
|
---|
6013 | ("%d\n", enmTrunkType), VERR_INVALID_PARAMETER);
|
---|
6014 | switch (enmTrunkType)
|
---|
6015 | {
|
---|
6016 | case kIntNetTrunkType_None:
|
---|
6017 | case kIntNetTrunkType_WhateverNone:
|
---|
6018 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
6019 | case kIntNetTrunkType_SrvNat:
|
---|
6020 | #endif
|
---|
6021 | if (*pszTrunk)
|
---|
6022 | return VERR_INVALID_PARAMETER;
|
---|
6023 | break;
|
---|
6024 |
|
---|
6025 | case kIntNetTrunkType_NetFlt:
|
---|
6026 | case kIntNetTrunkType_NetAdp:
|
---|
6027 | if (!*pszTrunk)
|
---|
6028 | return VERR_INVALID_PARAMETER;
|
---|
6029 | break;
|
---|
6030 |
|
---|
6031 | default:
|
---|
6032 | return VERR_NOT_IMPLEMENTED;
|
---|
6033 | }
|
---|
6034 |
|
---|
6035 | AssertMsgReturn(!(fFlags & ~INTNET_OPEN_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
6036 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
|
---|
6037 | AssertMsgReturn((fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair) != g_afIntNetOpenNetworkNetFlags[i].fPair,
|
---|
6038 | ("%#x (%#x)\n", fFlags, g_afIntNetOpenNetworkNetFlags[i].fPair), VERR_INVALID_PARAMETER);
|
---|
6039 | for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkIfFlags); i++)
|
---|
6040 | AssertMsgReturn((fFlags & g_afIntNetOpenNetworkIfFlags[i].fPair) != g_afIntNetOpenNetworkIfFlags[i].fPair,
|
---|
6041 | ("%#x (%#x)\n", fFlags, g_afIntNetOpenNetworkIfFlags[i].fPair), VERR_INVALID_PARAMETER);
|
---|
6042 | AssertPtrReturn(phIf, VERR_INVALID_PARAMETER);
|
---|
6043 |
|
---|
6044 | /*
|
---|
6045 | * Acquire the mutex to serialize open/create/close.
|
---|
6046 | */
|
---|
6047 | int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
6048 | if (RT_FAILURE(rc))
|
---|
6049 | return rc;
|
---|
6050 |
|
---|
6051 | /*
|
---|
6052 | * Try open / create the network and create an interface on it for the
|
---|
6053 | * caller to use.
|
---|
6054 | */
|
---|
6055 | PINTNETNETWORK pNetwork = NULL;
|
---|
6056 | rc = intnetR0OpenNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
|
---|
6057 | if (RT_SUCCESS(rc))
|
---|
6058 | {
|
---|
6059 | rc = intnetR0NetworkCreateIf(pNetwork, pSession, cbSend, cbRecv, fFlags, phIf);
|
---|
6060 | if (RT_SUCCESS(rc))
|
---|
6061 | {
|
---|
6062 | intnetR0AdaptOpenNetworkFlags(pNetwork, fFlags);
|
---|
6063 | rc = VINF_ALREADY_INITIALIZED;
|
---|
6064 | }
|
---|
6065 | else
|
---|
6066 | SUPR0ObjRelease(pNetwork->pvObj, pSession);
|
---|
6067 | }
|
---|
6068 | else if (rc == VERR_NOT_FOUND)
|
---|
6069 | {
|
---|
6070 | rc = intnetR0CreateNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
|
---|
6071 | if (RT_SUCCESS(rc))
|
---|
6072 | {
|
---|
6073 | rc = intnetR0NetworkCreateIf(pNetwork, pSession, cbSend, cbRecv, fFlags, phIf);
|
---|
6074 | if (RT_FAILURE(rc))
|
---|
6075 | SUPR0ObjRelease(pNetwork->pvObj, pSession);
|
---|
6076 | }
|
---|
6077 | }
|
---|
6078 |
|
---|
6079 | RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
6080 | LogFlow(("IntNetR0Open: return %Rrc *phIf=%RX32\n", rc, *phIf));
|
---|
6081 | return rc;
|
---|
6082 | }
|
---|
6083 |
|
---|
6084 |
|
---|
6085 | /**
|
---|
6086 | * VMMR0 request wrapper for IntNetR0Open.
|
---|
6087 | *
|
---|
6088 | * @returns see GMMR0MapUnmapChunk.
|
---|
6089 | * @param pSession The caller's session.
|
---|
6090 | * @param pReq The request packet.
|
---|
6091 | */
|
---|
6092 | INTNETR0DECL(int) IntNetR0OpenReq(PSUPDRVSESSION pSession, PINTNETOPENREQ pReq)
|
---|
6093 | {
|
---|
6094 | if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
|
---|
6095 | return VERR_INVALID_PARAMETER;
|
---|
6096 | return IntNetR0Open(pSession, &pReq->szNetwork[0], pReq->enmTrunkType, pReq->szTrunk,
|
---|
6097 | pReq->fFlags, pReq->cbSend, pReq->cbRecv, &pReq->hIf);
|
---|
6098 | }
|
---|
6099 |
|
---|
6100 |
|
---|
6101 | /**
|
---|
6102 | * Count the internal networks.
|
---|
6103 | *
|
---|
6104 | * This is mainly for providing the testcase with some introspection to validate
|
---|
6105 | * behavior when closing interfaces.
|
---|
6106 | *
|
---|
6107 | * @returns The number of networks.
|
---|
6108 | */
|
---|
6109 | INTNETR0DECL(uint32_t) IntNetR0GetNetworkCount(void)
|
---|
6110 | {
|
---|
6111 | /*
|
---|
6112 | * Grab the instance.
|
---|
6113 | */
|
---|
6114 | PINTNET pIntNet = g_pIntNet;
|
---|
6115 | if (!pIntNet)
|
---|
6116 | return 0;
|
---|
6117 | AssertPtrReturn(pIntNet, 0);
|
---|
6118 | AssertReturn(pIntNet->u32Magic == INTNET_MAGIC, 0);
|
---|
6119 |
|
---|
6120 | /*
|
---|
6121 | * Grab the mutex and count the networks.
|
---|
6122 | */
|
---|
6123 | int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
|
---|
6124 | if (RT_FAILURE(rc))
|
---|
6125 | return 0;
|
---|
6126 |
|
---|
6127 | uint32_t cNetworks = 0;
|
---|
6128 | for (PINTNETNETWORK pCur = pIntNet->pNetworks; pCur; pCur = pCur->pNext)
|
---|
6129 | cNetworks++;
|
---|
6130 |
|
---|
6131 | RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
|
---|
6132 |
|
---|
6133 | return cNetworks;
|
---|
6134 | }
|
---|
6135 |
|
---|
6136 |
|
---|
6137 |
|
---|
6138 | /**
|
---|
6139 | * Destroys an instance of the Ring-0 internal networking service.
|
---|
6140 | */
|
---|
6141 | INTNETR0DECL(void) IntNetR0Term(void)
|
---|
6142 | {
|
---|
6143 | LogFlow(("IntNetR0Term:\n"));
|
---|
6144 |
|
---|
6145 | /*
|
---|
6146 | * Zap the global pointer and validate it.
|
---|
6147 | */
|
---|
6148 | PINTNET pIntNet = g_pIntNet;
|
---|
6149 | g_pIntNet = NULL;
|
---|
6150 | if (!pIntNet)
|
---|
6151 | return;
|
---|
6152 | AssertPtrReturnVoid(pIntNet);
|
---|
6153 | AssertReturnVoid(pIntNet->u32Magic == INTNET_MAGIC);
|
---|
6154 |
|
---|
6155 | /*
|
---|
6156 | * There is not supposed to be any networks hanging around at this time.
|
---|
6157 | */
|
---|
6158 | AssertReturnVoid(ASMAtomicCmpXchgU32(&pIntNet->u32Magic, ~INTNET_MAGIC, INTNET_MAGIC));
|
---|
6159 | Assert(pIntNet->pNetworks == NULL);
|
---|
6160 | if (pIntNet->hMtxCreateOpenDestroy != NIL_RTSEMMUTEX)
|
---|
6161 | {
|
---|
6162 | RTSemMutexDestroy(pIntNet->hMtxCreateOpenDestroy);
|
---|
6163 | pIntNet->hMtxCreateOpenDestroy = NIL_RTSEMMUTEX;
|
---|
6164 | }
|
---|
6165 | if (pIntNet->hHtIfs != NIL_RTHANDLETABLE)
|
---|
6166 | {
|
---|
6167 | /** @todo does it make sense to have a deleter here? */
|
---|
6168 | RTHandleTableDestroy(pIntNet->hHtIfs, NULL, NULL);
|
---|
6169 | pIntNet->hHtIfs = NIL_RTHANDLETABLE;
|
---|
6170 | }
|
---|
6171 |
|
---|
6172 | RTMemFree(pIntNet);
|
---|
6173 | }
|
---|
6174 |
|
---|
6175 |
|
---|
6176 | /**
|
---|
6177 | * Initializes the internal network ring-0 service.
|
---|
6178 | *
|
---|
6179 | * @returns VBox status code.
|
---|
6180 | */
|
---|
6181 | INTNETR0DECL(int) IntNetR0Init(void)
|
---|
6182 | {
|
---|
6183 | LogFlow(("IntNetR0Init:\n"));
|
---|
6184 | int rc = VERR_NO_MEMORY;
|
---|
6185 | PINTNET pIntNet = (PINTNET)RTMemAllocZ(sizeof(*pIntNet));
|
---|
6186 | if (pIntNet)
|
---|
6187 | {
|
---|
6188 | //pIntNet->pNetworks = NULL;
|
---|
6189 |
|
---|
6190 | rc = RTSemMutexCreate(&pIntNet->hMtxCreateOpenDestroy);
|
---|
6191 | if (RT_SUCCESS(rc))
|
---|
6192 | {
|
---|
6193 | rc = RTHandleTableCreateEx(&pIntNet->hHtIfs, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
|
---|
6194 | UINT32_C(0x8ffe0000), 4096, intnetR0IfRetainHandle, NULL);
|
---|
6195 | if (RT_SUCCESS(rc))
|
---|
6196 | {
|
---|
6197 | pIntNet->u32Magic = INTNET_MAGIC;
|
---|
6198 | g_pIntNet = pIntNet;
|
---|
6199 | LogFlow(("IntNetR0Init: returns VINF_SUCCESS pIntNet=%p\n", pIntNet));
|
---|
6200 | return VINF_SUCCESS;
|
---|
6201 | }
|
---|
6202 |
|
---|
6203 | RTSemMutexDestroy(pIntNet->hMtxCreateOpenDestroy);
|
---|
6204 | }
|
---|
6205 | RTMemFree(pIntNet);
|
---|
6206 | }
|
---|
6207 | LogFlow(("IntNetR0Init: returns %Rrc\n", rc));
|
---|
6208 | return rc;
|
---|
6209 | }
|
---|
6210 |
|
---|