VirtualBox

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

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

vboxnetflt: host-only checksum fix (xtracker 5020)

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