VirtualBox

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

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

SUPDrv,VBoxNetFlt: More EFLAGS preservation fixes for linux.

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