VirtualBox

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

Last change on this file since 106401 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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