VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/linux/VBoxNetFlt-linux.c@ 47484

Last change on this file since 47484 was 47484, checked in by vboxsync, 11 years ago

VBoxNetFlt/linux: fix for Linux 3.11

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.6 KB
Line 
1/* $Id: VBoxNetFlt-linux.c 47484 2013-07-31 09:30:33Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Linux Specific Code.
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* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
22#define VBOXNETFLT_LINUX_NO_XMIT_QUEUE
23#include "the-linux-kernel.h"
24#include "version-generated.h"
25#include "product-generated.h"
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/rtnetlink.h>
29#include <linux/miscdevice.h>
30#include <linux/ip.h>
31#include <linux/if_vlan.h>
32
33#include <VBox/log.h>
34#include <VBox/err.h>
35#include <VBox/intnetinline.h>
36#include <VBox/vmm/pdmnetinline.h>
37#include <VBox/param.h>
38#include <iprt/alloca.h>
39#include <iprt/assert.h>
40#include <iprt/spinlock.h>
41#include <iprt/semaphore.h>
42#include <iprt/initterm.h>
43#include <iprt/process.h>
44#include <iprt/mem.h>
45#include <iprt/net.h>
46#include <iprt/log.h>
47#include <iprt/mp.h>
48#include <iprt/mem.h>
49#include <iprt/time.h>
50
51#define VBOXNETFLT_OS_SPECFIC 1
52#include "../VBoxNetFltInternal.h"
53
54
55/*******************************************************************************
56* Defined Constants And Macros *
57*******************************************************************************/
58#define VBOX_FLT_NB_TO_INST(pNB) RT_FROM_MEMBER(pNB, VBOXNETFLTINS, u.s.Notifier)
59#define VBOX_FLT_PT_TO_INST(pPT) RT_FROM_MEMBER(pPT, VBOXNETFLTINS, u.s.PacketType)
60#ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
61# define VBOX_FLT_XT_TO_INST(pXT) RT_FROM_MEMBER(pXT, VBOXNETFLTINS, u.s.XmitTask)
62#endif
63
64#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
65# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb_reset_network_header(skb)
66# define VBOX_SKB_RESET_MAC_HDR(skb) skb_reset_mac_header(skb)
67#else
68# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb->nh.raw = skb->data
69# define VBOX_SKB_RESET_MAC_HDR(skb) skb->mac.raw = skb->data
70#endif
71
72#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
73# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb)
74#else
75# define CHECKSUM_PARTIAL CHECKSUM_HW
76# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
77# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb, 0)
78# else
79# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
80# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(&skb, 0)
81# else
82# define VBOX_SKB_CHECKSUM_HELP(skb) (!skb_checksum_help(skb))
83# endif
84/* Versions prior 2.6.10 use stats for both bstats and qstats */
85# define bstats stats
86# define qstats stats
87# endif
88#endif
89
90#ifndef NET_IP_ALIGN
91# define NET_IP_ALIGN 2
92#endif
93
94#if 0
95/** Create scatter / gather segments for fragments. When not used, we will
96 * linearize the socket buffer before creating the internal networking SG. */
97# define VBOXNETFLT_SG_SUPPORT 1
98#endif
99
100#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
101/** Indicates that the linux kernel may send us GSO frames. */
102# define VBOXNETFLT_WITH_GSO 1
103
104/** This enables or disables the transmitting of GSO frame from the internal
105 * network and to the host. */
106# define VBOXNETFLT_WITH_GSO_XMIT_HOST 1
107
108# if 0 /** @todo This is currently disable because it causes performance loss of 5-10%. */
109/** This enables or disables the transmitting of GSO frame from the internal
110 * network and to the wire. */
111# define VBOXNETFLT_WITH_GSO_XMIT_WIRE 1
112# endif
113
114/** This enables or disables the forwarding/flooding of GSO frame from the host
115 * to the internal network. */
116# define VBOXNETFLT_WITH_GSO_RECV 1
117
118#endif
119
120#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
121/** This enables or disables handling of GSO frames coming from the wire (GRO). */
122# define VBOXNETFLT_WITH_GRO 1
123#endif
124/*
125 * GRO support was backported to RHEL 5.4
126 */
127#ifdef RHEL_RELEASE_CODE
128# if RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5, 4)
129# define VBOXNETFLT_WITH_GRO 1
130# endif
131#endif
132
133/*******************************************************************************
134* Internal Functions *
135*******************************************************************************/
136static int VBoxNetFltLinuxInit(void);
137static void VBoxNetFltLinuxUnload(void);
138static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf);
139
140
141/*******************************************************************************
142* Global Variables *
143*******************************************************************************/
144/**
145 * The (common) global data.
146 */
147static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
148
149module_init(VBoxNetFltLinuxInit);
150module_exit(VBoxNetFltLinuxUnload);
151
152MODULE_AUTHOR(VBOX_VENDOR);
153MODULE_DESCRIPTION(VBOX_PRODUCT " Network Filter Driver");
154MODULE_LICENSE("GPL");
155#ifdef MODULE_VERSION
156MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
157#endif
158
159
160#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) && defined(LOG_ENABLED)
161unsigned dev_get_flags(const struct net_device *dev)
162{
163 unsigned flags;
164
165 flags = (dev->flags & ~(IFF_PROMISC |
166 IFF_ALLMULTI |
167 IFF_RUNNING)) |
168 (dev->gflags & (IFF_PROMISC |
169 IFF_ALLMULTI));
170
171 if (netif_running(dev) && netif_carrier_ok(dev))
172 flags |= IFF_RUNNING;
173
174 return flags;
175}
176#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) */
177
178
179/**
180 * Initialize module.
181 *
182 * @returns appropriate status code.
183 */
184static int __init VBoxNetFltLinuxInit(void)
185{
186 int rc;
187 /*
188 * Initialize IPRT.
189 */
190 rc = RTR0Init(0);
191 if (RT_SUCCESS(rc))
192 {
193 Log(("VBoxNetFltLinuxInit\n"));
194
195 /*
196 * Initialize the globals and connect to the support driver.
197 *
198 * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
199 * for establishing the connect to the support driver.
200 */
201 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
202 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
203 if (RT_SUCCESS(rc))
204 {
205 LogRel(("VBoxNetFlt: Successfully started.\n"));
206 return 0;
207 }
208
209 LogRel(("VBoxNetFlt: failed to initialize device extension (rc=%d)\n", rc));
210 RTR0Term();
211 }
212 else
213 LogRel(("VBoxNetFlt: failed to initialize IPRT (rc=%d)\n", rc));
214
215 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
216 return -RTErrConvertToErrno(rc);
217}
218
219
220/**
221 * Unload the module.
222 *
223 * @todo We have to prevent this if we're busy!
224 */
225static void __exit VBoxNetFltLinuxUnload(void)
226{
227 int rc;
228 Log(("VBoxNetFltLinuxUnload\n"));
229 Assert(vboxNetFltCanUnload(&g_VBoxNetFltGlobals));
230
231 /*
232 * Undo the work done during start (in reverse order).
233 */
234 rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
235 AssertRC(rc); NOREF(rc);
236
237 RTR0Term();
238
239 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
240
241 Log(("VBoxNetFltLinuxUnload - done\n"));
242}
243
244
245/**
246 * We filter traffic from the host to the internal network
247 * before it reaches the NIC driver.
248 *
249 * The current code uses a very ugly hack overriding hard_start_xmit
250 * callback in the device structure, but it has been shown to give us a
251 * performance boost of 60-100% though. Eventually we have to find some
252 * less hacky way of getting this job done.
253 */
254#define VBOXNETFLT_WITH_HOST2WIRE_FILTER
255
256#ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
257
258# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
259
260# include <linux/ethtool.h>
261
262typedef struct ethtool_ops OVR_OPSTYPE;
263# define OVR_OPS ethtool_ops
264# define OVR_XMIT pfnStartXmit
265
266# else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
267
268typedef struct net_device_ops OVR_OPSTYPE;
269# define OVR_OPS netdev_ops
270# define OVR_XMIT pOrgOps->ndo_start_xmit
271
272# endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
273
274/**
275 * The overridden net_device_ops of the device we're attached to.
276 *
277 * As there is no net_device_ops structure in pre-2.6.29 kernels we override
278 * ethtool_ops instead along with hard_start_xmit callback in net_device
279 * structure.
280 *
281 * This is a very dirty hack that was created to explore how much we can improve
282 * the host to guest transfers by not CC'ing the NIC. It turns out to be
283 * the only way to filter outgoing packets for devices without TX queue.
284 */
285typedef struct VBoxNetDeviceOpsOverride
286{
287 /** Our overridden ops. */
288 OVR_OPSTYPE Ops;
289 /** Magic word. */
290 uint32_t u32Magic;
291 /** Pointer to the original ops. */
292 OVR_OPSTYPE const *pOrgOps;
293# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
294 /** Pointer to the original hard_start_xmit function. */
295 int (*pfnStartXmit)(struct sk_buff *pSkb, struct net_device *pDev);
296# endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
297 /** Pointer to the net filter instance. */
298 PVBOXNETFLTINS pVBoxNetFlt;
299 /** The number of filtered packages. */
300 uint64_t cFiltered;
301 /** The total number of packets */
302 uint64_t cTotal;
303} VBOXNETDEVICEOPSOVERRIDE, *PVBOXNETDEVICEOPSOVERRIDE;
304/** VBOXNETDEVICEOPSOVERRIDE::u32Magic value. */
305#define VBOXNETDEVICEOPSOVERRIDE_MAGIC UINT32_C(0x00c0ffee)
306
307/**
308 * ndo_start_xmit wrapper that drops packets that shouldn't go to the wire
309 * because they belong on the internal network.
310 *
311 * @returns NETDEV_TX_XXX.
312 * @param pSkb The socket buffer to transmit.
313 * @param pDev The net device.
314 */
315static int vboxNetFltLinuxStartXmitFilter(struct sk_buff *pSkb, struct net_device *pDev)
316{
317 PVBOXNETDEVICEOPSOVERRIDE pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->OVR_OPS;
318 uint8_t abHdrBuf[sizeof(RTNETETHERHDR) + sizeof(uint32_t) + RTNETIPV4_MIN_LEN];
319 PCRTNETETHERHDR pEtherHdr;
320 PINTNETTRUNKSWPORT pSwitchPort;
321 uint32_t cbHdrs;
322
323
324 /*
325 * Validate the override structure.
326 *
327 * Note! We're racing vboxNetFltLinuxUnhookDev here. If this was supposed
328 * to be production quality code, we would have to be much more
329 * careful here and avoid the race.
330 */
331 if ( !VALID_PTR(pOverride)
332 || pOverride->u32Magic != VBOXNETDEVICEOPSOVERRIDE_MAGIC
333# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
334 || !VALID_PTR(pOverride->pOrgOps)
335# endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
336 )
337 {
338 printk("vboxNetFltLinuxStartXmitFilter: bad override %p\n", pOverride);
339 dev_kfree_skb(pSkb);
340 return NETDEV_TX_OK;
341 }
342 pOverride->cTotal++;
343
344 /*
345 * Do the filtering base on the default OUI of our virtual NICs
346 *
347 * Note! In a real solution, we would ask the switch whether the
348 * destination MAC is 100% to be on the internal network and then
349 * drop it.
350 */
351 cbHdrs = skb_headlen(pSkb);
352 cbHdrs = RT_MIN(cbHdrs, sizeof(abHdrBuf));
353 pEtherHdr = (PCRTNETETHERHDR)skb_header_pointer(pSkb, 0, cbHdrs, &abHdrBuf[0]);
354 if ( pEtherHdr
355 && VALID_PTR(pOverride->pVBoxNetFlt)
356 && (pSwitchPort = pOverride->pVBoxNetFlt->pSwitchPort) != NULL
357 && VALID_PTR(pSwitchPort)
358 && cbHdrs >= 6)
359 {
360 INTNETSWDECISION enmDecision;
361
362 /** @todo consider reference counting, etc. */
363 enmDecision = pSwitchPort->pfnPreRecv(pSwitchPort, pEtherHdr, cbHdrs, INTNETTRUNKDIR_HOST);
364 if (enmDecision == INTNETSWDECISION_INTNET)
365 {
366 dev_kfree_skb(pSkb);
367 pOverride->cFiltered++;
368 return NETDEV_TX_OK;
369 }
370 }
371
372 return pOverride->OVR_XMIT(pSkb, pDev);
373}
374
375/**
376 * Hooks the device ndo_start_xmit operation of the device.
377 *
378 * @param pThis The net filter instance.
379 * @param pDev The net device.
380 */
381static void vboxNetFltLinuxHookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
382{
383 PVBOXNETDEVICEOPSOVERRIDE pOverride;
384
385 /* Cancel override if ethtool_ops is missing (host-only case, @bugref{5712}) */
386 if (!VALID_PTR(pDev->OVR_OPS))
387 return;
388 pOverride = RTMemAlloc(sizeof(*pOverride));
389 if (!pOverride)
390 return;
391 pOverride->pOrgOps = pDev->OVR_OPS;
392 pOverride->Ops = *pDev->OVR_OPS;
393# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
394 pOverride->pfnStartXmit = pDev->hard_start_xmit;
395# else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
396 pOverride->Ops.ndo_start_xmit = vboxNetFltLinuxStartXmitFilter;
397# endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
398 pOverride->u32Magic = VBOXNETDEVICEOPSOVERRIDE_MAGIC;
399 pOverride->cTotal = 0;
400 pOverride->cFiltered = 0;
401 pOverride->pVBoxNetFlt = pThis;
402
403 RTSpinlockAcquire(pThis->hSpinlock); /* (this isn't necessary, but so what) */
404 ASMAtomicWritePtr((void * volatile *)&pDev->OVR_OPS, pOverride);
405# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
406 ASMAtomicXchgPtr((void * volatile *)&pDev->hard_start_xmit, vboxNetFltLinuxStartXmitFilter);
407# endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
408 RTSpinlockReleaseNoInts(pThis->hSpinlock);
409}
410
411/**
412 * Undos what vboxNetFltLinuxHookDev did.
413 *
414 * @param pThis The net filter instance.
415 * @param pDev The net device. Can be NULL, in which case
416 * we'll try retrieve it from @a pThis.
417 */
418static void vboxNetFltLinuxUnhookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
419{
420 PVBOXNETDEVICEOPSOVERRIDE pOverride;
421
422 RTSpinlockAcquire(pThis->hSpinlock);
423 if (!pDev)
424 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
425 if (VALID_PTR(pDev))
426 {
427 pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->OVR_OPS;
428 if ( VALID_PTR(pOverride)
429 && pOverride->u32Magic == VBOXNETDEVICEOPSOVERRIDE_MAGIC
430 && VALID_PTR(pOverride->pOrgOps)
431 )
432 {
433# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
434 ASMAtomicWritePtr((void * volatile *)&pDev->hard_start_xmit, pOverride->pfnStartXmit);
435# endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
436 ASMAtomicWritePtr((void const * volatile *)&pDev->OVR_OPS, pOverride->pOrgOps);
437 ASMAtomicWriteU32(&pOverride->u32Magic, 0);
438 }
439 else
440 pOverride = NULL;
441 }
442 else
443 pOverride = NULL;
444 RTSpinlockReleaseNoInts(pThis->hSpinlock);
445
446 if (pOverride)
447 {
448 printk("vboxnetflt: %llu out of %llu packets were not sent (directed to host)\n", pOverride->cFiltered, pOverride->cTotal);
449 RTMemFree(pOverride);
450 }
451}
452
453#endif /* VBOXNETFLT_WITH_HOST2WIRE_FILTER */
454
455
456/**
457 * Reads and retains the host interface handle.
458 *
459 * @returns The handle, NULL if detached.
460 * @param pThis
461 */
462DECLINLINE(struct net_device *) vboxNetFltLinuxRetainNetDev(PVBOXNETFLTINS pThis)
463{
464#if 0
465 struct net_device *pDev = NULL;
466
467 Log(("vboxNetFltLinuxRetainNetDev\n"));
468 /*
469 * Be careful here to avoid problems racing the detached callback.
470 */
471 RTSpinlockAcquire(pThis->hSpinlock);
472 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
473 {
474 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
475 if (pDev)
476 {
477 dev_hold(pDev);
478 Log(("vboxNetFltLinuxRetainNetDev: Device %p(%s) retained. ref=%d\n",
479 pDev, pDev->name,
480#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
481 netdev_refcnt_read(pDev)
482#else
483 atomic_read(&pDev->refcnt)
484#endif
485 ));
486 }
487 }
488 RTSpinlockRelease(pThis->hSpinlock);
489
490 Log(("vboxNetFltLinuxRetainNetDev - done\n"));
491 return pDev;
492#else
493 return ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
494#endif
495}
496
497
498/**
499 * Release the host interface handle previously retained
500 * by vboxNetFltLinuxRetainNetDev.
501 *
502 * @param pThis The instance.
503 * @param pDev The vboxNetFltLinuxRetainNetDev
504 * return value, NULL is fine.
505 */
506DECLINLINE(void) vboxNetFltLinuxReleaseNetDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
507{
508#if 0
509 Log(("vboxNetFltLinuxReleaseNetDev\n"));
510 NOREF(pThis);
511 if (pDev)
512 {
513 dev_put(pDev);
514 Log(("vboxNetFltLinuxReleaseNetDev: Device %p(%s) released. ref=%d\n",
515 pDev, pDev->name,
516#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
517 netdev_refcnt_read(pDev)
518#else
519 atomic_read(&pDev->refcnt)
520#endif
521 ));
522 }
523 Log(("vboxNetFltLinuxReleaseNetDev - done\n"));
524#endif
525}
526
527#define VBOXNETFLT_CB_TAG(skb) (0xA1C90000 | (skb->dev->ifindex & 0xFFFF))
528#define VBOXNETFLT_SKB_TAG(skb) (*(uint32_t*)&((skb)->cb[sizeof((skb)->cb)-sizeof(uint32_t)]))
529
530/**
531 * Checks whether this is an mbuf created by vboxNetFltLinuxMBufFromSG,
532 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
533 *
534 * @returns true / false accordingly.
535 * @param pBuf The sk_buff.
536 */
537DECLINLINE(bool) vboxNetFltLinuxSkBufIsOur(struct sk_buff *pBuf)
538{
539 return VBOXNETFLT_SKB_TAG(pBuf) == VBOXNETFLT_CB_TAG(pBuf);
540}
541
542
543/**
544 * Checks whether this SG list contains a GSO packet.
545 *
546 * @returns true / false accordingly.
547 * @param pSG The (scatter/)gather list.
548 */
549DECLINLINE(bool) vboxNetFltLinuxIsGso(PINTNETSG pSG)
550{
551#if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
552 return !((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID);
553#else /* !VBOXNETFLT_WITH_GSO_XMIT_WIRE && !VBOXNETFLT_WITH_GSO_XMIT_HOST */
554 return false;
555#endif /* !VBOXNETFLT_WITH_GSO_XMIT_WIRE && !VBOXNETFLT_WITH_GSO_XMIT_HOST */
556}
557
558
559/**
560 * Find out the frame size (of a single segment in case of GSO frames).
561 *
562 * @returns the frame size.
563 * @param pSG The (scatter/)gather list.
564 */
565DECLINLINE(uint32_t) vboxNetFltLinuxFrameSize(PINTNETSG pSG)
566{
567 uint16_t u16Type = 0;
568 uint32_t cbVlanTag = 0;
569 if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR))
570 u16Type = RT_BE2H_U16(((PCRTNETETHERHDR)pSG->aSegs[0].pv)->EtherType);
571 else if (pSG->cbTotal >= sizeof(RTNETETHERHDR))
572 {
573 uint32_t off = RT_OFFSETOF(RTNETETHERHDR, EtherType);
574 uint32_t i;
575 for (i = 0; i < pSG->cSegsUsed; ++i)
576 {
577 if (off <= pSG->aSegs[i].cb)
578 {
579 if (off + sizeof(uint16_t) <= pSG->aSegs[i].cb)
580 u16Type = RT_BE2H_U16(*(uint16_t *)((uintptr_t)pSG->aSegs[i].pv + off));
581 else if (i + 1 < pSG->cSegsUsed)
582 u16Type = RT_BE2H_U16( ((uint16_t)( ((uint8_t *)pSG->aSegs[i].pv)[off] ) << 8)
583 + *(uint8_t *)pSG->aSegs[i + 1].pv); /* ASSUMES no empty segments! */
584 /* else: frame is too short. */
585 break;
586 }
587 off -= pSG->aSegs[i].cb;
588 }
589 }
590 if (u16Type == RTNET_ETHERTYPE_VLAN)
591 cbVlanTag = 4;
592 return (vboxNetFltLinuxIsGso(pSG) ? (uint32_t)pSG->GsoCtx.cbMaxSeg + pSG->GsoCtx.cbHdrsTotal : pSG->cbTotal) - cbVlanTag;
593}
594
595
596/**
597 * Internal worker that create a linux sk_buff for a
598 * (scatter/)gather list.
599 *
600 * @returns Pointer to the sk_buff.
601 * @param pThis The instance.
602 * @param pSG The (scatter/)gather list.
603 * @param fDstWire Set if the destination is the wire.
604 */
605static struct sk_buff *vboxNetFltLinuxSkBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG, bool fDstWire)
606{
607 struct sk_buff *pPkt;
608 struct net_device *pDev;
609 unsigned fGsoType = 0;
610
611 if (pSG->cbTotal == 0)
612 {
613 LogRel(("VBoxNetFlt: Dropped empty packet coming from internal network.\n"));
614 return NULL;
615 }
616 Log5(("VBoxNetFlt: Packet to %s of %d bytes (frame=%d).\n", fDstWire?"wire":"host", pSG->cbTotal, vboxNetFltLinuxFrameSize(pSG)));
617 if (fDstWire && (vboxNetFltLinuxFrameSize(pSG) > ASMAtomicReadU32(&pThis->u.s.cbMtu) + 14))
618 {
619 static bool s_fOnce = true;
620 if (s_fOnce)
621 {
622 s_fOnce = false;
623 printk("VBoxNetFlt: Dropped over-sized packet (%d bytes) coming from internal network.\n", vboxNetFltLinuxFrameSize(pSG));
624 }
625 return NULL;
626 }
627
628 /** @todo We should use fragments mapping the SG buffers with large packets.
629 * 256 bytes seems to be the a threshold used a lot for this. It
630 * requires some nasty work on the intnet side though... */
631 /*
632 * Allocate a packet and copy over the data.
633 */
634 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
635 pPkt = dev_alloc_skb(pSG->cbTotal + NET_IP_ALIGN);
636 if (RT_UNLIKELY(!pPkt))
637 {
638 Log(("vboxNetFltLinuxSkBufFromSG: Failed to allocate sk_buff(%u).\n", pSG->cbTotal));
639 pSG->pvUserData = NULL;
640 return NULL;
641 }
642 pPkt->dev = pDev;
643 pPkt->ip_summed = CHECKSUM_NONE;
644
645 /* Align IP header on 16-byte boundary: 2 + 14 (ethernet hdr size). */
646 skb_reserve(pPkt, NET_IP_ALIGN);
647
648 /* Copy the segments. */
649 skb_put(pPkt, pSG->cbTotal);
650 IntNetSgRead(pSG, pPkt->data);
651
652#if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
653 /*
654 * Setup GSO if used by this packet.
655 */
656 switch ((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type)
657 {
658 default:
659 AssertMsgFailed(("%u (%s)\n", pSG->GsoCtx.u8Type, PDMNetGsoTypeName((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type) ));
660 /* fall thru */
661 case PDMNETWORKGSOTYPE_INVALID:
662 fGsoType = 0;
663 break;
664 case PDMNETWORKGSOTYPE_IPV4_TCP:
665 fGsoType = SKB_GSO_TCPV4;
666 break;
667 case PDMNETWORKGSOTYPE_IPV4_UDP:
668 fGsoType = SKB_GSO_UDP;
669 break;
670 case PDMNETWORKGSOTYPE_IPV6_TCP:
671 fGsoType = SKB_GSO_TCPV6;
672 break;
673 }
674 if (fGsoType)
675 {
676 struct skb_shared_info *pShInfo = skb_shinfo(pPkt);
677
678 pShInfo->gso_type = fGsoType | SKB_GSO_DODGY;
679 pShInfo->gso_size = pSG->GsoCtx.cbMaxSeg;
680 pShInfo->gso_segs = PDMNetGsoCalcSegmentCount(&pSG->GsoCtx, pSG->cbTotal);
681
682 /*
683 * We need to set checksum fields even if the packet goes to the host
684 * directly as it may be immediately forwarded by IP layer @bugref{5020}.
685 */
686 Assert(skb_headlen(pPkt) >= pSG->GsoCtx.cbHdrsTotal);
687 pPkt->ip_summed = CHECKSUM_PARTIAL;
688# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
689 pPkt->csum_start = skb_headroom(pPkt) + pSG->GsoCtx.offHdr2;
690 if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
691 pPkt->csum_offset = RT_OFFSETOF(RTNETTCP, th_sum);
692 else
693 pPkt->csum_offset = RT_OFFSETOF(RTNETUDP, uh_sum);
694# else
695 pPkt->h.raw = pPkt->data + pSG->GsoCtx.offHdr2;
696 if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
697 pPkt->csum = RT_OFFSETOF(RTNETTCP, th_sum);
698 else
699 pPkt->csum = RT_OFFSETOF(RTNETUDP, uh_sum);
700# endif
701 if (!fDstWire)
702 PDMNetGsoPrepForDirectUse(&pSG->GsoCtx, pPkt->data, pSG->cbTotal, PDMNETCSUMTYPE_PSEUDO);
703 }
704#endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE || VBOXNETFLT_WITH_GSO_XMIT_HOST */
705
706 /*
707 * Finish up the socket buffer.
708 */
709 pPkt->protocol = eth_type_trans(pPkt, pDev);
710 if (fDstWire)
711 {
712 VBOX_SKB_RESET_NETWORK_HDR(pPkt);
713
714 /* Restore ethernet header back. */
715 skb_push(pPkt, ETH_HLEN); /** @todo VLAN: +4 if VLAN? */
716 VBOX_SKB_RESET_MAC_HDR(pPkt);
717 }
718 VBOXNETFLT_SKB_TAG(pPkt) = VBOXNETFLT_CB_TAG(pPkt);
719
720 return pPkt;
721}
722
723
724/**
725 * Initializes a SG list from an sk_buff.
726 *
727 * @returns Number of segments.
728 * @param pThis The instance.
729 * @param pBuf The sk_buff.
730 * @param pSG The SG.
731 * @param pvFrame The frame pointer, optional.
732 * @param cSegs The number of segments allocated for the SG.
733 * This should match the number in the mbuf exactly!
734 * @param fSrc The source of the frame.
735 * @param pGso Pointer to the GSO context if it's a GSO
736 * internal network frame. NULL if regular frame.
737 */
738DECLINLINE(void) vboxNetFltLinuxSkBufToSG(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, PINTNETSG pSG,
739 unsigned cSegs, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
740{
741 int i;
742 NOREF(pThis);
743
744 Assert(!skb_shinfo(pBuf)->frag_list);
745
746 if (!pGsoCtx)
747 IntNetSgInitTempSegs(pSG, pBuf->len, cSegs, 0 /*cSegsUsed*/);
748 else
749 IntNetSgInitTempSegsGso(pSG, pBuf->len, cSegs, 0 /*cSegsUsed*/, pGsoCtx);
750
751#ifdef VBOXNETFLT_SG_SUPPORT
752 pSG->aSegs[0].cb = skb_headlen(pBuf);
753 pSG->aSegs[0].pv = pBuf->data;
754 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
755
756 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
757 {
758 skb_frag_t *pFrag = &skb_shinfo(pBuf)->frags[i];
759 pSG->aSegs[i+1].cb = pFrag->size;
760 pSG->aSegs[i+1].pv = kmap(pFrag->page);
761 printk("%p = kmap()\n", pSG->aSegs[i+1].pv);
762 pSG->aSegs[i+1].Phys = NIL_RTHCPHYS;
763 }
764 ++i;
765
766#else
767 pSG->aSegs[0].cb = pBuf->len;
768 pSG->aSegs[0].pv = pBuf->data;
769 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
770 i = 1;
771#endif
772
773 pSG->cSegsUsed = i;
774
775#ifdef PADD_RUNT_FRAMES_FROM_HOST
776 /*
777 * Add a trailer if the frame is too small.
778 *
779 * Since we're getting to the packet before it is framed, it has not
780 * yet been padded. The current solution is to add a segment pointing
781 * to a buffer containing all zeros and pray that works for all frames...
782 */
783 if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
784 {
785 static uint8_t const s_abZero[128] = {0};
786
787 AssertReturnVoid(i < cSegs);
788
789 pSG->aSegs[i].Phys = NIL_RTHCPHYS;
790 pSG->aSegs[i].pv = (void *)&s_abZero[0];
791 pSG->aSegs[i].cb = 60 - pSG->cbTotal;
792 pSG->cbTotal = 60;
793 pSG->cSegsUsed++;
794 Assert(i + 1 <= pSG->cSegsAlloc)
795 }
796#endif
797
798 Log4(("vboxNetFltLinuxSkBufToSG: allocated=%d, segments=%d frags=%d next=%p frag_list=%p pkt_type=%x fSrc=%x\n",
799 pSG->cSegsAlloc, pSG->cSegsUsed, skb_shinfo(pBuf)->nr_frags, pBuf->next, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, fSrc));
800 for (i = 0; i < pSG->cSegsUsed; i++)
801 Log4(("vboxNetFltLinuxSkBufToSG: #%d: cb=%d pv=%p\n",
802 i, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
803}
804
805/**
806 * Packet handler,
807 *
808 * @returns 0 or EJUSTRETURN.
809 * @param pThis The instance.
810 * @param pMBuf The mbuf.
811 * @param pvFrame The start of the frame, optional.
812 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
813 * @param eProtocol The protocol.
814 */
815#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
816static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
817 struct net_device *pSkbDev,
818 struct packet_type *pPacketType,
819 struct net_device *pOrigDev)
820#else
821static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
822 struct net_device *pSkbDev,
823 struct packet_type *pPacketType)
824#endif
825{
826 PVBOXNETFLTINS pThis;
827 struct net_device *pDev;
828 LogFlow(("vboxNetFltLinuxPacketHandler: pBuf=%p pSkbDev=%p pPacketType=%p\n",
829 pBuf, pSkbDev, pPacketType));
830#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
831 Log3(("vboxNetFltLinuxPacketHandler: skb len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
832 pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size, skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
833# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
834 Log4(("vboxNetFltLinuxPacketHandler: packet dump follows:\n%.*Rhxd\n", pBuf->len-pBuf->data_len, skb_mac_header(pBuf)));
835# endif
836#else
837 Log3(("vboxNetFltLinuxPacketHandler: skb len=%u data_len=%u truesize=%u next=%p nr_frags=%u tso_size=%u tso_seqs=%u frag_list=%p pkt_type=%x\n",
838 pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->tso_size, skb_shinfo(pBuf)->tso_segs, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
839#endif
840 /*
841 * Drop it immediately?
842 */
843 if (!pBuf)
844 return 0;
845
846 if (pBuf->pkt_type == PACKET_LOOPBACK)
847 {
848 /*
849 * We are not interested in loopbacked packets as they will always have
850 * another copy going to the wire.
851 */
852 Log2(("vboxNetFltLinuxPacketHandler: dropped loopback packet (cb=%u)\n", pBuf->len));
853 dev_kfree_skb(pBuf); /* We must 'consume' all packets we get (@bugref{6539})! */
854 return 0;
855 }
856
857 pThis = VBOX_FLT_PT_TO_INST(pPacketType);
858 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
859 if (pDev != pSkbDev)
860 {
861 Log(("vboxNetFltLinuxPacketHandler: Devices do not match, pThis may be wrong! pThis=%p\n", pThis));
862 kfree_skb(pBuf); /* This is a failure, so we use kfree_skb instead of dev_kfree_skb. */
863 return 0;
864 }
865
866 Log4(("vboxNetFltLinuxPacketHandler: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
867 if (vboxNetFltLinuxSkBufIsOur(pBuf))
868 {
869 Log2(("vboxNetFltLinuxPacketHandler: got our own sk_buff, drop it.\n"));
870 dev_kfree_skb(pBuf);
871 return 0;
872 }
873
874#ifndef VBOXNETFLT_SG_SUPPORT
875 {
876 /*
877 * Get rid of fragmented packets, they cause too much trouble.
878 */
879 unsigned int uMacLen = pBuf->mac_len;
880 struct sk_buff *pCopy = skb_copy(pBuf, GFP_ATOMIC);
881 dev_kfree_skb(pBuf);
882 if (!pCopy)
883 {
884 LogRel(("VBoxNetFlt: Failed to allocate packet buffer, dropping the packet.\n"));
885 return 0;
886 }
887 pBuf = pCopy;
888 /* Somehow skb_copy ignores mac_len */
889 pBuf->mac_len = uMacLen;
890# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
891 /* Restore VLAN tag stripped by host hardware */
892 if (vlan_tx_tag_present(pBuf) && skb_headroom(pBuf) >= VLAN_ETH_HLEN)
893 {
894 uint8_t *pMac = (uint8_t*)skb_mac_header(pBuf);
895 struct vlan_ethhdr *pVHdr = (struct vlan_ethhdr *)(pMac - VLAN_HLEN);
896# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
897 memmove(pVHdr, pMac, ETH_ALEN * 2);
898# else
899 memmove(pVHdr, pMac, VLAN_ETH_ALEN * 2);
900# endif
901 pVHdr->h_vlan_proto = RT_H2N_U16(ETH_P_8021Q);
902 pVHdr->h_vlan_TCI = RT_H2N_U16(vlan_tx_tag_get(pBuf));
903 pBuf->mac_header -= VLAN_HLEN;
904 pBuf->mac_len += VLAN_HLEN;
905 }
906# endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) */
907
908# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
909 Log3(("vboxNetFltLinuxPacketHandler: skb copy len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
910 pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size, skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
911# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
912 Log4(("vboxNetFltLinuxPacketHandler: packet dump follows:\n%.*Rhxd\n", pBuf->len-pBuf->data_len, skb_mac_header(pBuf)));
913# endif
914# else
915 Log3(("vboxNetFltLinuxPacketHandler: skb copy len=%u data_len=%u truesize=%u next=%p nr_frags=%u tso_size=%u tso_seqs=%u frag_list=%p pkt_type=%x\n",
916 pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->tso_size, skb_shinfo(pBuf)->tso_segs, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
917# endif
918 }
919#endif
920
921#ifdef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
922 /* Forward it to the internal network. */
923 vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
924#else
925 /* Add the packet to transmit queue and schedule the bottom half. */
926 skb_queue_tail(&pThis->u.s.XmitQueue, pBuf);
927 schedule_work(&pThis->u.s.XmitTask);
928 Log4(("vboxNetFltLinuxPacketHandler: scheduled work %p for sk_buff %p\n",
929 &pThis->u.s.XmitTask, pBuf));
930#endif
931
932 /* It does not really matter what we return, it is ignored by the kernel. */
933 return 0;
934}
935
936/**
937 * Calculate the number of INTNETSEG segments the socket buffer will need.
938 *
939 * @returns Segment count.
940 * @param pBuf The socket buffer.
941 */
942DECLINLINE(unsigned) vboxNetFltLinuxCalcSGSegments(struct sk_buff *pBuf)
943{
944#ifdef VBOXNETFLT_SG_SUPPORT
945 unsigned cSegs = 1 + skb_shinfo(pBuf)->nr_frags;
946#else
947 unsigned cSegs = 1;
948#endif
949#ifdef PADD_RUNT_FRAMES_FROM_HOST
950 /* vboxNetFltLinuxSkBufToSG adds a padding segment if it's a runt. */
951 if (pBuf->len < 60)
952 cSegs++;
953#endif
954 return cSegs;
955}
956
957/**
958 * Destroy the intnet scatter / gather buffer created by
959 * vboxNetFltLinuxSkBufToSG.
960 */
961static void vboxNetFltLinuxDestroySG(PINTNETSG pSG)
962{
963#ifdef VBOXNETFLT_SG_SUPPORT
964 int i;
965
966 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
967 {
968 printk("kunmap(%p)\n", pSG->aSegs[i+1].pv);
969 kunmap(pSG->aSegs[i+1].pv);
970 }
971#endif
972 NOREF(pSG);
973}
974
975#ifdef LOG_ENABLED
976/**
977 * Logging helper.
978 */
979static void vboxNetFltDumpPacket(PINTNETSG pSG, bool fEgress, const char *pszWhere, int iIncrement)
980{
981 int i, offSeg;
982 uint8_t *pInt, *pExt;
983 static int iPacketNo = 1;
984 iPacketNo += iIncrement;
985 if (fEgress)
986 {
987 pExt = pSG->aSegs[0].pv;
988 pInt = pExt + 6;
989 }
990 else
991 {
992 pInt = pSG->aSegs[0].pv;
993 pExt = pInt + 6;
994 }
995 Log(("VBoxNetFlt: (int)%02x:%02x:%02x:%02x:%02x:%02x"
996 " %s (%s)%02x:%02x:%02x:%02x:%02x:%02x (%u bytes) packet #%u\n",
997 pInt[0], pInt[1], pInt[2], pInt[3], pInt[4], pInt[5],
998 fEgress ? "-->" : "<--", pszWhere,
999 pExt[0], pExt[1], pExt[2], pExt[3], pExt[4], pExt[5],
1000 pSG->cbTotal, iPacketNo));
1001 if (pSG->cSegsUsed == 1)
1002 {
1003 Log3(("%.*Rhxd\n", pSG->aSegs[0].cb, pSG->aSegs[0].pv));
1004 }
1005 else
1006 {
1007 for (i = 0, offSeg = 0; i < pSG->cSegsUsed; i++)
1008 {
1009 Log3(("-- segment %d at 0x%x (%d bytes) --\n%.*Rhxd\n",
1010 i, offSeg, pSG->aSegs[i].cb, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
1011 offSeg += pSG->aSegs[i].cb;
1012 }
1013 }
1014
1015}
1016#else
1017# define vboxNetFltDumpPacket(a, b, c, d) do {} while (0)
1018#endif
1019
1020#ifdef VBOXNETFLT_WITH_GSO_RECV
1021
1022/**
1023 * Worker for vboxNetFltLinuxForwardToIntNet that checks if we can forwards a
1024 * GSO socket buffer without having to segment it.
1025 *
1026 * @returns true on success, false if needs segmenting.
1027 * @param pThis The net filter instance.
1028 * @param pSkb The GSO socket buffer.
1029 * @param fSrc The source.
1030 * @param pGsoCtx Where to return the GSO context on success.
1031 */
1032static bool vboxNetFltLinuxCanForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc,
1033 PPDMNETWORKGSO pGsoCtx)
1034{
1035 PDMNETWORKGSOTYPE enmGsoType;
1036 uint16_t uEtherType;
1037 unsigned int cbTransport;
1038 unsigned int offTransport;
1039 unsigned int cbTransportHdr;
1040 unsigned uProtocol;
1041 union
1042 {
1043 RTNETIPV4 IPv4;
1044 RTNETIPV6 IPv6;
1045 RTNETTCP Tcp;
1046 uint8_t ab[40];
1047 uint16_t au16[40/2];
1048 uint32_t au32[40/4];
1049 } Buf;
1050
1051 /*
1052 * Check the GSO properties of the socket buffer and make sure it fits.
1053 */
1054 /** @todo Figure out how to handle SKB_GSO_TCP_ECN! */
1055 if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_type & ~(SKB_GSO_UDP | SKB_GSO_DODGY | SKB_GSO_TCPV6 | SKB_GSO_TCPV4) ))
1056 {
1057 Log5(("vboxNetFltLinuxCanForwardAsGso: gso_type=%#x\n", skb_shinfo(pSkb)->gso_type));
1058 return false;
1059 }
1060 if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_size < 1
1061 || pSkb->len > VBOX_MAX_GSO_SIZE ))
1062 {
1063 Log5(("vboxNetFltLinuxCanForwardAsGso: gso_size=%#x skb_len=%#x (max=%#x)\n", skb_shinfo(pSkb)->gso_size, pSkb->len, VBOX_MAX_GSO_SIZE));
1064 return false;
1065 }
1066 /*
1067 * It is possible to receive GSO packets from wire if GRO is enabled.
1068 */
1069 if (RT_UNLIKELY(fSrc & INTNETTRUNKDIR_WIRE))
1070 {
1071 Log5(("vboxNetFltLinuxCanForwardAsGso: fSrc=wire\n"));
1072#ifdef VBOXNETFLT_WITH_GRO
1073 /*
1074 * The packet came from the wire and the driver has already consumed
1075 * mac header. We need to restore it back.
1076 */
1077 pSkb->mac_len = skb_network_header(pSkb) - skb_mac_header(pSkb);
1078 skb_push(pSkb, pSkb->mac_len);
1079 Log5(("vboxNetFltLinuxCanForwardAsGso: mac_len=%d data=%p mac_header=%p network_header=%p\n",
1080 pSkb->mac_len, pSkb->data, skb_mac_header(pSkb), skb_network_header(pSkb)));
1081#else /* !VBOXNETFLT_WITH_GRO */
1082 /* Older kernels didn't have GRO. */
1083 return false;
1084#endif /* !VBOXNETFLT_WITH_GRO */
1085 }
1086 else
1087 {
1088 /*
1089 * skb_gso_segment does the following. Do we need to do it as well?
1090 */
1091#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1092 skb_reset_mac_header(pSkb);
1093 pSkb->mac_len = pSkb->network_header - pSkb->mac_header;
1094#else
1095 pSkb->mac.raw = pSkb->data;
1096 pSkb->mac_len = pSkb->nh.raw - pSkb->data;
1097#endif
1098 }
1099
1100 /*
1101 * Switch on the ethertype.
1102 */
1103 uEtherType = pSkb->protocol;
1104 if ( uEtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_VLAN)
1105 && pSkb->mac_len == sizeof(RTNETETHERHDR) + sizeof(uint32_t))
1106 {
1107 uint16_t const *puEtherType = skb_header_pointer(pSkb, sizeof(RTNETETHERHDR) + sizeof(uint16_t), sizeof(uint16_t), &Buf);
1108 if (puEtherType)
1109 uEtherType = *puEtherType;
1110 }
1111 switch (uEtherType)
1112 {
1113 case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4):
1114 {
1115 unsigned int cbHdr;
1116 PCRTNETIPV4 pIPv4 = (PCRTNETIPV4)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv4), &Buf);
1117 if (RT_UNLIKELY(!pIPv4))
1118 {
1119 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv4 hdr\n"));
1120 return false;
1121 }
1122
1123 cbHdr = pIPv4->ip_hl * 4;
1124 cbTransport = RT_N2H_U16(pIPv4->ip_len);
1125 if (RT_UNLIKELY( cbHdr < RTNETIPV4_MIN_LEN
1126 || cbHdr > cbTransport ))
1127 {
1128 Log5(("vboxNetFltLinuxCanForwardAsGso: invalid IPv4 lengths: ip_hl=%u ip_len=%u\n", pIPv4->ip_hl, RT_N2H_U16(pIPv4->ip_len)));
1129 return false;
1130 }
1131 cbTransport -= cbHdr;
1132 offTransport = pSkb->mac_len + cbHdr;
1133 uProtocol = pIPv4->ip_p;
1134 if (uProtocol == RTNETIPV4_PROT_TCP)
1135 enmGsoType = PDMNETWORKGSOTYPE_IPV4_TCP;
1136 else if (uProtocol == RTNETIPV4_PROT_UDP)
1137 enmGsoType = PDMNETWORKGSOTYPE_IPV4_UDP;
1138 else /** @todo IPv6: 4to6 tunneling */
1139 enmGsoType = PDMNETWORKGSOTYPE_INVALID;
1140 break;
1141 }
1142
1143 case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV6):
1144 {
1145 PCRTNETIPV6 pIPv6 = (PCRTNETIPV6)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv6), &Buf);
1146 if (RT_UNLIKELY(!pIPv6))
1147 {
1148 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv6 hdr\n"));
1149 return false;
1150 }
1151
1152 cbTransport = RT_N2H_U16(pIPv6->ip6_plen);
1153 offTransport = pSkb->mac_len + sizeof(RTNETIPV6);
1154 uProtocol = pIPv6->ip6_nxt;
1155 /** @todo IPv6: Dig our way out of the other headers. */
1156 if (uProtocol == RTNETIPV4_PROT_TCP)
1157 enmGsoType = PDMNETWORKGSOTYPE_IPV6_TCP;
1158 else if (uProtocol == RTNETIPV4_PROT_UDP)
1159 enmGsoType = PDMNETWORKGSOTYPE_IPV4_UDP;
1160 else
1161 enmGsoType = PDMNETWORKGSOTYPE_INVALID;
1162 break;
1163 }
1164
1165 default:
1166 Log5(("vboxNetFltLinuxCanForwardAsGso: uEtherType=%#x\n", RT_H2N_U16(uEtherType)));
1167 return false;
1168 }
1169
1170 if (enmGsoType == PDMNETWORKGSOTYPE_INVALID)
1171 {
1172 Log5(("vboxNetFltLinuxCanForwardAsGso: Unsupported protocol %d\n", uProtocol));
1173 return false;
1174 }
1175
1176 if (RT_UNLIKELY( offTransport + cbTransport <= offTransport
1177 || offTransport + cbTransport > pSkb->len
1178 || cbTransport < (uProtocol == RTNETIPV4_PROT_TCP ? RTNETTCP_MIN_LEN : RTNETUDP_MIN_LEN)) )
1179 {
1180 Log5(("vboxNetFltLinuxCanForwardAsGso: Bad transport length; off=%#x + cb=%#x => %#x; skb_len=%#x (%s)\n",
1181 offTransport, cbTransport, offTransport + cbTransport, pSkb->len, PDMNetGsoTypeName(enmGsoType) ));
1182 return false;
1183 }
1184
1185 /*
1186 * Check the TCP/UDP bits.
1187 */
1188 if (uProtocol == RTNETIPV4_PROT_TCP)
1189 {
1190 PCRTNETTCP pTcp = (PCRTNETTCP)skb_header_pointer(pSkb, offTransport, sizeof(Buf.Tcp), &Buf);
1191 if (RT_UNLIKELY(!pTcp))
1192 {
1193 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access TCP hdr\n"));
1194 return false;
1195 }
1196
1197 cbTransportHdr = pTcp->th_off * 4;
1198 pGsoCtx->cbHdrsSeg = offTransport + cbTransportHdr;
1199 if (RT_UNLIKELY( cbTransportHdr < RTNETTCP_MIN_LEN
1200 || cbTransportHdr > cbTransport
1201 || offTransport + cbTransportHdr >= UINT8_MAX
1202 || offTransport + cbTransportHdr >= pSkb->len ))
1203 {
1204 Log5(("vboxNetFltLinuxCanForwardAsGso: No space for TCP header; off=%#x cb=%#x skb_len=%#x\n", offTransport, cbTransportHdr, pSkb->len));
1205 return false;
1206 }
1207
1208 }
1209 else
1210 {
1211 Assert(uProtocol == RTNETIPV4_PROT_UDP);
1212 cbTransportHdr = sizeof(RTNETUDP);
1213 pGsoCtx->cbHdrsSeg = offTransport; /* Exclude UDP header */
1214 if (RT_UNLIKELY( offTransport + cbTransportHdr >= UINT8_MAX
1215 || offTransport + cbTransportHdr >= pSkb->len ))
1216 {
1217 Log5(("vboxNetFltLinuxCanForwardAsGso: No space for UDP header; off=%#x skb_len=%#x\n", offTransport, pSkb->len));
1218 return false;
1219 }
1220 }
1221
1222 /*
1223 * We're good, init the GSO context.
1224 */
1225 pGsoCtx->u8Type = enmGsoType;
1226 pGsoCtx->cbHdrsTotal = offTransport + cbTransportHdr;
1227 pGsoCtx->cbMaxSeg = skb_shinfo(pSkb)->gso_size;
1228 pGsoCtx->offHdr1 = pSkb->mac_len;
1229 pGsoCtx->offHdr2 = offTransport;
1230 pGsoCtx->u8Unused = 0;
1231
1232 return true;
1233}
1234
1235/**
1236 * Forward the socket buffer as a GSO internal network frame.
1237 *
1238 * @returns IPRT status code.
1239 * @param pThis The net filter instance.
1240 * @param pSkb The GSO socket buffer.
1241 * @param fSrc The source.
1242 * @param pGsoCtx Where to return the GSO context on success.
1243 */
1244static int vboxNetFltLinuxForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
1245{
1246 int rc;
1247 unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pSkb);
1248 if (RT_LIKELY(cSegs <= MAX_SKB_FRAGS + 1))
1249 {
1250 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
1251 if (RT_LIKELY(pSG))
1252 {
1253 vboxNetFltLinuxSkBufToSG(pThis, pSkb, pSG, cSegs, fSrc, pGsoCtx);
1254
1255 vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
1256 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
1257
1258 vboxNetFltLinuxDestroySG(pSG);
1259 rc = VINF_SUCCESS;
1260 }
1261 else
1262 {
1263 Log(("VBoxNetFlt: Dropping the sk_buff (failure case).\n"));
1264 rc = VERR_NO_MEMORY;
1265 }
1266 }
1267 else
1268 {
1269 Log(("VBoxNetFlt: Bad sk_buff? cSegs=%#x.\n", cSegs));
1270 rc = VERR_INTERNAL_ERROR_3;
1271 }
1272
1273 Log4(("VBoxNetFlt: Dropping the sk_buff.\n"));
1274 dev_kfree_skb(pSkb);
1275 return rc;
1276}
1277
1278#endif /* VBOXNETFLT_WITH_GSO_RECV */
1279
1280/**
1281 * Worker for vboxNetFltLinuxForwardToIntNet.
1282 *
1283 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
1284 * @param pThis The net filter instance.
1285 * @param pBuf The socket buffer.
1286 * @param fSrc The source.
1287 */
1288static int vboxNetFltLinuxForwardSegment(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, uint32_t fSrc)
1289{
1290 int rc;
1291 unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pBuf);
1292 if (cSegs <= MAX_SKB_FRAGS + 1)
1293 {
1294 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
1295 if (RT_LIKELY(pSG))
1296 {
1297 if (fSrc & INTNETTRUNKDIR_WIRE)
1298 {
1299 /*
1300 * The packet came from wire, ethernet header was removed by device driver.
1301 * Restore it using mac_len field. This takes into account VLAN headers too.
1302 */
1303 skb_push(pBuf, pBuf->mac_len);
1304 }
1305
1306 vboxNetFltLinuxSkBufToSG(pThis, pBuf, pSG, cSegs, fSrc, NULL /*pGsoCtx*/);
1307
1308 vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
1309 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
1310
1311 vboxNetFltLinuxDestroySG(pSG);
1312 rc = VINF_SUCCESS;
1313 }
1314 else
1315 {
1316 Log(("VBoxNetFlt: Failed to allocate SG buffer.\n"));
1317 rc = VERR_NO_MEMORY;
1318 }
1319 }
1320 else
1321 {
1322 Log(("VBoxNetFlt: Bad sk_buff? cSegs=%#x.\n", cSegs));
1323 rc = VERR_INTERNAL_ERROR_3;
1324 }
1325
1326 Log4(("VBoxNetFlt: Dropping the sk_buff.\n"));
1327 dev_kfree_skb(pBuf);
1328 return rc;
1329}
1330
1331/**
1332 *
1333 * @param pBuf The socket buffer. This is consumed by this function.
1334 */
1335static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf)
1336{
1337 uint32_t fSrc = pBuf->pkt_type == PACKET_OUTGOING ? INTNETTRUNKDIR_HOST : INTNETTRUNKDIR_WIRE;
1338
1339#ifdef VBOXNETFLT_WITH_GSO
1340 if (skb_is_gso(pBuf))
1341 {
1342 PDMNETWORKGSO GsoCtx;
1343 Log3(("vboxNetFltLinuxForwardToIntNet: skb len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x ip_summed=%d\n",
1344 pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size, skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, pBuf->ip_summed));
1345# ifdef VBOXNETFLT_WITH_GSO_RECV
1346 if ( (skb_shinfo(pBuf)->gso_type & (SKB_GSO_UDP | SKB_GSO_TCPV6 | SKB_GSO_TCPV4))
1347 && vboxNetFltLinuxCanForwardAsGso(pThis, pBuf, fSrc, &GsoCtx) )
1348 vboxNetFltLinuxForwardAsGso(pThis, pBuf, fSrc, &GsoCtx);
1349 else
1350# endif
1351 {
1352 /* Need to segment the packet */
1353 struct sk_buff *pNext;
1354 struct sk_buff *pSegment = skb_gso_segment(pBuf, 0 /*supported features*/);
1355 if (IS_ERR(pSegment))
1356 {
1357 dev_kfree_skb(pBuf);
1358 LogRel(("VBoxNetFlt: Failed to segment a packet (%d).\n", PTR_ERR(pSegment)));
1359 return;
1360 }
1361
1362 for (; pSegment; pSegment = pNext)
1363 {
1364 Log3(("vboxNetFltLinuxForwardToIntNet: segment len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
1365 pSegment->len, pSegment->data_len, pSegment->truesize, pSegment->next, skb_shinfo(pSegment)->nr_frags, skb_shinfo(pSegment)->gso_size, skb_shinfo(pSegment)->gso_segs, skb_shinfo(pSegment)->gso_type, skb_shinfo(pSegment)->frag_list, pSegment->pkt_type));
1366 pNext = pSegment->next;
1367 pSegment->next = 0;
1368 vboxNetFltLinuxForwardSegment(pThis, pSegment, fSrc);
1369 }
1370 dev_kfree_skb(pBuf);
1371 }
1372 }
1373 else
1374#endif /* VBOXNETFLT_WITH_GSO */
1375 {
1376 if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
1377 {
1378#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
1379 /*
1380 * Try to work around the problem with CentOS 4.7 and 5.2 (2.6.9
1381 * and 2.6.18 kernels), they pass wrong 'h' pointer down. We take IP
1382 * header length from the header itself and reconstruct 'h' pointer
1383 * to TCP (or whatever) header.
1384 */
1385 unsigned char *tmp = pBuf->h.raw;
1386 if (pBuf->h.raw == pBuf->nh.raw && pBuf->protocol == htons(ETH_P_IP))
1387 pBuf->h.raw = pBuf->nh.raw + pBuf->nh.iph->ihl * 4;
1388#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
1389 if (VBOX_SKB_CHECKSUM_HELP(pBuf))
1390 {
1391 LogRel(("VBoxNetFlt: Failed to compute checksum, dropping the packet.\n"));
1392 dev_kfree_skb(pBuf);
1393 return;
1394 }
1395#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
1396 /* Restore the original (wrong) pointer. */
1397 pBuf->h.raw = tmp;
1398#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
1399 }
1400 vboxNetFltLinuxForwardSegment(pThis, pBuf, fSrc);
1401 }
1402}
1403
1404#ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
1405/**
1406 * Work queue handler that forwards the socket buffers queued by
1407 * vboxNetFltLinuxPacketHandler to the internal network.
1408 *
1409 * @param pWork The work queue.
1410 */
1411# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1412static void vboxNetFltLinuxXmitTask(struct work_struct *pWork)
1413# else
1414static void vboxNetFltLinuxXmitTask(void *pWork)
1415# endif
1416{
1417 PVBOXNETFLTINS pThis = VBOX_FLT_XT_TO_INST(pWork);
1418 struct sk_buff *pBuf;
1419
1420 Log4(("vboxNetFltLinuxXmitTask: Got work %p.\n", pWork));
1421
1422 /*
1423 * Active? Retain the instance and increment the busy counter.
1424 */
1425 if (vboxNetFltTryRetainBusyActive(pThis))
1426 {
1427 while ((pBuf = skb_dequeue(&pThis->u.s.XmitQueue)) != NULL)
1428 vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
1429
1430 vboxNetFltRelease(pThis, true /* fBusy */);
1431 }
1432 else
1433 {
1434 /** @todo Shouldn't we just drop the packets here? There is little point in
1435 * making them accumulate when the VM is paused and it'll only waste
1436 * kernel memory anyway... Hmm. maybe wait a short while (2-5 secs)
1437 * before start draining the packets (goes for the intnet ring buf
1438 * too)? */
1439 }
1440}
1441#endif /* !VBOXNETFLT_LINUX_NO_XMIT_QUEUE */
1442
1443/**
1444 * Reports the GSO capabilities of the hardware NIC.
1445 *
1446 * @param pThis The net filter instance. The caller hold a
1447 * reference to this.
1448 */
1449static void vboxNetFltLinuxReportNicGsoCapabilities(PVBOXNETFLTINS pThis)
1450{
1451#ifdef VBOXNETFLT_WITH_GSO_XMIT_WIRE
1452 if (vboxNetFltTryRetainBusyNotDisconnected(pThis))
1453 {
1454 struct net_device *pDev;
1455 PINTNETTRUNKSWPORT pSwitchPort;
1456 unsigned int fFeatures;
1457
1458 RTSpinlockAcquire(pThis->hSpinlock);
1459
1460 pSwitchPort = pThis->pSwitchPort; /* this doesn't need to be here, but it doesn't harm. */
1461 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
1462 if (pDev)
1463 fFeatures = pDev->features;
1464 else
1465 fFeatures = 0;
1466
1467 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1468
1469 if (pThis->pSwitchPort)
1470 {
1471 /* Set/update the GSO capabilities of the NIC. */
1472 uint32_t fGsoCapabilites = 0;
1473 if (fFeatures & NETIF_F_TSO)
1474 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP);
1475 if (fFeatures & NETIF_F_TSO6)
1476 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP);
1477# if 0 /** @todo GSO: Test UDP offloading (UFO) on linux. */
1478 if (fFeatures & NETIF_F_UFO)
1479 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_UDP);
1480 if (fFeatures & NETIF_F_UFO)
1481 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_UDP);
1482# endif
1483 Log3(("vboxNetFltLinuxReportNicGsoCapabilities: reporting wire %s%s%s%s\n",
1484 (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP)) ? "tso " : "",
1485 (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP)) ? "tso6 " : "",
1486 (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_UDP)) ? "ufo " : "",
1487 (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_UDP)) ? "ufo6 " : ""));
1488 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, fGsoCapabilites, INTNETTRUNKDIR_WIRE);
1489 }
1490
1491 vboxNetFltRelease(pThis, true /*fBusy*/);
1492 }
1493#endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE */
1494}
1495
1496/**
1497 * Helper that determines whether the host (ignoreing us) is operating the
1498 * interface in promiscuous mode or not.
1499 */
1500static bool vboxNetFltLinuxPromiscuous(PVBOXNETFLTINS pThis)
1501{
1502 bool fRc = false;
1503 struct net_device * pDev = vboxNetFltLinuxRetainNetDev(pThis);
1504 if (pDev)
1505 {
1506 fRc = !!(pDev->promiscuity - (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet) & 1));
1507 LogFlow(("vboxNetFltPortOsIsPromiscuous: returns %d, pDev->promiscuity=%d, fPromiscuousSet=%d\n",
1508 fRc, pDev->promiscuity, pThis->u.s.fPromiscuousSet));
1509 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1510 }
1511 return fRc;
1512}
1513
1514#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
1515/**
1516 * Helper for detecting TAP devices.
1517 */
1518static bool vboxNetFltIsTapDevice(PVBOXNETFLTINS pThis, struct net_device *pDev)
1519{
1520 if (pDev->ethtool_ops && pDev->ethtool_ops->get_drvinfo)
1521 {
1522 struct ethtool_drvinfo Info;
1523
1524 memset(&Info, 0, sizeof(Info));
1525 Info.cmd = ETHTOOL_GDRVINFO;
1526 pDev->ethtool_ops->get_drvinfo(pDev, &Info);
1527 Log3(("vboxNetFltIsTapDevice: driver=%s version=%s bus_info=%s\n",
1528 Info.driver, Info.version, Info.bus_info));
1529
1530 return !strncmp(Info.driver, "tun", 4)
1531 && !strncmp(Info.bus_info, "tap", 4);
1532 }
1533
1534 return false;
1535}
1536
1537/**
1538 * Helper for updating the link state of TAP devices.
1539 * Only TAP devices are affected.
1540 */
1541static void vboxNetFltSetTapLinkState(PVBOXNETFLTINS pThis, struct net_device *pDev, bool fLinkUp)
1542{
1543 if (vboxNetFltIsTapDevice(pThis, pDev))
1544 {
1545 Log3(("vboxNetFltSetTapLinkState: bringing %s tap device link state\n",
1546 fLinkUp ? "up" : "down"));
1547 netif_tx_lock_bh(pDev);
1548 if (fLinkUp)
1549 netif_carrier_on(pDev);
1550 else
1551 netif_carrier_off(pDev);
1552 netif_tx_unlock_bh(pDev);
1553 }
1554}
1555#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) */
1556DECLINLINE(void) vboxNetFltSetTapLinkState(PVBOXNETFLTINS pThis, struct net_device *pDev, bool fLinkUp)
1557{
1558 /* Nothing to do for pre-2.6.36 kernels. */
1559}
1560#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) */
1561
1562/**
1563 * Internal worker for vboxNetFltLinuxNotifierCallback.
1564 *
1565 * @returns VBox status code.
1566 * @param pThis The instance.
1567 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
1568 * flood the release log.
1569 */
1570static int vboxNetFltLinuxAttachToInterface(PVBOXNETFLTINS pThis, struct net_device *pDev)
1571{
1572 LogFlow(("vboxNetFltLinuxAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
1573
1574 /*
1575 * Retain and store the device.
1576 */
1577 dev_hold(pDev);
1578
1579 RTSpinlockAcquire(pThis->hSpinlock);
1580 ASMAtomicUoWritePtr(&pThis->u.s.pDev, pDev);
1581 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1582
1583 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) retained. ref=%d\n",
1584 pDev, pDev->name,
1585#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
1586 netdev_refcnt_read(pDev)
1587#else
1588 atomic_read(&pDev->refcnt)
1589#endif
1590 ));
1591 Log(("vboxNetFltLinuxAttachToInterface: Got pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
1592 pDev, pThis, ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *)));
1593
1594 /* Get the mac address while we still have a valid net_device reference. */
1595 memcpy(&pThis->u.s.MacAddr, pDev->dev_addr, sizeof(pThis->u.s.MacAddr));
1596 /* Initialize MTU */
1597 pThis->u.s.cbMtu = pDev->mtu;
1598
1599 /*
1600 * Install a packet filter for this device with a protocol wildcard (ETH_P_ALL).
1601 */
1602 pThis->u.s.PacketType.type = __constant_htons(ETH_P_ALL);
1603 pThis->u.s.PacketType.dev = pDev;
1604 pThis->u.s.PacketType.func = vboxNetFltLinuxPacketHandler;
1605 dev_add_pack(&pThis->u.s.PacketType);
1606 ASMAtomicUoWriteBool(&pThis->u.s.fPacketHandler, true);
1607 Log(("vboxNetFltLinuxAttachToInterface: this=%p: Packet handler installed.\n", pThis));
1608
1609#ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
1610 vboxNetFltLinuxHookDev(pThis, pDev);
1611#endif
1612
1613 /*
1614 * If attaching to TAP interface we need to bring the link state up
1615 * starting from 2.6.36 kernel.
1616 */
1617 vboxNetFltSetTapLinkState(pThis, pDev, true);
1618
1619 /*
1620 * Set indicators that require the spinlock. Be abit paranoid about racing
1621 * the device notification handle.
1622 */
1623 RTSpinlockAcquire(pThis->hSpinlock);
1624 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
1625 if (pDev)
1626 {
1627 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
1628 ASMAtomicUoWriteBool(&pThis->u.s.fRegistered, true);
1629 pDev = NULL; /* don't dereference it */
1630 }
1631 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1632
1633 /*
1634 * If the above succeeded report GSO capabilities, if not undo and
1635 * release the device.
1636 */
1637 if (!pDev)
1638 {
1639 Assert(pThis->pSwitchPort);
1640 if (vboxNetFltTryRetainBusyNotDisconnected(pThis))
1641 {
1642 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
1643 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
1644 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltLinuxPromiscuous(pThis));
1645 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
1646 vboxNetFltRelease(pThis, true /*fBusy*/);
1647 }
1648 }
1649 else
1650 {
1651#ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
1652 vboxNetFltLinuxUnhookDev(pThis, pDev);
1653#endif
1654 RTSpinlockAcquire(pThis->hSpinlock);
1655 ASMAtomicUoWriteNullPtr(&pThis->u.s.pDev);
1656 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1657 dev_put(pDev);
1658 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) released. ref=%d\n",
1659 pDev, pDev->name,
1660#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
1661 netdev_refcnt_read(pDev)
1662#else
1663 atomic_read(&pDev->refcnt)
1664#endif
1665 ));
1666 }
1667
1668 LogRel(("VBoxNetFlt: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.MacAddr), &pThis->u.s.MacAddr));
1669 return VINF_SUCCESS;
1670}
1671
1672
1673static int vboxNetFltLinuxUnregisterDevice(PVBOXNETFLTINS pThis, struct net_device *pDev)
1674{
1675 bool fRegistered;
1676 Assert(!pThis->fDisconnectedFromHost);
1677
1678#ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
1679 vboxNetFltLinuxUnhookDev(pThis, pDev);
1680#endif
1681
1682 if (ASMAtomicCmpXchgBool(&pThis->u.s.fPacketHandler, false, true))
1683 {
1684 dev_remove_pack(&pThis->u.s.PacketType);
1685 Log(("vboxNetFltLinuxUnregisterDevice: this=%p: packet handler removed.\n", pThis));
1686 }
1687
1688 RTSpinlockAcquire(pThis->hSpinlock);
1689 fRegistered = ASMAtomicXchgBool(&pThis->u.s.fRegistered, false);
1690 if (fRegistered)
1691 {
1692 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
1693 ASMAtomicUoWriteNullPtr(&pThis->u.s.pDev);
1694 }
1695 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1696
1697 if (fRegistered)
1698 {
1699#ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
1700 skb_queue_purge(&pThis->u.s.XmitQueue);
1701#endif
1702 Log(("vboxNetFltLinuxUnregisterDevice: this=%p: xmit queue purged.\n", pThis));
1703 Log(("vboxNetFltLinuxUnregisterDevice: Device %p(%s) released. ref=%d\n",
1704 pDev, pDev->name,
1705#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
1706 netdev_refcnt_read(pDev)
1707#else
1708 atomic_read(&pDev->refcnt)
1709#endif
1710 ));
1711 dev_put(pDev);
1712 }
1713
1714 return NOTIFY_OK;
1715}
1716
1717static int vboxNetFltLinuxDeviceIsUp(PVBOXNETFLTINS pThis, struct net_device *pDev)
1718{
1719 /* Check if we are not suspended and promiscuous mode has not been set. */
1720 if ( pThis->enmTrunkState == INTNETTRUNKIFSTATE_ACTIVE
1721 && !ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
1722 {
1723 /* Note that there is no need for locking as the kernel got hold of the lock already. */
1724 dev_set_promiscuity(pDev, 1);
1725 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, true);
1726 Log(("vboxNetFltLinuxDeviceIsUp: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1727 }
1728 else
1729 Log(("vboxNetFltLinuxDeviceIsUp: no need to enable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1730 return NOTIFY_OK;
1731}
1732
1733static int vboxNetFltLinuxDeviceGoingDown(PVBOXNETFLTINS pThis, struct net_device *pDev)
1734{
1735 /* Undo promiscuous mode if we has set it. */
1736 if (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
1737 {
1738 /* Note that there is no need for locking as the kernel got hold of the lock already. */
1739 dev_set_promiscuity(pDev, -1);
1740 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, false);
1741 Log(("vboxNetFltLinuxDeviceGoingDown: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1742 }
1743 else
1744 Log(("vboxNetFltLinuxDeviceGoingDown: no need to disable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1745 return NOTIFY_OK;
1746}
1747
1748/**
1749 * Callback for listening to MTU change event.
1750 *
1751 * We need to track changes of host's inteface MTU to discard over-sized frames
1752 * coming from the internal network as they may hang the TX queue of host's
1753 * adapter.
1754 *
1755 * @returns NOTIFY_OK
1756 * @param pThis The netfilter instance.
1757 * @param pDev Pointer to device structure of host's interface.
1758 */
1759static int vboxNetFltLinuxDeviceMtuChange(PVBOXNETFLTINS pThis, struct net_device *pDev)
1760{
1761 ASMAtomicWriteU32(&pThis->u.s.cbMtu, pDev->mtu);
1762 Log(("vboxNetFltLinuxDeviceMtuChange: set MTU for %s to %d\n", pThis->szName, pDev->mtu));
1763 return NOTIFY_OK;
1764}
1765
1766#ifdef LOG_ENABLED
1767/** Stringify the NETDEV_XXX constants. */
1768static const char *vboxNetFltLinuxGetNetDevEventName(unsigned long ulEventType)
1769{
1770 const char *pszEvent = "NETDRV_<unknown>";
1771 switch (ulEventType)
1772 {
1773 case NETDEV_REGISTER: pszEvent = "NETDEV_REGISTER"; break;
1774 case NETDEV_UNREGISTER: pszEvent = "NETDEV_UNREGISTER"; break;
1775 case NETDEV_UP: pszEvent = "NETDEV_UP"; break;
1776 case NETDEV_DOWN: pszEvent = "NETDEV_DOWN"; break;
1777 case NETDEV_REBOOT: pszEvent = "NETDEV_REBOOT"; break;
1778 case NETDEV_CHANGENAME: pszEvent = "NETDEV_CHANGENAME"; break;
1779 case NETDEV_CHANGE: pszEvent = "NETDEV_CHANGE"; break;
1780 case NETDEV_CHANGEMTU: pszEvent = "NETDEV_CHANGEMTU"; break;
1781 case NETDEV_CHANGEADDR: pszEvent = "NETDEV_CHANGEADDR"; break;
1782 case NETDEV_GOING_DOWN: pszEvent = "NETDEV_GOING_DOWN"; break;
1783# ifdef NETDEV_FEAT_CHANGE
1784 case NETDEV_FEAT_CHANGE: pszEvent = "NETDEV_FEAT_CHANGE"; break;
1785# endif
1786 }
1787 return pszEvent;
1788}
1789#endif /* LOG_ENABLED */
1790
1791/**
1792 * Callback for listening to netdevice events.
1793 *
1794 * This works the rediscovery, clean up on unregistration, promiscuity on
1795 * up/down, and GSO feature changes from ethtool.
1796 *
1797 * @returns NOTIFY_OK
1798 * @param self Pointer to our notifier registration block.
1799 * @param ulEventType The event.
1800 * @param ptr Event specific, but it is usually the device it
1801 * relates to.
1802 */
1803static int vboxNetFltLinuxNotifierCallback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
1804
1805{
1806 PVBOXNETFLTINS pThis = VBOX_FLT_NB_TO_INST(self);
1807#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
1808 struct net_device *pDev = netdev_notifier_info_to_dev(ptr);
1809#else
1810 struct net_device *pDev = (struct net_device *)ptr;
1811#endif
1812 int rc = NOTIFY_OK;
1813
1814 Log(("VBoxNetFlt: got event %s(0x%lx) on %s, pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
1815 vboxNetFltLinuxGetNetDevEventName(ulEventType), ulEventType, pDev->name, pDev, pThis, ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *)));
1816 if ( ulEventType == NETDEV_REGISTER
1817 && !strcmp(pDev->name, pThis->szName))
1818 {
1819 vboxNetFltLinuxAttachToInterface(pThis, pDev);
1820 }
1821 else
1822 {
1823 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
1824 if (pDev == ptr)
1825 {
1826 switch (ulEventType)
1827 {
1828 case NETDEV_UNREGISTER:
1829 rc = vboxNetFltLinuxUnregisterDevice(pThis, pDev);
1830 break;
1831 case NETDEV_UP:
1832 rc = vboxNetFltLinuxDeviceIsUp(pThis, pDev);
1833 break;
1834 case NETDEV_GOING_DOWN:
1835 rc = vboxNetFltLinuxDeviceGoingDown(pThis, pDev);
1836 break;
1837 case NETDEV_CHANGEMTU:
1838 rc = vboxNetFltLinuxDeviceMtuChange(pThis, pDev);
1839 break;
1840 case NETDEV_CHANGENAME:
1841 break;
1842#ifdef NETDEV_FEAT_CHANGE
1843 case NETDEV_FEAT_CHANGE:
1844 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
1845 break;
1846#endif
1847 }
1848 }
1849 }
1850
1851 return rc;
1852}
1853
1854bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
1855{
1856 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
1857}
1858
1859int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
1860{
1861 struct net_device * pDev;
1862 int err;
1863 int rc = VINF_SUCCESS;
1864 NOREF(pvIfData);
1865
1866 LogFlow(("vboxNetFltPortOsXmit: pThis=%p (%s)\n", pThis, pThis->szName));
1867
1868 pDev = vboxNetFltLinuxRetainNetDev(pThis);
1869 if (pDev)
1870 {
1871 /*
1872 * Create a sk_buff for the gather list and push it onto the wire.
1873 */
1874 if (fDst & INTNETTRUNKDIR_WIRE)
1875 {
1876 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, true);
1877 if (pBuf)
1878 {
1879 vboxNetFltDumpPacket(pSG, true, "wire", 1);
1880 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
1881 Log4(("vboxNetFltPortOsXmit: dev_queue_xmit(%p)\n", pBuf));
1882 err = dev_queue_xmit(pBuf);
1883 if (err)
1884 rc = RTErrConvertFromErrno(err);
1885 }
1886 else
1887 rc = VERR_NO_MEMORY;
1888 }
1889
1890 /*
1891 * Create a sk_buff for the gather list and push it onto the host stack.
1892 */
1893 if (fDst & INTNETTRUNKDIR_HOST)
1894 {
1895 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, false);
1896 if (pBuf)
1897 {
1898 vboxNetFltDumpPacket(pSG, true, "host", (fDst & INTNETTRUNKDIR_WIRE) ? 0 : 1);
1899 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
1900 Log4(("vboxNetFltPortOsXmit: netif_rx_ni(%p)\n", pBuf));
1901 err = netif_rx_ni(pBuf);
1902 if (err)
1903 rc = RTErrConvertFromErrno(err);
1904 }
1905 else
1906 rc = VERR_NO_MEMORY;
1907 }
1908
1909 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1910 }
1911
1912 return rc;
1913}
1914
1915
1916void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1917{
1918 struct net_device * pDev;
1919
1920 LogFlow(("vboxNetFltPortOsSetActive: pThis=%p (%s), fActive=%s, fDisablePromiscuous=%s\n",
1921 pThis, pThis->szName, fActive?"true":"false",
1922 pThis->fDisablePromiscuous?"true":"false"));
1923
1924 if (pThis->fDisablePromiscuous)
1925 return;
1926
1927 pDev = vboxNetFltLinuxRetainNetDev(pThis);
1928 if (pDev)
1929 {
1930 /*
1931 * This api is a bit weird, the best reference is the code.
1932 *
1933 * Also, we have a bit or race conditions wrt the maintenance of
1934 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1935 */
1936#ifdef LOG_ENABLED
1937 u_int16_t fIf;
1938 unsigned const cPromiscBefore = pDev->promiscuity;
1939#endif
1940 if (fActive)
1941 {
1942 Assert(!pThis->u.s.fPromiscuousSet);
1943
1944 rtnl_lock();
1945 dev_set_promiscuity(pDev, 1);
1946 rtnl_unlock();
1947 pThis->u.s.fPromiscuousSet = true;
1948 Log(("vboxNetFltPortOsSetActive: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1949 }
1950 else
1951 {
1952 if (pThis->u.s.fPromiscuousSet)
1953 {
1954 rtnl_lock();
1955 dev_set_promiscuity(pDev, -1);
1956 rtnl_unlock();
1957 Log(("vboxNetFltPortOsSetActive: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1958 }
1959 pThis->u.s.fPromiscuousSet = false;
1960
1961#ifdef LOG_ENABLED
1962 fIf = dev_get_flags(pDev);
1963 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, pDev->promiscuity));
1964#endif
1965 }
1966
1967 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1968 }
1969}
1970
1971
1972int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1973{
1974 /*
1975 * Remove packet handler when we get disconnected from internal switch as
1976 * we don't want the handler to forward packets to disconnected switch.
1977 */
1978 if (ASMAtomicCmpXchgBool(&pThis->u.s.fPacketHandler, false, true))
1979 {
1980 dev_remove_pack(&pThis->u.s.PacketType);
1981 Log(("vboxNetFltOsDisconnectIt: this=%p: Packet handler removed.\n", pThis));
1982 }
1983 return VINF_SUCCESS;
1984}
1985
1986
1987int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1988{
1989 /*
1990 * Report the GSO capabilities of the host and device (if connected).
1991 * Note! No need to mark ourselves busy here.
1992 */
1993 /** @todo duplicate work here now? Attach */
1994#if defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
1995 Log3(("vboxNetFltOsConnectIt: reporting host tso tso6 ufo\n"));
1996 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort,
1997 0
1998 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP)
1999 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP)
2000 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_UDP)
2001# if 0 /** @todo GSO: Test UDP offloading (UFO) on linux. */
2002 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_UDP)
2003# endif
2004 , INTNETTRUNKDIR_HOST);
2005
2006#endif
2007 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
2008
2009 return VINF_SUCCESS;
2010}
2011
2012
2013void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
2014{
2015 struct net_device *pDev;
2016 bool fRegistered;
2017
2018#ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
2019 vboxNetFltLinuxUnhookDev(pThis, NULL);
2020#endif
2021
2022 /** @todo This code may race vboxNetFltLinuxUnregisterDevice (very very
2023 * unlikely, but none the less). Since it doesn't actually update the
2024 * state (just reads it), it is likely to panic in some interesting
2025 * ways. */
2026
2027 RTSpinlockAcquire(pThis->hSpinlock);
2028 pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
2029 fRegistered = ASMAtomicXchgBool(&pThis->u.s.fRegistered, false);
2030 RTSpinlockReleaseNoInts(pThis->hSpinlock);
2031
2032 if (fRegistered)
2033 {
2034 vboxNetFltSetTapLinkState(pThis, pDev, false);
2035
2036#ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
2037 skb_queue_purge(&pThis->u.s.XmitQueue);
2038#endif
2039 Log(("vboxNetFltOsDeleteInstance: this=%p: xmit queue purged.\n", pThis));
2040 Log(("vboxNetFltOsDeleteInstance: Device %p(%s) released. ref=%d\n",
2041 pDev, pDev->name,
2042#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
2043 netdev_refcnt_read(pDev)
2044#else
2045 atomic_read(&pDev->refcnt)
2046#endif
2047 ));
2048 dev_put(pDev);
2049 }
2050 Log(("vboxNetFltOsDeleteInstance: this=%p: Notifier removed.\n", pThis));
2051 unregister_netdevice_notifier(&pThis->u.s.Notifier);
2052 module_put(THIS_MODULE);
2053}
2054
2055
2056int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
2057{
2058 int err;
2059 NOREF(pvContext);
2060
2061 pThis->u.s.Notifier.notifier_call = vboxNetFltLinuxNotifierCallback;
2062 err = register_netdevice_notifier(&pThis->u.s.Notifier);
2063 if (err)
2064 return VERR_INTNET_FLT_IF_FAILED;
2065 if (!pThis->u.s.fRegistered)
2066 {
2067 unregister_netdevice_notifier(&pThis->u.s.Notifier);
2068 LogRel(("VBoxNetFlt: failed to find %s.\n", pThis->szName));
2069 return VERR_INTNET_FLT_IF_NOT_FOUND;
2070 }
2071
2072 Log(("vboxNetFltOsInitInstance: this=%p: Notifier installed.\n", pThis));
2073 if ( pThis->fDisconnectedFromHost
2074 || !try_module_get(THIS_MODULE))
2075 return VERR_INTNET_FLT_IF_FAILED;
2076
2077 return VINF_SUCCESS;
2078}
2079
2080int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
2081{
2082 /*
2083 * Init the linux specific members.
2084 */
2085 ASMAtomicUoWriteNullPtr(&pThis->u.s.pDev);
2086 pThis->u.s.fRegistered = false;
2087 pThis->u.s.fPromiscuousSet = false;
2088 pThis->u.s.fPacketHandler = false;
2089 memset(&pThis->u.s.PacketType, 0, sizeof(pThis->u.s.PacketType));
2090#ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
2091 skb_queue_head_init(&pThis->u.s.XmitQueue);
2092# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
2093 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask);
2094# else
2095 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask, &pThis->u.s.XmitTask);
2096# endif
2097#endif
2098
2099 return VINF_SUCCESS;
2100}
2101
2102
2103void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
2104{
2105 NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
2106}
2107
2108
2109int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **pvIfData)
2110{
2111 /* Nothing to do */
2112 NOREF(pThis); NOREF(pvIf); NOREF(pvIfData);
2113 return VINF_SUCCESS;
2114}
2115
2116
2117int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
2118{
2119 /* Nothing to do */
2120 NOREF(pThis); NOREF(pvIfData);
2121 return VINF_SUCCESS;
2122}
2123
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette