VirtualBox

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

Last change on this file since 38549 was 38549, checked in by vboxsync, 13 years ago

GSO: UDP fragmentation offloading (#5846)

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