VirtualBox

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

Last change on this file since 31675 was 31675, checked in by vboxsync, 14 years ago

vboxnetflt: pre 2.6.18 kernel compilation fixes

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