VirtualBox

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

Last change on this file since 85701 was 85701, checked in by vboxsync, 4 years ago

IPRT,lnx-kmods: Added VBOX_RHEL_MAJ_PREREQ and RTLNX_SUSE_MAJ_PREREQ to version.h. Started using them.

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