VirtualBox

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

Last change on this file since 54461 was 54461, checked in by vboxsync, 10 years ago

HostDrivers/VBoxNetFlt: Linux 4 compile fix

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

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