VirtualBox

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

Last change on this file since 104760 was 104046, checked in by vboxsync, 10 months ago

Linux: VBoxNetFlt: Introduce initial support for kernel 6.9, bugref:10630.

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