VirtualBox

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

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

VBoxNetFlt/linux: Instead of fighting an uphill battle with ifdefs to
loop over all net devices, abuse/co-opt register_netdevice_notifier()
to do that for us.

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