VirtualBox

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

Last change on this file since 100862 was 100862, checked in by vboxsync, 16 months ago

Linux: Host drivers: VBoxNetFlt: Build fix for kernel 6.4.10, bugref:10441, bugref:10482.

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