VirtualBox

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

Last change on this file since 56315 was 56315, checked in by vboxsync, 9 years ago

whitespace

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