VirtualBox

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

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

VBoxNetFlt/linux: dev_net() appeared in 2.6.26

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