VirtualBox

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

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

IPRT,lnx-kmods: s/RTLNX_RHEL_PREREQ/RTLNX_RHEL_MIN/; added RTLNX_RHEL_MAX and RTLNX_RHEL_RANGE. Use them instead of the RHEL_XXXX defines everywhere.

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