VirtualBox

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

Last change on this file since 64206 was 64206, checked in by vboxsync, 8 years ago

NetFlt/Linux (bugref:8599) RHEL build fix for r111185

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