VirtualBox

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

Last change on this file since 97405 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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