VirtualBox

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

Last change on this file since 28281 was 28281, checked in by vboxsync, 15 years ago

VBoxNetFlt/linux: 2.6.22 that is

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.7 KB
Line 
1/* $Id: VBoxNetFlt-linux.c 28281 2010-04-14 06:40:48Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Linux Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include "the-linux-kernel.h"
26#include "version-generated.h"
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/rtnetlink.h>
30#include <linux/miscdevice.h>
31#include <linux/ip.h>
32
33#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
34#include <VBox/log.h>
35#include <VBox/err.h>
36#include <VBox/intnetinline.h>
37#include <VBox/pdmnetinline.h>
38#include <VBox/param.h>
39#include <iprt/alloca.h>
40#include <iprt/assert.h>
41#include <iprt/spinlock.h>
42#include <iprt/semaphore.h>
43#include <iprt/initterm.h>
44#include <iprt/process.h>
45#include <iprt/mem.h>
46#include <iprt/net.h>
47#include <iprt/log.h>
48#include <iprt/mp.h>
49#include <iprt/mem.h>
50#include <iprt/time.h>
51
52#define VBOXNETFLT_OS_SPECFIC 1
53#include "../VBoxNetFltInternal.h"
54
55
56/*******************************************************************************
57* Defined Constants And Macros *
58*******************************************************************************/
59#define VBOX_FLT_NB_TO_INST(pNB) ((PVBOXNETFLTINS)((uint8_t *)pNB - RT_OFFSETOF(VBOXNETFLTINS, u.s.Notifier)))
60#define VBOX_FLT_PT_TO_INST(pPT) ((PVBOXNETFLTINS)((uint8_t *)pPT - RT_OFFSETOF(VBOXNETFLTINS, u.s.PacketType)))
61#define VBOX_FLT_XT_TO_INST(pXT) ((PVBOXNETFLTINS)((uint8_t *)pXT - RT_OFFSETOF(VBOXNETFLTINS, u.s.XmitTask)))
62
63#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
64# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb_reset_network_header(skb)
65# define VBOX_SKB_RESET_MAC_HDR(skb) skb_reset_mac_header(skb)
66#else
67# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb->nh.raw = skb->data
68# define VBOX_SKB_RESET_MAC_HDR(skb) skb->mac.raw = skb->data
69#endif
70
71#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
72# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb)
73#else
74# define CHECKSUM_PARTIAL CHECKSUM_HW
75# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
76# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb, 0)
77# else
78# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
79# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(&skb, 0)
80# else
81# define VBOX_SKB_CHECKSUM_HELP(skb) (!skb_checksum_help(skb))
82# endif
83# endif
84#endif
85
86#ifndef NET_IP_ALIGN
87# define NET_IP_ALIGN 2
88#endif
89
90#if 0
91/** Create scatter / gather segments for fragments. When not used, we will
92 * linearize the socket buffer before creating the internal networking SG. */
93# define VBOXNETFLT_SG_SUPPORT 1
94#endif
95
96#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
97/** Indicates that the linux kernel may send us GSO frames. */
98# define VBOXNETFLT_WITH_GSO 1
99
100/** This enables or disables the transmitting of GSO frame from the internal
101 * network and to the host. */
102# define VBOXNETFLT_WITH_GSO_XMIT_HOST 1
103
104# if 0 /** @todo This is currently disable because it causes performance loss of 5-10%. */
105/** This enables or disables the transmitting of GSO frame from the internal
106 * network and to the wire. */
107# define VBOXNETFLT_WITH_GSO_XMIT_WIRE 1
108# endif
109
110/** This enables or disables the forwarding/flooding of GSO frame from the host
111 * to the internal network. */
112# define VBOXNETFLT_WITH_GSO_RECV 1
113
114#endif
115
116
117/*******************************************************************************
118* Internal Functions *
119*******************************************************************************/
120static int VBoxNetFltLinuxInit(void);
121static void VBoxNetFltLinuxUnload(void);
122
123
124/*******************************************************************************
125* Global Variables *
126*******************************************************************************/
127/**
128 * The (common) global data.
129 */
130static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
131
132module_init(VBoxNetFltLinuxInit);
133module_exit(VBoxNetFltLinuxUnload);
134
135MODULE_AUTHOR("Oracle Corporation");
136MODULE_DESCRIPTION("VirtualBox Network Filter Driver");
137MODULE_LICENSE("GPL");
138#ifdef MODULE_VERSION
139MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
140#endif
141
142
143#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) && defined(LOG_ENABLED)
144unsigned dev_get_flags(const struct net_device *dev)
145{
146 unsigned flags;
147
148 flags = (dev->flags & ~(IFF_PROMISC |
149 IFF_ALLMULTI |
150 IFF_RUNNING)) |
151 (dev->gflags & (IFF_PROMISC |
152 IFF_ALLMULTI));
153
154 if (netif_running(dev) && netif_carrier_ok(dev))
155 flags |= IFF_RUNNING;
156
157 return flags;
158}
159#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) */
160
161
162/**
163 * Initialize module.
164 *
165 * @returns appropriate status code.
166 */
167static int __init VBoxNetFltLinuxInit(void)
168{
169 int rc;
170 /*
171 * Initialize IPRT.
172 */
173 rc = RTR0Init(0);
174 if (RT_SUCCESS(rc))
175 {
176 Log(("VBoxNetFltLinuxInit\n"));
177
178 /*
179 * Initialize the globals and connect to the support driver.
180 *
181 * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
182 * for establishing the connect to the support driver.
183 */
184 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
185 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
186 if (RT_SUCCESS(rc))
187 {
188 LogRel(("VBoxNetFlt: Successfully started.\n"));
189 return 0;
190 }
191
192 LogRel(("VBoxNetFlt: failed to initialize device extension (rc=%d)\n", rc));
193 RTR0Term();
194 }
195 else
196 LogRel(("VBoxNetFlt: failed to initialize IPRT (rc=%d)\n", rc));
197
198 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
199 return -RTErrConvertToErrno(rc);
200}
201
202
203/**
204 * Unload the module.
205 *
206 * @todo We have to prevent this if we're busy!
207 */
208static void __exit VBoxNetFltLinuxUnload(void)
209{
210 int rc;
211 Log(("VBoxNetFltLinuxUnload\n"));
212 Assert(vboxNetFltCanUnload(&g_VBoxNetFltGlobals));
213
214 /*
215 * Undo the work done during start (in reverse order).
216 */
217 rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
218 AssertRC(rc); NOREF(rc);
219
220 RTR0Term();
221
222 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
223
224 Log(("VBoxNetFltLinuxUnload - done\n"));
225}
226
227/**
228 * Experiment where we filter trafic from the host to the internal network
229 * before it reaches the NIC driver.
230 *
231 * The current code uses a very ugly hack and only works on kernels using the
232 * net_device_ops (>= 2.6.29). It has been shown to give us a
233 * performance boost of 60-100% though. So, we have to find some less hacky way
234 * of getting this job done eventually.
235 *
236 * #define VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
237 */
238#ifdef VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
239
240/**
241 * The overridden net_device_ops of the device we're attached to.
242 *
243 * Requires Linux 2.6.29 or later.
244 *
245 * This is a very dirty hack that was create to explore how much we can improve
246 * the host to guest transfers by not CC'ing the NIC.
247 */
248typedef struct VBoxNetDeviceOpsOverride
249{
250 /** Our overridden ops. */
251 struct net_device_ops Ops;
252 /** Magic word. */
253 uint32_t u32Magic;
254 /** Pointer to the original ops. */
255 struct net_device_ops const *pOrgOps;
256 /** Pointer to the net filter instance. */
257 PVBOXNETFLTINS pVBoxNetFlt;
258 /** The number of filtered packages. */
259 uint64_t cFiltered;
260 /** The total number of packets */
261 uint64_t cTotal;
262} VBOXNETDEVICEOPSOVERRIDE, *PVBOXNETDEVICEOPSOVERRIDE;
263/** VBOXNETDEVICEOPSOVERRIDE::u32Magic value. */
264#define VBOXNETDEVICEOPSOVERRIDE_MAGIC UINT32_C(0x00c0ffee)
265
266/**
267 * ndo_start_xmit wrapper that drops packets that shouldn't go to the wire
268 * because they belong on the internal network.
269 *
270 * @returns NETDEV_TX_XXX.
271 * @param pSkb The socket buffer to transmit.
272 * @param pDev The net device.
273 */
274static int vboxNetFltLinuxStartXmitFilter(struct sk_buff *pSkb, struct net_device *pDev)
275{
276 PVBOXNETDEVICEOPSOVERRIDE pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->netdev_ops;
277 uint8_t abHdrBuf[sizeof(RTNETETHERHDR) + sizeof(uint32_t) + RTNETIPV4_MIN_LEN];
278 PCRTNETETHERHDR pEtherHdr;
279 PINTNETTRUNKSWPORT pSwitchPort;
280
281
282 /*
283 * Validate the override structure.
284 *
285 * Note! We're racing vboxNetFltLinuxUnhookDev here. If this was supposed
286 * to be production quality code, we would have to be much more
287 * careful here and avoid the race.
288 */
289 if ( !VALID_PTR(pOverride)
290 || pOverride->u32Magic != VBOXNETDEVICEOPSOVERRIDE_MAGIC
291 || !VALID_PTR(pOverride->pOrgOps))
292 {
293 printk("vboxNetFltLinuxStartXmitFilter: bad override %p\n", pOverride);
294 dev_kfree_skb(pSkb);
295 return NETDEV_TX_OK;
296 }
297 pOverride->cTotal++;
298
299 /*
300 * Do the filtering base on the defaul OUI of our virtual NICs
301 *
302 * Note! In a real solution, we would ask the switch whether the
303 * destination MAC is 100% to be on the internal network and then
304 * drop it.
305 */
306 pEtherHdr = (PCRTNETETHERHDR)skb_header_pointer(pSkb, 0, sizeof(abHdrBuf), &abHdrBuf[0]);
307 if ( pEtherHdr
308 && VALID_PTR(pOverride->pVBoxNetFlt)
309 && (pSwitchPort = pOverride->pVBoxNetFlt->pSwitchPort) != NULL
310 && VALID_PTR(pSwitchPort)
311 )
312 {
313 INTNETSWDECISION enmDecision;
314 uint32_t cbHdrs = skb_headlen(pSkb);
315 cbHdrs = RT_MAX(cbHdrs, sizeof(abHdrBuf));
316
317 /** @todo consider reference counting, etc. */
318 enmDecision = pSwitchPort->pfnPreRecv(pSwitchPort, pEtherHdr, cbHdrs, INTNETTRUNKDIR_HOST);
319 if (enmDecision == INTNETSWDECISION_INTNET)
320 {
321 dev_kfree_skb(pSkb);
322 pOverride->cFiltered++;
323 return NETDEV_TX_OK;
324 }
325 }
326
327 return pOverride->pOrgOps->ndo_start_xmit(pSkb, pDev);
328}
329
330/**
331 * Hooks the device ndo_start_xmit operation of the device.
332 *
333 * @param pThis The net filter instance.
334 * @param pDev The net device.
335 */
336static void vboxNetFltLinuxHookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
337{
338 PVBOXNETDEVICEOPSOVERRIDE pOverride;
339 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
340
341 pOverride = RTMemAlloc(sizeof(*pOverride));
342 if (!pOverride)
343 return;
344 pOverride->pOrgOps = pDev->netdev_ops;
345 pOverride->Ops = *pDev->netdev_ops;
346 pOverride->Ops.ndo_start_xmit = vboxNetFltLinuxStartXmitFilter;
347 pOverride->u32Magic = VBOXNETDEVICEOPSOVERRIDE_MAGIC;
348 pOverride->cTotal = 0;
349 pOverride->cFiltered = 0;
350 pOverride->pVBoxNetFlt = pThis;
351
352 RTSpinlockAcquire(pThis->hSpinlock, &Tmp); /* (this isn't necessary, but so what) */
353 ASMAtomicXchgPtr((void * volatile *)&pDev->netdev_ops, pOverride);
354 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
355}
356
357/**
358 * Undos what vboxNetFltLinuxHookDev did.
359 *
360 * @param pThis The net filter instance.
361 * @param pDev The net device. Can be NULL, in which case
362 * we'll try retrieve it from @a pThis.
363 */
364static void vboxNetFltLinuxUnhookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
365{
366 PVBOXNETDEVICEOPSOVERRIDE pOverride;
367 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
368
369 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
370 if (!pDev)
371 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
372 if (VALID_PTR(pDev))
373 {
374 pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->netdev_ops;
375 if ( VALID_PTR(pOverride)
376 && pOverride->u32Magic == VBOXNETDEVICEOPSOVERRIDE_MAGIC
377 && VALID_PTR(pOverride->pOrgOps)
378 )
379 {
380 ASMAtomicXchgPtr((void * volatile *)&pDev->netdev_ops, pOverride->pOrgOps);
381 ASMAtomicWriteU32(&pOverride->u32Magic, 0);
382 }
383 else
384 pOverride = NULL;
385 }
386 else
387 pOverride = NULL;
388 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
389
390 if (pOverride)
391 {
392 printk("vboxnetflt: dropped %llu out of %llu packets\n", pOverride->cFiltered, pOverride->cTotal);
393 RTMemFree(pOverride);
394 }
395}
396
397#endif /* VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT */
398
399
400/**
401 * Reads and retains the host interface handle.
402 *
403 * @returns The handle, NULL if detached.
404 * @param pThis
405 */
406DECLINLINE(struct net_device *) vboxNetFltLinuxRetainNetDev(PVBOXNETFLTINS pThis)
407{
408#if 0
409 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
410 struct net_device *pDev = NULL;
411
412 Log(("vboxNetFltLinuxRetainNetDev\n"));
413 /*
414 * Be careful here to avoid problems racing the detached callback.
415 */
416 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
417 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
418 {
419 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
420 if (pDev)
421 {
422 dev_hold(pDev);
423 Log(("vboxNetFltLinuxRetainNetDev: Device %p(%s) retained. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
424 }
425 }
426 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
427
428 Log(("vboxNetFltLinuxRetainNetDev - done\n"));
429 return pDev;
430#else
431 return (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
432#endif
433}
434
435
436/**
437 * Release the host interface handle previously retained
438 * by vboxNetFltLinuxRetainNetDev.
439 *
440 * @param pThis The instance.
441 * @param pDev The vboxNetFltLinuxRetainNetDev
442 * return value, NULL is fine.
443 */
444DECLINLINE(void) vboxNetFltLinuxReleaseNetDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
445{
446#if 0
447 Log(("vboxNetFltLinuxReleaseNetDev\n"));
448 NOREF(pThis);
449 if (pDev)
450 {
451 dev_put(pDev);
452 Log(("vboxNetFltLinuxReleaseNetDev: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
453 }
454 Log(("vboxNetFltLinuxReleaseNetDev - done\n"));
455#endif
456}
457
458#define VBOXNETFLT_CB_TAG(skb) (0xA1C90000 | (skb->dev->ifindex & 0xFFFF))
459#define VBOXNETFLT_SKB_TAG(skb) (*(uint32_t*)&((skb)->cb[sizeof((skb)->cb)-sizeof(uint32_t)]))
460
461/**
462 * Checks whether this is an mbuf created by vboxNetFltLinuxMBufFromSG,
463 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
464 *
465 * @returns true / false accordingly.
466 * @param pBuf The sk_buff.
467 */
468DECLINLINE(bool) vboxNetFltLinuxSkBufIsOur(struct sk_buff *pBuf)
469{
470 return VBOXNETFLT_SKB_TAG(pBuf) == VBOXNETFLT_CB_TAG(pBuf);
471}
472
473
474/**
475 * Internal worker that create a linux sk_buff for a
476 * (scatter/)gather list.
477 *
478 * @returns Pointer to the sk_buff.
479 * @param pThis The instance.
480 * @param pSG The (scatter/)gather list.
481 * @param fDstWire Set if the destination is the wire.
482 */
483static struct sk_buff *vboxNetFltLinuxSkBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG, bool fDstWire)
484{
485 struct sk_buff *pPkt;
486 struct net_device *pDev;
487 unsigned fGsoType = 0;
488
489 if (pSG->cbTotal == 0)
490 {
491 LogRel(("VBoxNetFlt: Dropped empty packet coming from internal network.\n"));
492 return NULL;
493 }
494
495 /** @todo We should use fragments mapping the SG buffers with large packets.
496 * 256 bytes seems to be the a threshold used a lot for this. It
497 * requires some nasty work on the intnet side though... */
498 /*
499 * Allocate a packet and copy over the data.
500 */
501 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
502 pPkt = dev_alloc_skb(pSG->cbTotal + NET_IP_ALIGN);
503 if (RT_UNLIKELY(!pPkt))
504 {
505 Log(("vboxNetFltLinuxSkBufFromSG: Failed to allocate sk_buff(%u).\n", pSG->cbTotal));
506 pSG->pvUserData = NULL;
507 return NULL;
508 }
509 pPkt->dev = pDev;
510 pPkt->ip_summed = CHECKSUM_NONE;
511
512 /* Align IP header on 16-byte boundary: 2 + 14 (ethernet hdr size). */
513 skb_reserve(pPkt, NET_IP_ALIGN);
514
515 /* Copy the segments. */
516 skb_put(pPkt, pSG->cbTotal);
517 INTNETSgRead(pSG, pPkt->data);
518
519#if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
520 /*
521 * Setup GSO if used by this packet.
522 */
523 switch ((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type)
524 {
525 default:
526 AssertMsgFailed(("%u (%s)\n", pSG->GsoCtx.u8Type, PDMNetGsoTypeName((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type) ));
527 /* fall thru */
528 case PDMNETWORKGSOTYPE_INVALID:
529 fGsoType = 0;
530 break;
531 case PDMNETWORKGSOTYPE_IPV4_TCP:
532 fGsoType = SKB_GSO_TCPV4;
533 break;
534 case PDMNETWORKGSOTYPE_IPV4_UDP:
535 fGsoType = SKB_GSO_UDP;
536 break;
537 case PDMNETWORKGSOTYPE_IPV6_TCP:
538 fGsoType = SKB_GSO_TCPV6;
539 break;
540 }
541 if (fGsoType)
542 {
543 struct skb_shared_info *pShInfo = skb_shinfo(pPkt);
544
545 pShInfo->gso_type = fGsoType | SKB_GSO_DODGY;
546 pShInfo->gso_size = pSG->GsoCtx.cbMaxSeg;
547 pShInfo->gso_segs = PDMNetGsoCalcSegmentCount(&pSG->GsoCtx, pSG->cbTotal);
548
549 if (fDstWire)
550 {
551 Assert(skb_headlen(pPkt) >= pSG->GsoCtx.cbHdrs);
552 pPkt->ip_summed = CHECKSUM_PARTIAL;
553# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
554 pPkt->csum_start = skb_headroom(pPkt) + pSG->GsoCtx.offHdr2;
555 if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
556 pPkt->csum_offset = RT_OFFSETOF(RTNETTCP, th_sum);
557 else
558 pPkt->csum_offset = RT_OFFSETOF(RTNETUDP, uh_sum);
559# else
560 pPkt->h.raw = pPkt->data + pSG->GsoCtx.offHdr2;
561 if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
562 pPkt->csum = RT_OFFSETOF(RTNETTCP, th_sum);
563 else
564 pPkt->csum = RT_OFFSETOF(RTNETUDP, uh_sum);
565# endif
566 }
567 else
568 {
569 pPkt->ip_summed = CHECKSUM_UNNECESSARY;
570 pPkt->csum = 0;
571 PDMNetGsoPrepForDirectUse(&pSG->GsoCtx, pPkt->data, pSG->cbTotal, false /*fPayloadChecksum*/);
572 }
573 }
574#endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE || VBOXNETFLT_WITH_GSO_XMIT_HOST */
575
576 /*
577 * Finish up the socket buffer.
578 */
579 pPkt->protocol = eth_type_trans(pPkt, pDev);
580 if (fDstWire)
581 {
582 VBOX_SKB_RESET_NETWORK_HDR(pPkt);
583
584 /* Restore ethernet header back. */
585 skb_push(pPkt, ETH_HLEN); /** @todo VLAN: +4 if VLAN? */
586 VBOX_SKB_RESET_MAC_HDR(pPkt);
587 }
588 VBOXNETFLT_SKB_TAG(pPkt) = VBOXNETFLT_CB_TAG(pPkt);
589
590 return pPkt;
591}
592
593
594/**
595 * Initializes a SG list from an sk_buff.
596 *
597 * @returns Number of segments.
598 * @param pThis The instance.
599 * @param pBuf The sk_buff.
600 * @param pSG The SG.
601 * @param pvFrame The frame pointer, optional.
602 * @param cSegs The number of segments allocated for the SG.
603 * This should match the number in the mbuf exactly!
604 * @param fSrc The source of the frame.
605 * @param pGso Pointer to the GSO context if it's a GSO
606 * internal network frame. NULL if regular frame.
607 */
608DECLINLINE(void) vboxNetFltLinuxSkBufToSG(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, PINTNETSG pSG,
609 unsigned cSegs, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
610{
611 int i;
612 NOREF(pThis);
613
614 Assert(!skb_shinfo(pBuf)->frag_list);
615
616 if (fSrc & INTNETTRUNKDIR_WIRE)
617 {
618 /*
619 * The packet came from wire, ethernet header was removed by device driver.
620 * Restore it.
621 */
622 skb_push(pBuf, ETH_HLEN);
623 }
624
625 if (!pGsoCtx)
626 INTNETSgInitTempSegs(pSG, pBuf->len, cSegs, 0 /*cSegsUsed*/);
627 else
628 INTNETSgInitTempSegsGso(pSG, pBuf->len, cSegs, 0 /*cSegsUsed*/, pGsoCtx);
629
630#ifdef VBOXNETFLT_SG_SUPPORT
631 pSG->aSegs[0].cb = skb_headlen(pBuf);
632 pSG->aSegs[0].pv = pBuf->data;
633 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
634
635 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
636 {
637 skb_frag_t *pFrag = &skb_shinfo(pBuf)->frags[i];
638 pSG->aSegs[i+1].cb = pFrag->size;
639 pSG->aSegs[i+1].pv = kmap(pFrag->page);
640 printk("%p = kmap()\n", pSG->aSegs[i+1].pv);
641 pSG->aSegs[i+1].Phys = NIL_RTHCPHYS;
642 }
643 ++i;
644
645#else
646 pSG->aSegs[0].cb = pBuf->len;
647 pSG->aSegs[0].pv = pBuf->data;
648 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
649 i = 1;
650#endif
651
652 pSG->cSegsUsed = i;
653
654#ifdef PADD_RUNT_FRAMES_FROM_HOST
655 /*
656 * Add a trailer if the frame is too small.
657 *
658 * Since we're getting to the packet before it is framed, it has not
659 * yet been padded. The current solution is to add a segment pointing
660 * to a buffer containing all zeros and pray that works for all frames...
661 */
662 if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
663 {
664 static uint8_t const s_abZero[128] = {0};
665
666 AssertReturnVoid(i < cSegs);
667
668 pSG->aSegs[i].Phys = NIL_RTHCPHYS;
669 pSG->aSegs[i].pv = (void *)&s_abZero[0];
670 pSG->aSegs[i].cb = 60 - pSG->cbTotal;
671 pSG->cbTotal = 60;
672 pSG->cSegsUsed++;
673 Assert(i + 1 <= pSG->cSegsAlloc)
674 }
675#endif
676
677 Log4(("vboxNetFltLinuxSkBufToSG: allocated=%d, segments=%d frags=%d next=%p frag_list=%p pkt_type=%x fSrc=%x\n",
678 pSG->cSegsAlloc, pSG->cSegsUsed, skb_shinfo(pBuf)->nr_frags, pBuf->next, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, fSrc));
679 for (i = 0; i < pSG->cSegsUsed; i++)
680 Log4(("vboxNetFltLinuxSkBufToSG: #%d: cb=%d pv=%p\n",
681 i, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
682}
683
684/**
685 * Packet handler,
686 *
687 * @returns 0 or EJUSTRETURN.
688 * @param pThis The instance.
689 * @param pMBuf The mbuf.
690 * @param pvFrame The start of the frame, optional.
691 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
692 * @param eProtocol The protocol.
693 */
694#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
695static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
696 struct net_device *pSkbDev,
697 struct packet_type *pPacketType,
698 struct net_device *pOrigDev)
699#else
700static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
701 struct net_device *pSkbDev,
702 struct packet_type *pPacketType)
703#endif
704{
705 PVBOXNETFLTINS pThis;
706 struct net_device *pDev;
707 LogFlow(("vboxNetFltLinuxPacketHandler: pBuf=%p pSkbDev=%p pPacketType=%p\n",
708 pBuf, pSkbDev, pPacketType));
709#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
710 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",
711 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));
712#else
713 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",
714 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));
715#endif
716 /*
717 * Drop it immediately?
718 */
719 if (!pBuf)
720 return 0;
721
722 pThis = VBOX_FLT_PT_TO_INST(pPacketType);
723 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
724 if (pThis->u.s.pDev != pSkbDev)
725 {
726 Log(("vboxNetFltLinuxPacketHandler: Devices do not match, pThis may be wrong! pThis=%p\n", pThis));
727 return 0;
728 }
729
730 Log4(("vboxNetFltLinuxPacketHandler: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
731 if (vboxNetFltLinuxSkBufIsOur(pBuf))
732 {
733 Log2(("vboxNetFltLinuxPacketHandler: got our own sk_buff, drop it.\n"));
734 dev_kfree_skb(pBuf);
735 return 0;
736 }
737
738#ifndef VBOXNETFLT_SG_SUPPORT
739 {
740 /*
741 * Get rid of fragmented packets, they cause too much trouble.
742 */
743 struct sk_buff *pCopy = skb_copy(pBuf, GFP_ATOMIC);
744 kfree_skb(pBuf);
745 if (!pCopy)
746 {
747 LogRel(("VBoxNetFlt: Failed to allocate packet buffer, dropping the packet.\n"));
748 return 0;
749 }
750 pBuf = pCopy;
751# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
752 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",
753 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));
754# else
755 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",
756 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));
757# endif
758 }
759#endif
760
761 /* Add the packet to transmit queue and schedule the bottom half. */
762 skb_queue_tail(&pThis->u.s.XmitQueue, pBuf);
763 schedule_work(&pThis->u.s.XmitTask);
764 Log4(("vboxNetFltLinuxPacketHandler: scheduled work %p for sk_buff %p\n",
765 &pThis->u.s.XmitTask, pBuf));
766
767 /* It does not really matter what we return, it is ignored by the kernel. */
768 return 0;
769}
770
771/**
772 * Calculate the number of INTNETSEG segments the socket buffer will need.
773 *
774 * @returns Segment count.
775 * @param pBuf The socket buffer.
776 */
777DECLINLINE(unsigned) vboxNetFltLinuxCalcSGSegments(struct sk_buff *pBuf)
778{
779#ifdef VBOXNETFLT_SG_SUPPORT
780 unsigned cSegs = 1 + skb_shinfo(pBuf)->nr_frags;
781#else
782 unsigned cSegs = 1;
783#endif
784#ifdef PADD_RUNT_FRAMES_FROM_HOST
785 /* vboxNetFltLinuxSkBufToSG adds a padding segment if it's a runt. */
786 if (pBuf->len < 60)
787 cSegs++;
788#endif
789 return cSegs;
790}
791
792/**
793 * Destroy the intnet scatter / gather buffer created by
794 * vboxNetFltLinuxSkBufToSG.
795 */
796static void vboxNetFltLinuxDestroySG(PINTNETSG pSG)
797{
798#ifdef VBOXNETFLT_SG_SUPPORT
799 int i;
800
801 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
802 {
803 printk("kunmap(%p)\n", pSG->aSegs[i+1].pv);
804 kunmap(pSG->aSegs[i+1].pv);
805 }
806#endif
807 NOREF(pSG);
808}
809
810#ifdef LOG_ENABLED
811/**
812 * Logging helper.
813 */
814static void vboxNetFltDumpPacket(PINTNETSG pSG, bool fEgress, const char *pszWhere, int iIncrement)
815{
816 uint8_t *pInt, *pExt;
817 static int iPacketNo = 1;
818 iPacketNo += iIncrement;
819 if (fEgress)
820 {
821 pExt = pSG->aSegs[0].pv;
822 pInt = pExt + 6;
823 }
824 else
825 {
826 pInt = pSG->aSegs[0].pv;
827 pExt = pInt + 6;
828 }
829 Log(("VBoxNetFlt: (int)%02x:%02x:%02x:%02x:%02x:%02x"
830 " %s (%s)%02x:%02x:%02x:%02x:%02x:%02x (%u bytes) packet #%u\n",
831 pInt[0], pInt[1], pInt[2], pInt[3], pInt[4], pInt[5],
832 fEgress ? "-->" : "<--", pszWhere,
833 pExt[0], pExt[1], pExt[2], pExt[3], pExt[4], pExt[5],
834 pSG->cbTotal, iPacketNo));
835 Log3(("%.*Rhxd\n", pSG->aSegs[0].cb, pSG->aSegs[0].pv));
836}
837#else
838# define vboxNetFltDumpPacket(a, b, c, d) do {} while (0)
839#endif
840
841#ifdef VBOXNETFLT_WITH_GSO_RECV
842
843/**
844 * Worker for vboxNetFltLinuxForwardToIntNet that checks if we can forwards a
845 * GSO socket buffer without having to segment it.
846 *
847 * @returns true on success, false if needs segmenting.
848 * @param pThis The net filter instance.
849 * @param pSkb The GSO socket buffer.
850 * @param fSrc The source.
851 * @param pGsoCtx Where to return the GSO context on success.
852 */
853static bool vboxNetFltLinuxCanForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc,
854 PPDMNETWORKGSO pGsoCtx)
855{
856 PDMNETWORKGSOTYPE enmGsoType;
857 uint16_t uEtherType;
858 unsigned int cbTransport;
859 unsigned int offTransport;
860 unsigned int cbTransportHdr;
861 unsigned uProtocol;
862 union
863 {
864 RTNETIPV4 IPv4;
865 RTNETIPV6 IPv6;
866 RTNETTCP Tcp;
867 uint8_t ab[40];
868 uint16_t au16[40/2];
869 uint32_t au32[40/4];
870 } Buf;
871
872 /*
873 * Check the GSO properties of the socket buffer and make sure it fits.
874 */
875 /** @todo Figure out how to handle SKB_GSO_TCP_ECN! */
876 if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_type & ~(SKB_GSO_UDP | SKB_GSO_DODGY | SKB_GSO_TCPV6 | SKB_GSO_TCPV4) ))
877 {
878 Log5(("vboxNetFltLinuxCanForwardAsGso: gso_type=%#x\n", skb_shinfo(pSkb)->gso_type));
879 return false;
880 }
881 if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_size < 1
882 || pSkb->len > VBOX_MAX_GSO_SIZE ))
883 {
884 Log5(("vboxNetFltLinuxCanForwardAsGso: gso_size=%#x skb_len=%#x (max=%#x)\n", skb_shinfo(pSkb)->gso_size, pSkb->len, VBOX_MAX_GSO_SIZE));
885 return false;
886 }
887 if (RT_UNLIKELY(fSrc & INTNETTRUNKDIR_WIRE))
888 {
889 Log5(("vboxNetFltLinuxCanForwardAsGso: fSrc=wire\n"));
890 return false;
891 }
892
893 /*
894 * skb_gso_segment does the following. Do we need to do it as well?
895 */
896#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
897 skb_reset_mac_header(pSkb);
898 pSkb->mac_len = pSkb->network_header - pSkb->mac_header;
899#else
900 pSkb->mac.raw = pSkb->data;
901 pSkb->mac_len = pSkb->nh.raw - pSkb->data;
902#endif
903
904 /*
905 * Switch on the ethertype.
906 */
907 uEtherType = pSkb->protocol;
908 if ( uEtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_VLAN)
909 && pSkb->mac_len == sizeof(RTNETETHERHDR) + sizeof(uint32_t))
910 {
911 uint16_t const *puEtherType = skb_header_pointer(pSkb, sizeof(RTNETETHERHDR) + sizeof(uint16_t), sizeof(uint16_t), &Buf);
912 if (puEtherType)
913 uEtherType = *puEtherType;
914 }
915 switch (uEtherType)
916 {
917 case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4):
918 {
919 unsigned int cbHdr;
920 PCRTNETIPV4 pIPv4 = (PCRTNETIPV4)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv4), &Buf);
921 if (RT_UNLIKELY(!pIPv4))
922 {
923 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv4 hdr\n"));
924 return false;
925 }
926
927 cbHdr = pIPv4->ip_hl * 4;
928 cbTransport = RT_N2H_U16(pIPv4->ip_len);
929 if (RT_UNLIKELY( cbHdr < RTNETIPV4_MIN_LEN
930 || cbHdr > cbTransport ))
931 {
932 Log5(("vboxNetFltLinuxCanForwardAsGso: invalid IPv4 lengths: ip_hl=%u ip_len=%u\n", pIPv4->ip_hl, RT_N2H_U16(pIPv4->ip_len)));
933 return false;
934 }
935 cbTransport -= cbHdr;
936 offTransport = pSkb->mac_len + cbHdr;
937 uProtocol = pIPv4->ip_p;
938 if (uProtocol == RTNETIPV4_PROT_TCP)
939 enmGsoType = PDMNETWORKGSOTYPE_IPV4_TCP;
940 else if (uProtocol == RTNETIPV4_PROT_UDP)
941 enmGsoType = PDMNETWORKGSOTYPE_IPV4_UDP;
942 else /** @todo IPv6: 4to6 tunneling */
943 enmGsoType = PDMNETWORKGSOTYPE_INVALID;
944 break;
945 }
946
947 case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV6):
948 {
949 PCRTNETIPV6 pIPv6 = (PCRTNETIPV6)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv6), &Buf);
950 if (RT_UNLIKELY(!pIPv6))
951 {
952 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv6 hdr\n"));
953 return false;
954 }
955
956 cbTransport = RT_N2H_U16(pIPv6->ip6_plen);
957 offTransport = pSkb->mac_len + sizeof(RTNETIPV6);
958 uProtocol = pIPv6->ip6_nxt;
959 /** @todo IPv6: Dig our way out of the other headers. */
960 if (uProtocol == RTNETIPV4_PROT_TCP)
961 enmGsoType = PDMNETWORKGSOTYPE_IPV6_TCP;
962 else if (uProtocol == RTNETIPV4_PROT_UDP)
963 enmGsoType = PDMNETWORKGSOTYPE_IPV4_UDP;
964 else
965 enmGsoType = PDMNETWORKGSOTYPE_INVALID;
966 break;
967 }
968
969 default:
970 Log5(("vboxNetFltLinuxCanForwardAsGso: uEtherType=%#x\n", RT_H2N_U16(uEtherType)));
971 return false;
972 }
973
974 if (enmGsoType == PDMNETWORKGSOTYPE_INVALID)
975 {
976 Log5(("vboxNetFltLinuxCanForwardAsGso: Unsupported protocol %d\n", uProtocol));
977 return false;
978 }
979
980 if (RT_UNLIKELY( offTransport + cbTransport <= offTransport
981 || offTransport + cbTransport > pSkb->len
982 || cbTransport < (uProtocol == RTNETIPV4_PROT_TCP ? RTNETTCP_MIN_LEN : RTNETUDP_MIN_LEN)) )
983 {
984 Log5(("vboxNetFltLinuxCanForwardAsGso: Bad transport length; off=%#x + cb=%#x => %#x; skb_len=%#x (%s)\n",
985 offTransport, cbTransport, offTransport + cbTransport, pSkb->len, PDMNetGsoTypeName(enmGsoType) ));
986 return false;
987 }
988
989 /*
990 * Check the TCP/UDP bits.
991 */
992 if (uProtocol == RTNETIPV4_PROT_TCP)
993 {
994 PCRTNETTCP pTcp = (PCRTNETTCP)skb_header_pointer(pSkb, offTransport, sizeof(Buf.Tcp), &Buf);
995 if (RT_UNLIKELY(!pTcp))
996 {
997 Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access TCP hdr\n"));
998 return false;
999 }
1000
1001 cbTransportHdr = pTcp->th_off * 4;
1002 if (RT_UNLIKELY( cbTransportHdr < RTNETTCP_MIN_LEN
1003 || cbTransportHdr > cbTransport
1004 || offTransport + cbTransportHdr >= UINT8_MAX
1005 || offTransport + cbTransportHdr >= pSkb->len ))
1006 {
1007 Log5(("vboxNetFltLinuxCanForwardAsGso: No space for TCP header; off=%#x cb=%#x skb_len=%#x\n", offTransport, cbTransportHdr, pSkb->len));
1008 return false;
1009 }
1010
1011 }
1012 else
1013 {
1014 Assert(uProtocol == RTNETIPV4_PROT_UDP);
1015 cbTransportHdr = sizeof(RTNETUDP);
1016 if (RT_UNLIKELY( offTransport + cbTransportHdr >= UINT8_MAX
1017 || offTransport + cbTransportHdr >= pSkb->len ))
1018 {
1019 Log5(("vboxNetFltLinuxCanForwardAsGso: No space for UDP header; off=%#x skb_len=%#x\n", offTransport, pSkb->len));
1020 return false;
1021 }
1022 }
1023
1024 /*
1025 * We're good, init the GSO context.
1026 */
1027 pGsoCtx->u8Type = enmGsoType;
1028 pGsoCtx->cbHdrs = offTransport + cbTransportHdr;
1029 pGsoCtx->cbMaxSeg = skb_shinfo(pSkb)->gso_size;
1030 pGsoCtx->offHdr1 = pSkb->mac_len;
1031 pGsoCtx->offHdr2 = offTransport;
1032 pGsoCtx->au8Unused[0] = 0;
1033 pGsoCtx->au8Unused[1] = 0;
1034
1035 return true;
1036}
1037
1038/**
1039 * Forward the socket buffer as a GSO internal network frame.
1040 *
1041 * @returns IPRT status code.
1042 * @param pThis The net filter instance.
1043 * @param pSkb The GSO socket buffer.
1044 * @param fSrc The source.
1045 * @param pGsoCtx Where to return the GSO context on success.
1046 */
1047static int vboxNetFltLinuxForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
1048{
1049 int rc;
1050 unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pSkb);
1051 if (RT_LIKELY(cSegs <= MAX_SKB_FRAGS + 1))
1052 {
1053 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
1054 if (RT_LIKELY(pSG))
1055 {
1056 vboxNetFltLinuxSkBufToSG(pThis, pSkb, pSG, cSegs, fSrc, pGsoCtx);
1057
1058 vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
1059 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, fSrc);
1060
1061 vboxNetFltLinuxDestroySG(pSG);
1062 rc = VINF_SUCCESS;
1063 }
1064 else
1065 {
1066 Log(("VBoxNetFlt: Dropping the sk_buff (failure case).\n"));
1067 rc = VERR_NO_MEMORY;
1068 }
1069 }
1070 else
1071 {
1072 Log(("VBoxNetFlt: Bad sk_buff? cSegs=%#x.\n", cSegs));
1073 rc = VERR_INTERNAL_ERROR_3;
1074 }
1075
1076 Log4(("VBoxNetFlt: Dropping the sk_buff.\n"));
1077 dev_kfree_skb(pSkb);
1078 return rc;
1079}
1080
1081#endif /* VBOXNETFLT_WITH_GSO_RECV */
1082
1083/**
1084 * Worker for vboxNetFltLinuxForwardToIntNet.
1085 *
1086 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
1087 * @param pThis The net filter instance.
1088 * @param pBuf The socket buffer.
1089 * @param fSrc The source.
1090 */
1091static int vboxNetFltLinuxForwardSegment(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, uint32_t fSrc)
1092{
1093 int rc;
1094 unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pBuf);
1095 if (cSegs <= MAX_SKB_FRAGS + 1)
1096 {
1097 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
1098 if (RT_LIKELY(pSG))
1099 {
1100 vboxNetFltLinuxSkBufToSG(pThis, pBuf, pSG, cSegs, fSrc, NULL /*pGsoCtx*/);
1101
1102 vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
1103 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, fSrc);
1104
1105 vboxNetFltLinuxDestroySG(pSG);
1106 rc = VINF_SUCCESS;
1107 }
1108 else
1109 {
1110 Log(("VBoxNetFlt: Failed to allocate SG buffer.\n"));
1111 rc = VERR_NO_MEMORY;
1112 }
1113 }
1114 else
1115 {
1116 Log(("VBoxNetFlt: Bad sk_buff? cSegs=%#x.\n", cSegs));
1117 rc = VERR_INTERNAL_ERROR_3;
1118 }
1119
1120 Log4(("VBoxNetFlt: Dropping the sk_buff.\n"));
1121 dev_kfree_skb(pBuf);
1122 return rc;
1123}
1124
1125static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf)
1126{
1127 uint32_t fSrc = pBuf->pkt_type == PACKET_OUTGOING ? INTNETTRUNKDIR_HOST : INTNETTRUNKDIR_WIRE;
1128
1129#ifdef VBOXNETFLT_WITH_GSO
1130 if (skb_is_gso(pBuf))
1131 {
1132 PDMNETWORKGSO GsoCtx;
1133 Log3(("vboxNetFltLinuxForwardToIntNet: 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 ip_summed=%d\n",
1134 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, pBuf->ip_summed));
1135# ifdef VBOXNETFLT_WITH_GSO_RECV
1136 if ( (skb_shinfo(pBuf)->gso_type & (SKB_GSO_UDP | SKB_GSO_TCPV6 | SKB_GSO_TCPV4))
1137 && vboxNetFltLinuxCanForwardAsGso(pThis, pBuf, fSrc, &GsoCtx) )
1138 vboxNetFltLinuxForwardAsGso(pThis, pBuf, fSrc, &GsoCtx);
1139 else
1140# endif
1141 {
1142 /* Need to segment the packet */
1143 struct sk_buff *pNext;
1144 struct sk_buff *pSegment = skb_gso_segment(pBuf, 0 /*supported features*/);
1145 if (IS_ERR(pSegment))
1146 {
1147 dev_kfree_skb(pBuf);
1148 LogRel(("VBoxNetFlt: Failed to segment a packet (%d).\n", PTR_ERR(pSegment)));
1149 return;
1150 }
1151
1152 for (; pSegment; pSegment = pNext)
1153 {
1154 Log3(("vboxNetFltLinuxForwardToIntNet: segment 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",
1155 pSegment->len, pSegment->data_len, pSegment->truesize, pSegment->next, skb_shinfo(pSegment)->nr_frags, skb_shinfo(pSegment)->gso_size, skb_shinfo(pSegment)->gso_segs, skb_shinfo(pSegment)->gso_type, skb_shinfo(pSegment)->frag_list, pSegment->pkt_type));
1156 pNext = pSegment->next;
1157 pSegment->next = 0;
1158 vboxNetFltLinuxForwardSegment(pThis, pSegment, fSrc);
1159 }
1160 dev_kfree_skb(pBuf);
1161 }
1162 }
1163 else
1164#endif /* VBOXNETFLT_WITH_GSO */
1165 {
1166 if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
1167 {
1168#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
1169 /*
1170 * Try to work around the problem with CentOS 4.7 and 5.2 (2.6.9
1171 * and 2.6.18 kernels), they pass wrong 'h' pointer down. We take IP
1172 * header length from the header itself and reconstruct 'h' pointer
1173 * to TCP (or whatever) header.
1174 */
1175 unsigned char *tmp = pBuf->h.raw;
1176 if (pBuf->h.raw == pBuf->nh.raw && pBuf->protocol == htons(ETH_P_IP))
1177 pBuf->h.raw = pBuf->nh.raw + pBuf->nh.iph->ihl * 4;
1178#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
1179 if (VBOX_SKB_CHECKSUM_HELP(pBuf))
1180 {
1181 LogRel(("VBoxNetFlt: Failed to compute checksum, dropping the packet.\n"));
1182 dev_kfree_skb(pBuf);
1183 return;
1184 }
1185#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
1186 /* Restore the original (wrong) pointer. */
1187 pBuf->h.raw = tmp;
1188#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
1189 }
1190 vboxNetFltLinuxForwardSegment(pThis, pBuf, fSrc);
1191 }
1192}
1193
1194/**
1195 * Work queue handler that forwards the socket buffers queued by
1196 * vboxNetFltLinuxPacketHandler to the internal network.
1197 *
1198 * @param pWork The work queue.
1199 */
1200#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1201static void vboxNetFltLinuxXmitTask(struct work_struct *pWork)
1202#else
1203static void vboxNetFltLinuxXmitTask(void *pWork)
1204#endif
1205{
1206 PVBOXNETFLTINS pThis = VBOX_FLT_XT_TO_INST(pWork);
1207 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1208 struct sk_buff *pBuf;
1209
1210 Log4(("vboxNetFltLinuxXmitTask: Got work %p.\n", pWork));
1211
1212 /*
1213 * Active? Retain the instance and increment the busy counter.
1214 */
1215 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1216 if (ASMAtomicUoReadBool(&pThis->fActive))
1217 {
1218 vboxNetFltRetain(pThis, true /* fBusy */);
1219 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1220
1221 while ((pBuf = skb_dequeue(&pThis->u.s.XmitQueue)) != NULL)
1222 vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
1223
1224 vboxNetFltRelease(pThis, true /* fBusy */);
1225 }
1226 else
1227 {
1228 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1229 /** @todo Shouldn't we just drop the packets here? There is little point in
1230 * making them accumulate when the VM is paused and it'll only waste
1231 * kernel memory anyway... Hmm. maybe wait a short while (2-5 secs)
1232 * before start draining the packets (goes for the intnet ring buf
1233 * too)? */
1234 }
1235}
1236
1237/**
1238 * Reports the GSO capabilites of the hardware NIC.
1239 *
1240 * @param pThis The net filter instance. The caller hold a
1241 * reference to this.
1242 */
1243static void vboxNetFltLinuxReportNicGsoCapabilities(PVBOXNETFLTINS pThis)
1244{
1245#ifdef VBOXNETFLT_WITH_GSO_XMIT_WIRE
1246 struct net_device *pDev;
1247 PINTNETTRUNKSWPORT pSwitchPort;
1248 unsigned int fFeatures;
1249 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1250
1251 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1252
1253 pSwitchPort = pThis->pSwitchPort; /* this doesn't need to be here, but it doesn't harm. */
1254 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
1255 if (pDev)
1256 fFeatures = pDev->features;
1257 else
1258 fFeatures = 0;
1259
1260 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1261
1262 if (pThis->pSwitchPort)
1263 {
1264 /* Set/update the GSO capabilities of the NIC. */
1265 uint32_t fGsoCapabilites = 0;
1266 if (fFeatures & NETIF_F_TSO)
1267 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP);
1268 if (fFeatures & NETIF_F_TSO6)
1269 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP);
1270# if 0 /** @todo GSO: Test UDP offloading (UFO) on linux. */
1271 if (fFeatures & NETIF_F_UFO)
1272 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_UDP);
1273 if (fFeatures & NETIF_F_UFO)
1274 fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_UDP);
1275# endif
1276 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, fGsoCapabilites, INTNETTRUNKDIR_WIRE);
1277 }
1278#endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE */
1279}
1280
1281/**
1282 * Internal worker for vboxNetFltLinuxNotifierCallback.
1283 *
1284 * @returns VBox status code.
1285 * @param pThis The instance.
1286 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
1287 * flood the release log.
1288 */
1289static int vboxNetFltLinuxAttachToInterface(PVBOXNETFLTINS pThis, struct net_device *pDev)
1290{
1291 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1292 LogFlow(("vboxNetFltLinuxAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
1293
1294 /*
1295 * Retain and store the device.
1296 */
1297 dev_hold(pDev);
1298
1299 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1300 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, pDev);
1301 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1302
1303 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) retained. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
1304 Log(("vboxNetFltLinuxAttachToInterface: Got pDev=%p pThis=%p pThis->u.s.pDev=%p\n", pDev, pThis, ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev)));
1305
1306 /* Get the mac address while we still have a valid net_device reference. */
1307 memcpy(&pThis->u.s.Mac, pDev->dev_addr, sizeof(pThis->u.s.Mac));
1308
1309 /*
1310 * Install a packet filter for this device with a protocol wildcard (ETH_P_ALL).
1311 */
1312 pThis->u.s.PacketType.type = __constant_htons(ETH_P_ALL);
1313 pThis->u.s.PacketType.dev = pDev;
1314 pThis->u.s.PacketType.func = vboxNetFltLinuxPacketHandler;
1315 dev_add_pack(&pThis->u.s.PacketType);
1316
1317#ifdef VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
1318 vboxNetFltLinuxHookDev(pThis, pDev);
1319#endif
1320
1321 /*
1322 * Set indicators that require the spinlock. Be abit paranoid about racing
1323 * the device notification handle.
1324 */
1325 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1326 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
1327 if (pDev)
1328 {
1329 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
1330 ASMAtomicUoWriteBool(&pThis->u.s.fRegistered, true);
1331 pDev = NULL; /* don't dereference it */
1332 }
1333 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1334 Log(("vboxNetFltLinuxAttachToInterface: this=%p: Packet handler installed.\n", pThis));
1335
1336 /*
1337 * If the above succeeded report GSO capabilites, if not undo and
1338 * release the device.
1339 */
1340 if (!pDev)
1341 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
1342 else
1343 {
1344#ifdef VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
1345 vboxNetFltLinuxUnhookDev(pThis, pDev);
1346#endif
1347 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1348 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, NULL);
1349 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1350 dev_put(pDev);
1351 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
1352 }
1353
1354 LogRel(("VBoxNetFlt: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.Mac), &pThis->u.s.Mac));
1355 return VINF_SUCCESS;
1356}
1357
1358
1359static int vboxNetFltLinuxUnregisterDevice(PVBOXNETFLTINS pThis, struct net_device *pDev)
1360{
1361 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1362
1363 Assert(!pThis->fDisconnectedFromHost);
1364
1365#ifdef VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
1366 vboxNetFltLinuxUnhookDev(pThis, pDev);
1367#endif
1368
1369 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1370 ASMAtomicWriteBool(&pThis->u.s.fRegistered, false);
1371 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
1372 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, NULL);
1373 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1374
1375 dev_remove_pack(&pThis->u.s.PacketType);
1376 skb_queue_purge(&pThis->u.s.XmitQueue);
1377 Log(("vboxNetFltLinuxUnregisterDevice: this=%p: Packet handler removed, xmit queue purged.\n", pThis));
1378 Log(("vboxNetFltLinuxUnregisterDevice: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
1379 dev_put(pDev);
1380
1381 return NOTIFY_OK;
1382}
1383
1384static int vboxNetFltLinuxDeviceIsUp(PVBOXNETFLTINS pThis, struct net_device *pDev)
1385{
1386 /* Check if we are not suspended and promiscuous mode has not been set. */
1387 if (ASMAtomicUoReadBool(&pThis->fActive) && !ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
1388 {
1389 /* Note that there is no need for locking as the kernel got hold of the lock already. */
1390 dev_set_promiscuity(pDev, 1);
1391 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, true);
1392 Log(("vboxNetFltLinuxDeviceIsUp: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1393 }
1394 else
1395 Log(("vboxNetFltLinuxDeviceIsUp: no need to enable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1396 return NOTIFY_OK;
1397}
1398
1399static int vboxNetFltLinuxDeviceGoingDown(PVBOXNETFLTINS pThis, struct net_device *pDev)
1400{
1401 /* Undo promiscuous mode if we has set it. */
1402 if (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
1403 {
1404 /* Note that there is no need for locking as the kernel got hold of the lock already. */
1405 dev_set_promiscuity(pDev, -1);
1406 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, false);
1407 Log(("vboxNetFltLinuxDeviceGoingDown: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1408 }
1409 else
1410 Log(("vboxNetFltLinuxDeviceGoingDown: no need to disable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1411 return NOTIFY_OK;
1412}
1413
1414#ifdef LOG_ENABLED
1415/** Stringify the NETDEV_XXX constants. */
1416static const char *vboxNetFltLinuxGetNetDevEventName(unsigned long ulEventType)
1417{
1418 const char *pszEvent = "NETDRV_<unknown>";
1419 switch (ulEventType)
1420 {
1421 case NETDEV_REGISTER: pszEvent = "NETDEV_REGISTER"; break;
1422 case NETDEV_UNREGISTER: pszEvent = "NETDEV_UNREGISTER"; break;
1423 case NETDEV_UP: pszEvent = "NETDEV_UP"; break;
1424 case NETDEV_DOWN: pszEvent = "NETDEV_DOWN"; break;
1425 case NETDEV_REBOOT: pszEvent = "NETDEV_REBOOT"; break;
1426 case NETDEV_CHANGENAME: pszEvent = "NETDEV_CHANGENAME"; break;
1427 case NETDEV_CHANGE: pszEvent = "NETDEV_CHANGE"; break;
1428 case NETDEV_CHANGEMTU: pszEvent = "NETDEV_CHANGEMTU"; break;
1429 case NETDEV_CHANGEADDR: pszEvent = "NETDEV_CHANGEADDR"; break;
1430 case NETDEV_GOING_DOWN: pszEvent = "NETDEV_GOING_DOWN"; break;
1431# ifdef NETDEV_FEAT_CHANGE
1432 case NETDEV_FEAT_CHANGE: pszEvent = "NETDEV_FEAT_CHANGE"; break;
1433# endif
1434 }
1435 return pszEvent;
1436}
1437#endif /* LOG_ENABLED */
1438
1439/**
1440 * Callback for listening to netdevice events.
1441 *
1442 * This works the rediscovery, clean up on unregistration, promiscuity on
1443 * up/down, and GSO feature changes from ethtool.
1444 *
1445 * @returns NOTIFY_OK
1446 * @param self Pointer to our notifier registration block.
1447 * @param ulEventType The event.
1448 * @param ptr Event specific, but it is usually the device it
1449 * relates to.
1450 */
1451static int vboxNetFltLinuxNotifierCallback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
1452
1453{
1454 PVBOXNETFLTINS pThis = VBOX_FLT_NB_TO_INST(self);
1455 struct net_device *pDev = (struct net_device *)ptr;
1456 int rc = NOTIFY_OK;
1457
1458 Log(("VBoxNetFlt: got event %s(0x%lx) on %s, pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
1459 vboxNetFltLinuxGetNetDevEventName(ulEventType), ulEventType, pDev->name, pDev, pThis, ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev)));
1460 if ( ulEventType == NETDEV_REGISTER
1461 && !strcmp(pDev->name, pThis->szName))
1462 {
1463 vboxNetFltLinuxAttachToInterface(pThis, pDev);
1464 }
1465 else
1466 {
1467 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
1468 if (pDev == ptr)
1469 {
1470 switch (ulEventType)
1471 {
1472 case NETDEV_UNREGISTER:
1473 rc = vboxNetFltLinuxUnregisterDevice(pThis, pDev);
1474 break;
1475 case NETDEV_UP:
1476 rc = vboxNetFltLinuxDeviceIsUp(pThis, pDev);
1477 break;
1478 case NETDEV_GOING_DOWN:
1479 rc = vboxNetFltLinuxDeviceGoingDown(pThis, pDev);
1480 break;
1481 case NETDEV_CHANGENAME:
1482 break;
1483#ifdef NETDEV_FEAT_CHANGE
1484 case NETDEV_FEAT_CHANGE:
1485 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
1486 break;
1487#endif
1488 }
1489 }
1490 }
1491
1492 return rc;
1493}
1494
1495bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
1496{
1497 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
1498}
1499
1500int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst)
1501{
1502 struct net_device * pDev;
1503 int err;
1504 int rc = VINF_SUCCESS;
1505
1506 LogFlow(("vboxNetFltPortOsXmit: pThis=%p (%s)\n", pThis, pThis->szName));
1507
1508 pDev = vboxNetFltLinuxRetainNetDev(pThis);
1509 if (pDev)
1510 {
1511 /*
1512 * Create a sk_buff for the gather list and push it onto the wire.
1513 */
1514 if (fDst & INTNETTRUNKDIR_WIRE)
1515 {
1516 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, true);
1517 if (pBuf)
1518 {
1519 vboxNetFltDumpPacket(pSG, true, "wire", 1);
1520 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
1521 Log4(("vboxNetFltPortOsXmit: dev_queue_xmit(%p)\n", pBuf));
1522 err = dev_queue_xmit(pBuf);
1523 if (err)
1524 rc = RTErrConvertFromErrno(err);
1525 }
1526 else
1527 rc = VERR_NO_MEMORY;
1528 }
1529
1530 /*
1531 * Create a sk_buff for the gather list and push it onto the host stack.
1532 */
1533 if (fDst & INTNETTRUNKDIR_HOST)
1534 {
1535 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, false);
1536 if (pBuf)
1537 {
1538 vboxNetFltDumpPacket(pSG, true, "host", (fDst & INTNETTRUNKDIR_WIRE) ? 0 : 1);
1539 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
1540 Log4(("vboxNetFltPortOsXmit: netif_rx_ni(%p)\n", pBuf));
1541 err = netif_rx_ni(pBuf);
1542 if (err)
1543 rc = RTErrConvertFromErrno(err);
1544 }
1545 else
1546 rc = VERR_NO_MEMORY;
1547 }
1548
1549 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1550 }
1551
1552 return rc;
1553}
1554
1555
1556bool vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis)
1557{
1558 bool fRc = false;
1559 struct net_device * pDev = vboxNetFltLinuxRetainNetDev(pThis);
1560 if (pDev)
1561 {
1562 fRc = !!(pDev->promiscuity - (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet) & 1));
1563 LogFlow(("vboxNetFltPortOsIsPromiscuous: returns %d, pDev->promiscuity=%d, fPromiscuousSet=%d\n",
1564 fRc, pDev->promiscuity, pThis->u.s.fPromiscuousSet));
1565 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1566 }
1567 return fRc;
1568}
1569
1570
1571void vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac)
1572{
1573 *pMac = pThis->u.s.Mac;
1574}
1575
1576
1577bool vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac)
1578{
1579 /* ASSUMES that the MAC address never changes. */
1580 return pThis->u.s.Mac.au16[0] == pMac->au16[0]
1581 && pThis->u.s.Mac.au16[1] == pMac->au16[1]
1582 && pThis->u.s.Mac.au16[2] == pMac->au16[2];
1583}
1584
1585
1586void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1587{
1588 struct net_device * pDev;
1589
1590 LogFlow(("vboxNetFltPortOsSetActive: pThis=%p (%s), fActive=%s, fDisablePromiscuous=%s\n",
1591 pThis, pThis->szName, fActive?"true":"false",
1592 pThis->fDisablePromiscuous?"true":"false"));
1593
1594 if (pThis->fDisablePromiscuous)
1595 return;
1596
1597 pDev = vboxNetFltLinuxRetainNetDev(pThis);
1598 if (pDev)
1599 {
1600 /*
1601 * This api is a bit weird, the best reference is the code.
1602 *
1603 * Also, we have a bit or race conditions wrt the maintance of
1604 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1605 */
1606#ifdef LOG_ENABLED
1607 u_int16_t fIf;
1608 unsigned const cPromiscBefore = pDev->promiscuity;
1609#endif
1610 if (fActive)
1611 {
1612 Assert(!pThis->u.s.fPromiscuousSet);
1613
1614 rtnl_lock();
1615 dev_set_promiscuity(pDev, 1);
1616 rtnl_unlock();
1617 pThis->u.s.fPromiscuousSet = true;
1618 Log(("vboxNetFltPortOsSetActive: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1619 }
1620 else
1621 {
1622 if (pThis->u.s.fPromiscuousSet)
1623 {
1624 rtnl_lock();
1625 dev_set_promiscuity(pDev, -1);
1626 rtnl_unlock();
1627 Log(("vboxNetFltPortOsSetActive: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
1628 }
1629 pThis->u.s.fPromiscuousSet = false;
1630
1631#ifdef LOG_ENABLED
1632 fIf = dev_get_flags(pDev);
1633 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, pDev->promiscuity));
1634#endif
1635 }
1636
1637 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1638 }
1639}
1640
1641
1642int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1643{
1644 /* Nothing to do here. */
1645 return VINF_SUCCESS;
1646}
1647
1648
1649int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1650{
1651 /*
1652 * Report the GSO capabilities of the host and device (if connected).
1653 */
1654#if defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
1655 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort,
1656 0
1657 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP)
1658 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP)
1659# if 0 /** @todo GSO: Test UDP offloading (UFO) on linux. */
1660 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_UDP)
1661 | RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_UDP)
1662# endif
1663 , INTNETTRUNKDIR_HOST);
1664
1665#endif
1666 vboxNetFltLinuxReportNicGsoCapabilities(pThis);
1667
1668 return VINF_SUCCESS;
1669}
1670
1671
1672void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1673{
1674 struct net_device *pDev;
1675 bool fRegistered;
1676 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1677
1678#ifdef VBOXNETFLT_WITH_FILTER_HOST2GUEST_SKBS_EXPERIMENT
1679 vboxNetFltLinuxUnhookDev(pThis, NULL);
1680#endif
1681
1682 /** @todo This code may race vboxNetFltLinuxUnregisterDevice (very very
1683 * unlikely, but none the less). Since it doesn't actually update the
1684 * state (just reads it), it is likely to panic in some interesting
1685 * ways. */
1686
1687 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1688 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
1689 fRegistered = ASMAtomicUoReadBool(&pThis->u.s.fRegistered);
1690 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1691
1692 if (fRegistered)
1693 {
1694 dev_remove_pack(&pThis->u.s.PacketType);
1695 skb_queue_purge(&pThis->u.s.XmitQueue);
1696 Log(("vboxNetFltOsDeleteInstance: this=%p: Packet handler removed, xmit queue purged.\n", pThis));
1697 Log(("vboxNetFltOsDeleteInstance: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
1698 dev_put(pDev);
1699 }
1700 Log(("vboxNetFltOsDeleteInstance: this=%p: Notifier removed.\n", pThis));
1701 unregister_netdevice_notifier(&pThis->u.s.Notifier);
1702 module_put(THIS_MODULE);
1703}
1704
1705
1706int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1707{
1708 int err;
1709 NOREF(pvContext);
1710
1711 pThis->u.s.Notifier.notifier_call = vboxNetFltLinuxNotifierCallback;
1712 err = register_netdevice_notifier(&pThis->u.s.Notifier);
1713 if (err)
1714 return VERR_INTNET_FLT_IF_FAILED;
1715 if (!pThis->u.s.fRegistered)
1716 {
1717 unregister_netdevice_notifier(&pThis->u.s.Notifier);
1718 LogRel(("VBoxNetFlt: failed to find %s.\n", pThis->szName));
1719 return VERR_INTNET_FLT_IF_NOT_FOUND;
1720 }
1721
1722 Log(("vboxNetFltOsInitInstance: this=%p: Notifier installed.\n", pThis));
1723 if ( pThis->fDisconnectedFromHost
1724 || !try_module_get(THIS_MODULE))
1725 return VERR_INTNET_FLT_IF_FAILED;
1726
1727 return VINF_SUCCESS;
1728}
1729
1730int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1731{
1732 /*
1733 * Init the linux specific members.
1734 */
1735 pThis->u.s.pDev = NULL;
1736 pThis->u.s.fRegistered = false;
1737 pThis->u.s.fPromiscuousSet = false;
1738 memset(&pThis->u.s.PacketType, 0, sizeof(pThis->u.s.PacketType));
1739 skb_queue_head_init(&pThis->u.s.XmitQueue);
1740#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1741 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask);
1742#else
1743 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask, &pThis->u.s.XmitTask);
1744#endif
1745
1746 return VINF_SUCCESS;
1747}
1748
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