VirtualBox

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

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

VBoxNetFlt/linux: Simplify check for IPv6 link-local addresses that
should work with older kernels.

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