VirtualBox

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

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

VBoxNetFlt: Deal with multi segment SG buffers. TODO: One weird windows code path.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.3 KB
Line 
1/* $Id: VBoxNetFlt-linux.c 28070 2010-04-07 23:20:09Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Linux Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include "the-linux-kernel.h"
26#include "version-generated.h"
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/rtnetlink.h>
30#include <linux/miscdevice.h>
31#include <linux/ip.h>
32
33#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
34#include <VBox/log.h>
35#include <VBox/err.h>
36#include <VBox/intnetinline.h>
37#include <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/log.h>
45#include <iprt/mp.h>
46#include <iprt/mem.h>
47#include <iprt/time.h>
48
49#define VBOXNETFLT_OS_SPECFIC 1
50#include "../VBoxNetFltInternal.h"
51
52#define VBOX_FLT_NB_TO_INST(pNB) ((PVBOXNETFLTINS)((uint8_t *)pNB - \
53 RT_OFFSETOF(VBOXNETFLTINS, u.s.Notifier)))
54#define VBOX_FLT_PT_TO_INST(pPT) ((PVBOXNETFLTINS)((uint8_t *)pPT - \
55 RT_OFFSETOF(VBOXNETFLTINS, u.s.PacketType)))
56#define VBOX_FLT_XT_TO_INST(pXT) ((PVBOXNETFLTINS)((uint8_t *)pXT - \
57 RT_OFFSETOF(VBOXNETFLTINS, u.s.XmitTask)))
58
59#define VBOX_GET_PCOUNT(pDev) (pDev->promiscuity)
60
61#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
62# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb_reset_network_header(skb)
63# define VBOX_SKB_RESET_MAC_HDR(skb) skb_reset_mac_header(skb)
64#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) */
65# define VBOX_SKB_RESET_NETWORK_HDR(skb) skb->nh.raw = skb->data
66# define VBOX_SKB_RESET_MAC_HDR(skb) skb->mac.raw = skb->data
67#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) */
68
69#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
70# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb)
71#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) */
72# define CHECKSUM_PARTIAL CHECKSUM_HW
73# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
74# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb, 0)
75# else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10) */
76# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
77# define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(&skb, 0)
78# else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 7) */
79# define VBOX_SKB_CHECKSUM_HELP(skb) (!skb_checksum_help(skb))
80# endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 7) */
81# endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10) */
82#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) */
83
84#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
85# define VBOX_SKB_IS_GSO(skb) skb_is_gso(skb)
86 /* No features, very dumb device */
87# define VBOX_SKB_GSO_SEGMENT(skb) skb_gso_segment(skb, 0)
88#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
89# define VBOX_SKB_IS_GSO(skb) false
90# define VBOX_SKB_GSO_SEGMENT(skb) NULL
91#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
92
93#ifndef NET_IP_ALIGN
94# define NET_IP_ALIGN 2
95#endif
96
97#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
98unsigned dev_get_flags(const struct net_device *dev)
99{
100 unsigned flags;
101
102 flags = (dev->flags & ~(IFF_PROMISC |
103 IFF_ALLMULTI |
104 IFF_RUNNING)) |
105 (dev->gflags & (IFF_PROMISC |
106 IFF_ALLMULTI));
107
108 if (netif_running(dev) && netif_carrier_ok(dev))
109 flags |= IFF_RUNNING;
110
111 return flags;
112}
113#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) */
114
115/*******************************************************************************
116* Internal Functions *
117*******************************************************************************/
118static int VBoxNetFltLinuxInit(void);
119static void VBoxNetFltLinuxUnload(void);
120
121
122/*******************************************************************************
123* Global Variables *
124*******************************************************************************/
125/**
126 * The (common) global data.
127 */
128static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
129
130module_init(VBoxNetFltLinuxInit);
131module_exit(VBoxNetFltLinuxUnload);
132
133MODULE_AUTHOR("Sun Microsystems, Inc.");
134MODULE_DESCRIPTION("VirtualBox Network Filter Driver");
135MODULE_LICENSE("GPL");
136#ifdef MODULE_VERSION
137MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
138#endif
139
140
141/**
142 * Initialize module.
143 *
144 * @returns appropriate status code.
145 */
146static int __init VBoxNetFltLinuxInit(void)
147{
148 int rc;
149 /*
150 * Initialize IPRT.
151 */
152 rc = RTR0Init(0);
153 if (RT_SUCCESS(rc))
154 {
155 Log(("VBoxNetFltLinuxInit\n"));
156
157 /*
158 * Initialize the globals and connect to the support driver.
159 *
160 * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
161 * for establishing the connect to the support driver.
162 */
163 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
164 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
165 if (RT_SUCCESS(rc))
166 {
167 LogRel(("VBoxNetFlt: Successfully started.\n"));
168 return 0;
169 }
170
171 LogRel(("VBoxNetFlt: failed to initialize device extension (rc=%d)\n", rc));
172 RTR0Term();
173 }
174 else
175 LogRel(("VBoxNetFlt: failed to initialize IPRT (rc=%d)\n", rc));
176
177 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
178 return -RTErrConvertToErrno(rc);
179}
180
181
182/**
183 * Unload the module.
184 *
185 * @todo We have to prevent this if we're busy!
186 */
187static void __exit VBoxNetFltLinuxUnload(void)
188{
189 int rc;
190 Log(("VBoxNetFltLinuxUnload\n"));
191 Assert(vboxNetFltCanUnload(&g_VBoxNetFltGlobals));
192
193 /*
194 * Undo the work done during start (in reverse order).
195 */
196 rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
197 AssertRC(rc); NOREF(rc);
198
199 RTR0Term();
200
201 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
202
203 Log(("VBoxNetFltLinuxUnload - done\n"));
204}
205
206
207/**
208 * Reads and retains the host interface handle.
209 *
210 * @returns The handle, NULL if detached.
211 * @param pThis
212 */
213DECLINLINE(struct net_device *) vboxNetFltLinuxRetainNetDev(PVBOXNETFLTINS pThis)
214{
215#if 0
216 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
217 struct net_device *pDev = NULL;
218
219 Log(("vboxNetFltLinuxRetainNetDev\n"));
220 /*
221 * Be careful here to avoid problems racing the detached callback.
222 */
223 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
224 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
225 {
226 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
227 if (pDev)
228 {
229 dev_hold(pDev);
230 Log(("vboxNetFltLinuxRetainNetDev: Device %p(%s) retained. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
231 }
232 }
233 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
234
235 Log(("vboxNetFltLinuxRetainNetDev - done\n"));
236 return pDev;
237#else
238 return (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
239#endif
240}
241
242
243/**
244 * Release the host interface handle previously retained
245 * by vboxNetFltLinuxRetainNetDev.
246 *
247 * @param pThis The instance.
248 * @param pDev The vboxNetFltLinuxRetainNetDev
249 * return value, NULL is fine.
250 */
251DECLINLINE(void) vboxNetFltLinuxReleaseNetDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
252{
253#if 0
254 Log(("vboxNetFltLinuxReleaseNetDev\n"));
255 NOREF(pThis);
256 if (pDev)
257 {
258 dev_put(pDev);
259 Log(("vboxNetFltLinuxReleaseNetDev: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
260 }
261 Log(("vboxNetFltLinuxReleaseNetDev - done\n"));
262#endif
263}
264
265#define VBOXNETFLT_CB_TAG(skb) (0xA1C90000 | (skb->dev->ifindex & 0xFFFF))
266#define VBOXNETFLT_SKB_TAG(skb) (*(uint32_t*)&((skb)->cb[sizeof((skb)->cb)-sizeof(uint32_t)]))
267
268/**
269 * Checks whether this is an mbuf created by vboxNetFltLinuxMBufFromSG,
270 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
271 *
272 * @returns true / false accordingly.
273 * @param pBuf The sk_buff.
274 */
275DECLINLINE(bool) vboxNetFltLinuxSkBufIsOur(struct sk_buff *pBuf)
276{
277 return VBOXNETFLT_SKB_TAG(pBuf) == VBOXNETFLT_CB_TAG(pBuf);
278}
279
280
281/**
282 * Internal worker that create a linux sk_buff for a
283 * (scatter/)gather list.
284 *
285 * @returns Pointer to the sk_buff.
286 * @param pThis The instance.
287 * @param pSG The (scatter/)gather list.
288 */
289static struct sk_buff *vboxNetFltLinuxSkBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG, bool fDstWire)
290{
291 struct sk_buff *pPkt;
292 struct net_device *pDev;
293
294 if (pSG->cbTotal == 0)
295 {
296 LogRel(("VBoxNetFlt: Dropped empty packet coming from internal network.\n"));
297 return NULL;
298 }
299
300 /*
301 * Allocate a packet and copy over the data.
302 */
303 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
304 pPkt = dev_alloc_skb(pSG->cbTotal + NET_IP_ALIGN);
305 if (RT_UNLIKELY(!pPkt))
306 {
307 Log(("vboxNetFltLinuxSkBufFromSG: Failed to allocate sk_buff(%u).\n", pSG->cbTotal));
308 pSG->pvUserData = NULL;
309 return NULL;
310 }
311
312 pPkt->dev = pDev;
313
314 /* Align IP header on 16-byte boundary: 2 + 14 (ethernet hdr size). */
315 skb_reserve(pPkt, NET_IP_ALIGN);
316
317 /* Copy the segments. */
318 skb_put(pPkt, pSG->cbTotal);
319 INTNETSgRead(pSG, pPkt->data);
320
321 /* Set protocol and packet_type fields. */
322 pPkt->protocol = eth_type_trans(pPkt, pDev);
323 pPkt->ip_summed = CHECKSUM_NONE;
324 if (fDstWire)
325 {
326 VBOX_SKB_RESET_NETWORK_HDR(pPkt);
327 /* Restore ethernet header back. */
328 skb_push(pPkt, ETH_HLEN);
329 VBOX_SKB_RESET_MAC_HDR(pPkt);
330 }
331 VBOXNETFLT_SKB_TAG(pPkt) = VBOXNETFLT_CB_TAG(pPkt);
332
333 return pPkt;
334}
335
336
337/**
338 * Initializes a SG list from an sk_buff.
339 *
340 * @returns Number of segments.
341 * @param pThis The instance.
342 * @param pBuf The sk_buff.
343 * @param pSG The SG.
344 * @param pvFrame The frame pointer, optional.
345 * @param cSegs The number of segments allocated for the SG.
346 * This should match the number in the mbuf exactly!
347 * @param fSrc The source of the frame.
348 */
349DECLINLINE(void) vboxNetFltLinuxSkBufToSG(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
350{
351 int i;
352 NOREF(pThis);
353
354 Assert(!skb_shinfo(pBuf)->frag_list);
355
356 if (fSrc & INTNETTRUNKDIR_WIRE)
357 {
358 /*
359 * The packet came from wire, ethernet header was removed by device driver.
360 * Restore it.
361 */
362 skb_push(pBuf, ETH_HLEN);
363 }
364
365 INTNETSgInitTempSegs(pSG, pBuf->len, cSegs, 0 /*cSegsUsed*/);
366
367#ifdef VBOXNETFLT_SG_SUPPORT
368 pSG->aSegs[0].cb = skb_headlen(pBuf);
369 pSG->aSegs[0].pv = pBuf->data;
370 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
371
372 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
373 {
374 skb_frag_t *pFrag = &skb_shinfo(pBuf)->frags[i];
375 pSG->aSegs[i+1].cb = pFrag->size;
376 pSG->aSegs[i+1].pv = kmap(pFrag->page);
377 printk("%p = kmap()\n", pSG->aSegs[i+1].pv);
378 pSG->aSegs[i+1].Phys = NIL_RTHCPHYS;
379 }
380 ++i;
381
382#else
383 pSG->aSegs[0].cb = pBuf->len;
384 pSG->aSegs[0].pv = pBuf->data;
385 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
386 i = 1;
387#endif
388
389 pSG->cSegsUsed = i;
390
391#ifdef PADD_RUNT_FRAMES_FROM_HOST
392 /*
393 * Add a trailer if the frame is too small.
394 *
395 * Since we're getting to the packet before it is framed, it has not
396 * yet been padded. The current solution is to add a segment pointing
397 * to a buffer containing all zeros and pray that works for all frames...
398 */
399 if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
400 {
401 static uint8_t const s_abZero[128] = {0};
402
403 AssertReturnVoid(i < cSegs);
404
405 pSG->aSegs[i].Phys = NIL_RTHCPHYS;
406 pSG->aSegs[i].pv = (void *)&s_abZero[0];
407 pSG->aSegs[i].cb = 60 - pSG->cbTotal;
408 pSG->cbTotal = 60;
409 pSG->cSegsUsed++;
410 Assert(i + 1 <= pSG->cSegsAlloc)
411 }
412#endif
413
414 Log4(("vboxNetFltLinuxSkBufToSG: allocated=%d, segments=%d frags=%d next=%p frag_list=%p pkt_type=%x fSrc=%x\n",
415 pSG->cSegsAlloc, pSG->cSegsUsed, skb_shinfo(pBuf)->nr_frags, pBuf->next, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, fSrc));
416 for (i = 0; i < pSG->cSegsUsed; i++)
417 Log4(("vboxNetFltLinuxSkBufToSG: #%d: cb=%d pv=%p\n",
418 i, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
419}
420
421/**
422 * Packet handler,
423 *
424 * @returns 0 or EJUSTRETURN.
425 * @param pThis The instance.
426 * @param pMBuf The mbuf.
427 * @param pvFrame The start of the frame, optional.
428 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
429 * @param eProtocol The protocol.
430 */
431#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
432static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
433 struct net_device *pSkbDev,
434 struct packet_type *pPacketType,
435 struct net_device *pOrigDev)
436#else
437static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
438 struct net_device *pSkbDev,
439 struct packet_type *pPacketType)
440#endif
441{
442 PVBOXNETFLTINS pThis;
443 struct net_device *pDev;
444 LogFlow(("vboxNetFltLinuxPacketHandler: pBuf=%p pSkbDev=%p pPacketType=%p\n",
445 pBuf, pSkbDev, pPacketType));
446#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
447 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",
448 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));
449#else
450 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",
451 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));
452#endif
453 /*
454 * Drop it immediately?
455 */
456 if (!pBuf)
457 return 0;
458
459 pThis = VBOX_FLT_PT_TO_INST(pPacketType);
460 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
461 if (pThis->u.s.pDev != pSkbDev)
462 {
463 Log(("vboxNetFltLinuxPacketHandler: Devices do not match, pThis may be wrong! pThis=%p\n", pThis));
464 return 0;
465 }
466
467 Log4(("vboxNetFltLinuxPacketHandler: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
468 if (vboxNetFltLinuxSkBufIsOur(pBuf))
469 {
470 Log2(("vboxNetFltLinuxPacketHandler: got our own sk_buff, drop it.\n"));
471 dev_kfree_skb(pBuf);
472 return 0;
473 }
474
475#ifndef VBOXNETFLT_SG_SUPPORT
476 {
477 /*
478 * Get rid of fragmented packets, they cause too much trouble.
479 */
480 struct sk_buff *pCopy = skb_copy(pBuf, GFP_ATOMIC);
481 kfree_skb(pBuf);
482 if (!pCopy)
483 {
484 LogRel(("VBoxNetFlt: Failed to allocate packet buffer, dropping the packet.\n"));
485 return 0;
486 }
487 pBuf = pCopy;
488#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
489 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",
490 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));
491#else
492 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",
493 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));
494#endif
495 }
496#endif
497
498 /* Add the packet to transmit queue and schedule the bottom half. */
499 skb_queue_tail(&pThis->u.s.XmitQueue, pBuf);
500 schedule_work(&pThis->u.s.XmitTask);
501 Log4(("vboxNetFltLinuxPacketHandler: scheduled work %p for sk_buff %p\n",
502 &pThis->u.s.XmitTask, pBuf));
503 /* It does not really matter what we return, it is ignored by the kernel. */
504 return 0;
505}
506
507static unsigned vboxNetFltLinuxSGSegments(PVBOXNETFLTINS pThis, struct sk_buff *pBuf)
508{
509#ifdef VBOXNETFLT_SG_SUPPORT
510 unsigned cSegs = 1 + skb_shinfo(pBuf)->nr_frags;
511#else
512 unsigned cSegs = 1;
513#endif
514#ifdef PADD_RUNT_FRAMES_FROM_HOST
515 /*
516 * Add a trailer if the frame is too small.
517 */
518 if (pBuf->len < 60)
519 cSegs++;
520#endif
521 return cSegs;
522}
523
524/* WARNING! This function should only be called after vboxNetFltLinuxSkBufToSG()! */
525static void vboxNetFltLinuxFreeSkBuff(struct sk_buff *pBuf, PINTNETSG pSG)
526{
527#ifdef VBOXNETFLT_SG_SUPPORT
528 int i;
529
530 for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
531 {
532 printk("kunmap(%p)\n", pSG->aSegs[i+1].pv);
533 kunmap(pSG->aSegs[i+1].pv);
534 }
535#endif
536
537 dev_kfree_skb(pBuf);
538}
539
540#ifndef LOG_ENABLED
541# define vboxNetFltDumpPacket(a, b, c, d) do {} while (0)
542#else
543static void vboxNetFltDumpPacket(PINTNETSG pSG, bool fEgress, const char *pszWhere, int iIncrement)
544{
545 uint8_t *pInt, *pExt;
546 static int iPacketNo = 1;
547 iPacketNo += iIncrement;
548 if (fEgress)
549 {
550 pExt = pSG->aSegs[0].pv;
551 pInt = pExt + 6;
552 }
553 else
554 {
555 pInt = pSG->aSegs[0].pv;
556 pExt = pInt + 6;
557 }
558 Log(("VBoxNetFlt: (int)%02x:%02x:%02x:%02x:%02x:%02x"
559 " %s (%s)%02x:%02x:%02x:%02x:%02x:%02x (%u bytes) packet #%u\n",
560 pInt[0], pInt[1], pInt[2], pInt[3], pInt[4], pInt[5],
561 fEgress ? "-->" : "<--", pszWhere,
562 pExt[0], pExt[1], pExt[2], pExt[3], pExt[4], pExt[5],
563 pSG->cbTotal, iPacketNo));
564 Log3(("%.*Rhxd\n", pSG->aSegs[0].cb, pSG->aSegs[0].pv));
565}
566#endif
567
568static int vboxNetFltLinuxForwardSegment(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, uint32_t fSrc)
569{
570 unsigned cSegs = vboxNetFltLinuxSGSegments(pThis, pBuf);
571 if (cSegs < MAX_SKB_FRAGS)
572 {
573 uint8_t *pTmp;
574 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
575 if (!pSG)
576 {
577 Log(("VBoxNetFlt: Failed to allocate SG buffer.\n"));
578 return VERR_NO_MEMORY;
579 }
580 vboxNetFltLinuxSkBufToSG(pThis, pBuf, pSG, cSegs, fSrc);
581
582 pTmp = pSG->aSegs[0].pv;
583 vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
584 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, fSrc);
585 Log4(("VBoxNetFlt: Dropping the sk_buff.\n"));
586 vboxNetFltLinuxFreeSkBuff(pBuf, pSG);
587 }
588
589 return VINF_SUCCESS;
590}
591
592static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf)
593{
594 uint32_t fSrc = pBuf->pkt_type == PACKET_OUTGOING ? INTNETTRUNKDIR_HOST : INTNETTRUNKDIR_WIRE;
595
596 if (VBOX_SKB_IS_GSO(pBuf))
597 {
598 /* Need to segment the packet */
599 struct sk_buff *pNext, *pSegment;
600#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
601 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",
602 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));
603#endif
604
605 pSegment = VBOX_SKB_GSO_SEGMENT(pBuf);
606 if (IS_ERR(pSegment))
607 {
608 dev_kfree_skb(pBuf);
609 LogRel(("VBoxNetFlt: Failed to segment a packet (%d).\n", PTR_ERR(pBuf)));
610 return;
611 }
612 for (; pSegment; pSegment = pNext)
613 {
614#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
615 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",
616 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));
617#endif
618 pNext = pSegment->next;
619 pSegment->next = 0;
620 vboxNetFltLinuxForwardSegment(pThis, pSegment, fSrc);
621 }
622 dev_kfree_skb(pBuf);
623 }
624 else
625 {
626 if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
627 {
628#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
629 /*
630 * Try to work around the problem with CentOS 4.7 and 5.2 (2.6.9
631 * and 2.6.18 kernels), they pass wrong 'h' pointer down. We take IP
632 * header length from the header itself and reconstruct 'h' pointer
633 * to TCP (or whatever) header.
634 */
635 unsigned char *tmp = pBuf->h.raw;
636 if (pBuf->h.raw == pBuf->nh.raw && pBuf->protocol == htons(ETH_P_IP))
637 pBuf->h.raw = pBuf->nh.raw + pBuf->nh.iph->ihl * 4;
638#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
639 if (VBOX_SKB_CHECKSUM_HELP(pBuf))
640 {
641 LogRel(("VBoxNetFlt: Failed to compute checksum, dropping the packet.\n"));
642 dev_kfree_skb(pBuf);
643 return;
644 }
645#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
646 /* Restore the original (wrong) pointer. */
647 pBuf->h.raw = tmp;
648#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
649 }
650 vboxNetFltLinuxForwardSegment(pThis, pBuf, fSrc);
651 }
652 /*
653 * Create a (scatter/)gather list for the sk_buff and feed it to the internal network.
654 */
655}
656
657#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
658static void vboxNetFltLinuxXmitTask(struct work_struct *pWork)
659#else
660static void vboxNetFltLinuxXmitTask(void *pWork)
661#endif
662{
663 struct sk_buff *pBuf;
664 bool fActive;
665 PVBOXNETFLTINS pThis;
666 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
667
668 Log4(("vboxNetFltLinuxXmitTask: Got work %p.\n", pWork));
669 pThis = VBOX_FLT_XT_TO_INST(pWork);
670 /*
671 * Active? Retain the instance and increment the busy counter.
672 */
673 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
674 fActive = ASMAtomicUoReadBool(&pThis->fActive);
675 if (fActive)
676 vboxNetFltRetain(pThis, true /* fBusy */);
677 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
678 if (!fActive)
679 return;
680
681 while ((pBuf = skb_dequeue(&pThis->u.s.XmitQueue)) != 0)
682 vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
683
684 vboxNetFltRelease(pThis, true /* fBusy */);
685}
686
687/**
688 * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
689 *
690 * @returns VBox status code.
691 * @param pThis The instance.
692 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
693 * flood the release log.
694 */
695static int vboxNetFltLinuxAttachToInterface(PVBOXNETFLTINS pThis, struct net_device *pDev)
696{
697 struct packet_type *pt;
698 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
699
700 LogFlow(("vboxNetFltLinuxAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
701
702 if (!pDev)
703 {
704 Log(("VBoxNetFlt: failed to find device '%s'\n", pThis->szName));
705 return VERR_INTNET_FLT_IF_NOT_FOUND;
706 }
707
708 dev_hold(pDev);
709 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
710 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, pDev);
711 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
712
713 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) retained. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
714 Log(("vboxNetFltLinuxAttachToInterface: Got pDev=%p pThis=%p pThis->u.s.pDev=%p\n", pDev, pThis, ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev)));
715 /*
716 * Get the mac address while we still have a valid ifnet reference.
717 */
718 memcpy(&pThis->u.s.Mac, pDev->dev_addr, sizeof(pThis->u.s.Mac));
719
720 pt = &pThis->u.s.PacketType;
721 pt->type = __constant_htons(ETH_P_ALL);
722 pt->dev = pDev;
723 pt->func = vboxNetFltLinuxPacketHandler;
724 dev_add_pack(pt);
725 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
726 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
727 if (pDev)
728 {
729 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
730 ASMAtomicUoWriteBool(&pThis->u.s.fRegistered, true);
731 pDev = NULL; /* don't dereference it */
732 }
733 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
734 Log(("vboxNetFltLinuxAttachToInterface: this=%p: Packet handler installed.\n", pThis));
735
736 /* Release the interface on failure. */
737 if (pDev)
738 {
739 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
740 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, NULL);
741 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
742 dev_put(pDev);
743 Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
744 }
745
746 LogRel(("VBoxNetFlt: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.Mac), &pThis->u.s.Mac));
747 return VINF_SUCCESS;
748}
749
750
751static int vboxNetFltLinuxUnregisterDevice(PVBOXNETFLTINS pThis, struct net_device *pDev)
752{
753 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
754
755 Assert(!pThis->fDisconnectedFromHost);
756 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
757 ASMAtomicWriteBool(&pThis->u.s.fRegistered, false);
758 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
759 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.pDev, NULL);
760 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
761
762 dev_remove_pack(&pThis->u.s.PacketType);
763 skb_queue_purge(&pThis->u.s.XmitQueue);
764 Log(("vboxNetFltLinuxUnregisterDevice: this=%p: Packet handler removed, xmit queue purged.\n", pThis));
765 Log(("vboxNetFltLinuxUnregisterDevice: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
766 dev_put(pDev);
767
768 return NOTIFY_OK;
769}
770
771static int vboxNetFltLinuxDeviceIsUp(PVBOXNETFLTINS pThis, struct net_device *pDev)
772{
773 /* Check if we are not suspended and promiscuous mode has not been set. */
774 if (ASMAtomicUoReadBool(&pThis->fActive) && !ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
775 {
776 /* Note that there is no need for locking as the kernel got hold of the lock already. */
777 dev_set_promiscuity(pDev, 1);
778 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, true);
779 Log(("vboxNetFltLinuxDeviceIsUp: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
780 }
781 else
782 Log(("vboxNetFltLinuxDeviceIsUp: no need to enable promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
783 return NOTIFY_OK;
784}
785
786static int vboxNetFltLinuxDeviceGoingDown(PVBOXNETFLTINS pThis, struct net_device *pDev)
787{
788 /* Undo promiscuous mode if we has set it. */
789 if (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
790 {
791 /* Note that there is no need for locking as the kernel got hold of the lock already. */
792 dev_set_promiscuity(pDev, -1);
793 ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, false);
794 Log(("vboxNetFltLinuxDeviceGoingDown: disabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
795 }
796 else
797 Log(("vboxNetFltLinuxDeviceGoingDown: no need to disable promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
798 return NOTIFY_OK;
799}
800
801static int vboxNetFltLinuxNotifierCallback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
802
803{
804 int rc = NOTIFY_OK;
805#ifdef DEBUG
806 char *pszEvent = "<unknown>";
807#endif
808 struct net_device *pDev = (struct net_device *)ptr;
809 PVBOXNETFLTINS pThis = VBOX_FLT_NB_TO_INST(self);
810
811#ifdef DEBUG
812 switch (ulEventType)
813 {
814 case NETDEV_REGISTER: pszEvent = "NETDEV_REGISTER"; break;
815 case NETDEV_UNREGISTER: pszEvent = "NETDEV_UNREGISTER"; break;
816 case NETDEV_UP: pszEvent = "NETDEV_UP"; break;
817 case NETDEV_DOWN: pszEvent = "NETDEV_DOWN"; break;
818 case NETDEV_REBOOT: pszEvent = "NETDEV_REBOOT"; break;
819 case NETDEV_CHANGENAME: pszEvent = "NETDEV_CHANGENAME"; break;
820 case NETDEV_CHANGE: pszEvent = "NETDEV_CHANGE"; break;
821 case NETDEV_CHANGEMTU: pszEvent = "NETDEV_CHANGEMTU"; break;
822 case NETDEV_CHANGEADDR: pszEvent = "NETDEV_CHANGEADDR"; break;
823 case NETDEV_GOING_DOWN: pszEvent = "NETDEV_GOING_DOWN"; break;
824 }
825 Log(("VBoxNetFlt: got event %s(0x%lx) on %s, pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
826 pszEvent, ulEventType, pDev->name, pDev, pThis, ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev)));
827#endif
828 if (ulEventType == NETDEV_REGISTER && !strcmp(pDev->name, pThis->szName))
829 {
830 vboxNetFltLinuxAttachToInterface(pThis, pDev);
831 }
832 else
833 {
834 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
835 if (pDev != ptr)
836 return NOTIFY_OK;
837 rc = NOTIFY_OK;
838 switch (ulEventType)
839 {
840 case NETDEV_UNREGISTER:
841 rc = vboxNetFltLinuxUnregisterDevice(pThis, pDev);
842 break;
843 case NETDEV_UP:
844 rc = vboxNetFltLinuxDeviceIsUp(pThis, pDev);
845 break;
846 case NETDEV_GOING_DOWN:
847 rc = vboxNetFltLinuxDeviceGoingDown(pThis, pDev);
848 break;
849 case NETDEV_CHANGENAME:
850 break;
851 }
852 }
853
854 return rc;
855}
856
857bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
858{
859 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
860}
861
862int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst)
863{
864 struct net_device * pDev;
865 int err;
866 int rc = VINF_SUCCESS;
867
868 LogFlow(("vboxNetFltPortOsXmit: pThis=%p (%s)\n", pThis, pThis->szName));
869
870 pDev = vboxNetFltLinuxRetainNetDev(pThis);
871 if (pDev)
872 {
873 /*
874 * Create a sk_buff for the gather list and push it onto the wire.
875 */
876 if (fDst & INTNETTRUNKDIR_WIRE)
877 {
878 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, true);
879 if (pBuf)
880 {
881 vboxNetFltDumpPacket(pSG, true, "wire", 1);
882 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
883 Log4(("vboxNetFltPortOsXmit: dev_queue_xmit(%p)\n", pBuf));
884 err = dev_queue_xmit(pBuf);
885 if (err)
886 rc = RTErrConvertFromErrno(err);
887 }
888 else
889 rc = VERR_NO_MEMORY;
890 }
891
892 /*
893 * Create a sk_buff for the gather list and push it onto the host stack.
894 */
895 if (fDst & INTNETTRUNKDIR_HOST)
896 {
897 struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, false);
898 if (pBuf)
899 {
900 vboxNetFltDumpPacket(pSG, true, "host", (fDst & INTNETTRUNKDIR_WIRE) ? 0 : 1);
901 Log4(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
902 Log4(("vboxNetFltPortOsXmit: netif_rx_ni(%p)\n", pBuf));
903 err = netif_rx_ni(pBuf);
904 if (err)
905 rc = RTErrConvertFromErrno(err);
906 }
907 else
908 rc = VERR_NO_MEMORY;
909 }
910
911 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
912 }
913
914 return rc;
915}
916
917
918bool vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis)
919{
920 bool fRc = false;
921 struct net_device * pDev = vboxNetFltLinuxRetainNetDev(pThis);
922 if (pDev)
923 {
924 fRc = !!(pDev->promiscuity - (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet) & 1));
925 LogFlow(("vboxNetFltPortOsIsPromiscuous: returns %d, pDev->promiscuity=%d, fPromiscuousSet=%d\n",
926 fRc, pDev->promiscuity, pThis->u.s.fPromiscuousSet));
927 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
928 }
929 return fRc;
930}
931
932
933void vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac)
934{
935 *pMac = pThis->u.s.Mac;
936}
937
938
939bool vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac)
940{
941 /* ASSUMES that the MAC address never changes. */
942 return pThis->u.s.Mac.au16[0] == pMac->au16[0]
943 && pThis->u.s.Mac.au16[1] == pMac->au16[1]
944 && pThis->u.s.Mac.au16[2] == pMac->au16[2];
945}
946
947
948void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
949{
950 struct net_device * pDev;
951
952 LogFlow(("vboxNetFltPortOsSetActive: pThis=%p (%s), fActive=%s, fDisablePromiscuous=%s\n",
953 pThis, pThis->szName, fActive?"true":"false",
954 pThis->fDisablePromiscuous?"true":"false"));
955
956 if (pThis->fDisablePromiscuous)
957 return;
958
959 pDev = vboxNetFltLinuxRetainNetDev(pThis);
960 if (pDev)
961 {
962 /*
963 * This api is a bit weird, the best reference is the code.
964 *
965 * Also, we have a bit or race conditions wrt the maintance of
966 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
967 */
968#ifdef LOG_ENABLED
969 u_int16_t fIf;
970 unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pDev);
971#endif
972 if (fActive)
973 {
974 Assert(!pThis->u.s.fPromiscuousSet);
975
976 rtnl_lock();
977 dev_set_promiscuity(pDev, 1);
978 rtnl_unlock();
979 pThis->u.s.fPromiscuousSet = true;
980 Log(("vboxNetFltPortOsSetActive: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
981 }
982 else
983 {
984 if (pThis->u.s.fPromiscuousSet)
985 {
986 rtnl_lock();
987 dev_set_promiscuity(pDev, -1);
988 rtnl_unlock();
989 Log(("vboxNetFltPortOsSetActive: disabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pDev)));
990 }
991 pThis->u.s.fPromiscuousSet = false;
992
993#ifdef LOG_ENABLED
994 fIf = dev_get_flags(pDev);
995 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pDev)));
996#endif
997 }
998
999 vboxNetFltLinuxReleaseNetDev(pThis, pDev);
1000 }
1001}
1002
1003
1004int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1005{
1006 /* Nothing to do here. */
1007 return VINF_SUCCESS;
1008}
1009
1010
1011int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1012{
1013 /* Nothing to do here. */
1014 return VINF_SUCCESS;
1015}
1016
1017
1018void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1019{
1020 struct net_device *pDev;
1021 bool fRegistered;
1022 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1023
1024 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
1025 pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
1026 fRegistered = ASMAtomicUoReadBool(&pThis->u.s.fRegistered);
1027 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
1028 if (fRegistered)
1029 {
1030 dev_remove_pack(&pThis->u.s.PacketType);
1031 skb_queue_purge(&pThis->u.s.XmitQueue);
1032 Log(("vboxNetFltOsDeleteInstance: this=%p: Packet handler removed, xmit queue purged.\n", pThis));
1033 Log(("vboxNetFltOsDeleteInstance: Device %p(%s) released. ref=%d\n", pDev, pDev->name, atomic_read(&pDev->refcnt)));
1034 dev_put(pDev);
1035 }
1036 Log(("vboxNetFltOsDeleteInstance: this=%p: Notifier removed.\n", pThis));
1037 unregister_netdevice_notifier(&pThis->u.s.Notifier);
1038 module_put(THIS_MODULE);
1039}
1040
1041
1042int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1043{
1044 int err;
1045 NOREF(pvContext);
1046
1047 pThis->u.s.Notifier.notifier_call = vboxNetFltLinuxNotifierCallback;
1048 err = register_netdevice_notifier(&pThis->u.s.Notifier);
1049 if (err)
1050 return VERR_INTNET_FLT_IF_FAILED;
1051 if (!pThis->u.s.fRegistered)
1052 {
1053 unregister_netdevice_notifier(&pThis->u.s.Notifier);
1054 LogRel(("VBoxNetFlt: failed to find %s.\n", pThis->szName));
1055 return VERR_INTNET_FLT_IF_NOT_FOUND;
1056 }
1057
1058 Log(("vboxNetFltOsInitInstance: this=%p: Notifier installed.\n", pThis));
1059 if ( pThis->fDisconnectedFromHost
1060 || !try_module_get(THIS_MODULE))
1061 return VERR_INTNET_FLT_IF_FAILED;
1062
1063 return VINF_SUCCESS;
1064}
1065
1066int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1067{
1068 /*
1069 * Init the linux specific members.
1070 */
1071 pThis->u.s.pDev = NULL;
1072 pThis->u.s.fRegistered = false;
1073 pThis->u.s.fPromiscuousSet = false;
1074 memset(&pThis->u.s.PacketType, 0, sizeof(pThis->u.s.PacketType));
1075 skb_queue_head_init(&pThis->u.s.XmitQueue);
1076#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1077 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask);
1078#else
1079 INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask, &pThis->u.s.XmitTask);
1080#endif
1081
1082 return VINF_SUCCESS;
1083}
1084
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