VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/freebsd/VBoxNetFlt-freebsd.c@ 28854

最後變更 在這個檔案從28854是 28830,由 vboxsync 提交於 15 年 前

IntNet,VBoxNetFlt: Cleaned up the locking protocol between IntNet and NetFlt. Eleminated the out-bound trunk lock that IntNet always took when calling NetFlt.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 21.1 KB
 
1/* $Id: VBoxNetFlt-freebsd.c 28830 2010-04-27 14:05:25Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), FreeBSD Specific Code.
4 */
5
6/*
7 * Copyright (c) 2009 Fredrik Lindberg <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <sys/param.h>
35#undef PVM
36#include <sys/types.h>
37#include <sys/module.h>
38#include <sys/systm.h>
39#include <sys/errno.h>
40#include <sys/kernel.h>
41#include <sys/fcntl.h>
42#include <sys/conf.h>
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/syscallsubr.h>
46#include <sys/queue.h>
47#include <sys/taskqueue.h>
48
49#include <net/if.h>
50#include <net/if_var.h>
51#include <net/if_dl.h>
52#include <net/if_types.h>
53#include <net/ethernet.h>
54
55#include <netgraph/ng_message.h>
56#include <netgraph/netgraph.h>
57#include <netgraph/ng_parse.h>
58
59#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
60#include <VBox/version.h>
61#include <VBox/err.h>
62#include <VBox/log.h>
63#include <VBox/intnetinline.h>
64#include <iprt/initterm.h>
65#include <iprt/string.h>
66#include <iprt/spinlock.h>
67#include <iprt/process.h>
68#include <iprt/assert.h>
69#include <iprt/uuid.h>
70#include <iprt/alloc.h>
71#include <iprt/err.h>
72
73#define VBOXNETFLT_OS_SPECFIC 1
74#include "../VBoxNetFltInternal.h"
75
76static int vboxnetflt_modevent(struct module *, int, void *);
77static ng_constructor_t ng_vboxnetflt_constructor;
78static ng_rcvmsg_t ng_vboxnetflt_rcvmsg;
79static ng_shutdown_t ng_vboxnetflt_shutdown;
80static ng_newhook_t ng_vboxnetflt_newhook;
81static ng_rcvdata_t ng_vboxnetflt_rcvdata;
82static ng_disconnect_t ng_vboxnetflt_disconnect;
83static int ng_vboxnetflt_mod_event(module_t mod, int event, void *data);
84
85/** Netgraph node type */
86#define NG_VBOXNETFLT_NODE_TYPE "vboxnetflt"
87/** Netgraph message cookie */
88#define NGM_VBOXNETFLT_COOKIE 0x56424f58
89
90/** Input netgraph hook name */
91#define NG_VBOXNETFLT_HOOK_IN "input"
92/** Output netgraph hook name */
93#define NG_VBOXNETFLT_HOOK_OUT "output"
94
95/** mbuf tag identifier */
96#define MTAG_VBOX 0x56424f58
97/** mbuf packet tag */
98#define PACKET_TAG_VBOX 128
99
100/*
101 * Netgraph command list, we don't support any
102 * additional commands.
103 */
104static const struct ng_cmdlist ng_vboxnetflt_cmdlist[] =
105{
106 { 0 }
107};
108
109/*
110 * Netgraph type definition
111 */
112static struct ng_type ng_vboxnetflt_typestruct =
113{
114 .version = NG_ABI_VERSION,
115 .name = NG_VBOXNETFLT_NODE_TYPE,
116 .mod_event = vboxnetflt_modevent,
117 .constructor = ng_vboxnetflt_constructor,
118 .rcvmsg = ng_vboxnetflt_rcvmsg,
119 .shutdown = ng_vboxnetflt_shutdown,
120 .newhook = ng_vboxnetflt_newhook,
121 .rcvdata = ng_vboxnetflt_rcvdata,
122 .disconnect = ng_vboxnetflt_disconnect,
123 .cmdlist = ng_vboxnetflt_cmdlist,
124};
125NETGRAPH_INIT(vboxnetflt, &ng_vboxnetflt_typestruct);
126MODULE_VERSION(ng_vboxnetflt, 1);
127MODULE_DEPEND(ng_vboxnetflt, vboxdrv, 1, 1, 1);
128
129/**
130 * The (common) global data.
131 */
132static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
133
134/**
135 * Module event handler, called from netgraph subsystem.
136 */
137static int vboxnetflt_modevent(struct module *pMod, int enmEventType, void *pvArg)
138{
139 int rc;
140
141 Log(("VBoxNetFltFreeBSDModuleEvent\n"));
142
143 switch (enmEventType)
144 {
145 case MOD_LOAD:
146 rc = RTR0Init(0);
147 if (RT_FAILURE(rc))
148 {
149 printf("RTR0Init failed %d\n", rc);
150 return RTErrConvertToErrno(rc);
151 }
152
153 memset(&g_VBoxNetFltGlobals, 0, sizeof(VBOXNETFLTGLOBALS));
154 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
155 if (RT_FAILURE(rc))
156 {
157 printf("vboxNetFltInitGlobalsAndIdc failed %d\n", rc);
158 return RTErrConvertToErrno(rc);
159 }
160 /* No MODULE_VERSION in ng_ether so we can't MODULE_DEPEND it */
161 kern_kldload(curthread, "ng_ether", NULL);
162 break;
163
164 case MOD_UNLOAD:
165 rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
166 memset(&g_VBoxNetFltGlobals, 0, sizeof(VBOXNETFLTGLOBALS));
167 RTR0Term();
168 break;
169
170 case MOD_SHUTDOWN:
171 case MOD_QUIESCE:
172 default:
173 return EOPNOTSUPP;
174 }
175
176 if (RT_SUCCESS(rc))
177 return 0;
178 return RTErrConvertToErrno(rc);
179}
180
181/*
182 * Convert from mbufs to vbox scatter-gather data structure
183 */
184static void vboxNetFltFreeBSDMBufToSG(PVBOXNETFLTINS pThis, struct mbuf *m, PINTNETSG pSG,
185 unsigned int cSegs, unsigned int segOffset)
186{
187 static uint8_t const s_abZero[128] = {0};
188 unsigned int i;
189 struct mbuf *m0;
190
191 IntNetSgInitTempSegs(pSG, m_length(m, NULL), cSegs, 0 /*cSegsUsed*/);
192
193 for (m0 = m, i = segOffset; m0; m0 = m0->m_next)
194 {
195 if (m0->m_len == 0)
196 continue;
197
198 pSG->aSegs[i].cb = m0->m_len;
199 pSG->aSegs[i].pv = mtod(m0, uint8_t *);
200 pSG->aSegs[i].Phys = NIL_RTHCPHYS;
201 i++;
202 }
203
204#ifdef PADD_RUNT_FRAMES_FROM_HOST
205 if (pSG->cbTotal < 60)
206 {
207 pSG->aSegs[i].Phys = NIL_RTHCPHYS;
208 pSG->aSegs[i].pv = (void *)&s_abZero[0];
209 pSG->aSegs[i].cb = 60 - pSG->cbTotal;
210 pSG->cbTotal = 60;
211 i++;
212 }
213#endif
214
215 pSG->cSegsUsed = i;
216}
217
218/*
219 * Convert to mbufs from vbox scatter-gather data structure
220 */
221static struct mbuf * vboxNetFltFreeBSDSGMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
222{
223 struct mbuf *m;
224 int error;
225 unsigned int i;
226
227 if (pSG->cbTotal == 0)
228 return (NULL);
229
230 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
231 if (m == NULL)
232 return (NULL);
233
234 m->m_pkthdr.len = m->m_len = 0;
235 m->m_pkthdr.rcvif = NULL;
236
237 for (i = 0; i < pSG->cSegsUsed; i++)
238 {
239 error = m_append(m, pSG->aSegs[i].cb, pSG->aSegs[i].pv);
240 if (error == 0)
241 {
242 m_freem(m);
243 return (NULL);
244 }
245 }
246 return (m);
247}
248
249
250static int ng_vboxnetflt_constructor(node_p node)
251{
252 /* Nothing to do */
253 return (EINVAL);
254}
255
256/*
257 * Setup netgraph hooks
258 */
259static int ng_vboxnetflt_newhook(node_p node, hook_p hook, const char *name)
260{
261 PVBOXNETFLTINS pThis = NG_NODE_PRIVATE(node);
262
263 if (strcmp(name, NG_VBOXNETFLT_HOOK_IN) == 0)
264 {
265#if __FreeBSD_version >= 800000
266 NG_HOOK_SET_TO_INBOUND(hook);
267#endif
268 pThis->u.s.input = hook;
269 }
270 else if (strcmp(name, NG_VBOXNETFLT_HOOK_OUT) == 0)
271 {
272 pThis->u.s.output = hook;
273 }
274 else
275 return (EINVAL);
276
277 NG_HOOK_HI_STACK(hook);
278 return (0);
279}
280
281/**
282 * Netgraph message processing for node specific messages.
283 * We don't accept any special messages so this is not used.
284 */
285static int ng_vboxnetflt_rcvmsg(node_p node, item_p item, hook_p lasthook)
286{
287 PVBOXNETFLTINS pThis = NG_NODE_PRIVATE(node);
288 struct ng_mesg *msg;
289 int error = 0;
290
291 NGI_GET_MSG(item, msg);
292 if (msg->header.typecookie != NGM_VBOXNETFLT_COOKIE)
293 return (EINVAL);
294
295 switch (msg->header.cmd)
296 {
297 default:
298 error = EINVAL;
299 }
300 return (error);
301}
302
303/**
304 * Handle data on netgraph hooks.
305 * Frames processing is deferred to a taskqueue because this might
306 * be called with non-sleepable locks held and code paths inside
307 * the virtual switch might sleep.
308 */
309static int ng_vboxnetflt_rcvdata(hook_p hook, item_p item)
310{
311 const node_p node = NG_HOOK_NODE(hook);
312 PVBOXNETFLTINS pThis = NG_NODE_PRIVATE(node);
313 struct ifnet *ifp = pThis->u.s.ifp;
314 struct mbuf *m;
315 struct m_tag *mtag;
316 bool fActive;
317
318 fActive = vboxNetFltTryRetainBusyActive(pThis);
319
320 NGI_GET_M(item, m);
321 NG_FREE_ITEM(item);
322
323 /* Locate tag to see if processing should be skipped for this frame */
324 mtag = m_tag_locate(m, MTAG_VBOX, PACKET_TAG_VBOX, NULL);
325 if (mtag != NULL)
326 {
327 m_tag_unlink(m, mtag);
328 m_tag_free(mtag);
329 }
330
331 /*
332 * Handle incoming hook. This is connected to the
333 * input path of the interface, thus handling incoming frames.
334 */
335 if (pThis->u.s.input == hook)
336 {
337 if (mtag != NULL || !fActive)
338 {
339 ether_demux(ifp, m);
340 if (fActive)
341 vboxNetFltRelease(pThis, true /*fBusy*/);
342 return (0);
343 }
344 mtx_lock_spin(&pThis->u.s.inq.ifq_mtx);
345 _IF_ENQUEUE(&pThis->u.s.inq, m);
346 mtx_unlock_spin(&pThis->u.s.inq.ifq_mtx);
347 taskqueue_enqueue_fast(taskqueue_fast, &pThis->u.s.tskin);
348 }
349 /*
350 * Handle mbufs on the outgoing hook, frames going to the interface
351 */
352 else if (pThis->u.s.output == hook)
353 {
354 if (mtag != NULL || !fActive)
355 {
356 int rc = ether_output_frame(ifp, m);
357 if (fActive)
358 vboxNetFltRelease(pThis, true /*fBusy*/);
359 return rc;
360 }
361 mtx_lock_spin(&pThis->u.s.outq.ifq_mtx);
362 _IF_ENQUEUE(&pThis->u.s.outq, m);
363 mtx_unlock_spin(&pThis->u.s.outq.ifq_mtx);
364 taskqueue_enqueue_fast(taskqueue_fast, &pThis->u.s.tskout);
365 }
366 else
367 {
368 m_freem(m);
369 }
370
371 if (fActive)
372 vboxNetFltRelease(pThis, true /*fBusy*/);
373 return (0);
374}
375
376static int ng_vboxnetflt_shutdown(node_p node)
377{
378 PVBOXNETFLTINS pThis = NG_NODE_PRIVATE(node);
379 bool fActive;
380
381 /* Prevent node shutdown if we're active */
382 if (pThis->enmTrunkState == INTNETTRUNKIFSTATE_ACTIVE)
383 return (EBUSY);
384 NG_NODE_UNREF(node);
385 return (0);
386}
387
388static int ng_vboxnetflt_disconnect(hook_p hook)
389{
390 return (0);
391}
392
393/**
394 * Input processing task, handles incoming frames
395 */
396static void vboxNetFltFreeBSDinput(void *arg, int pending)
397{
398 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)arg;
399 struct mbuf *m, *m0;
400 struct ifnet *ifp = pThis->u.s.ifp;
401 unsigned int cSegs = 0;
402 bool fDropIt = false, fActive;
403 PINTNETSG pSG;
404
405 vboxNetFltRetain(pThis, true /* fBusy */);
406 for (;;)
407 {
408 mtx_lock_spin(&pThis->u.s.inq.ifq_mtx);
409 _IF_DEQUEUE(&pThis->u.s.inq, m);
410 mtx_unlock_spin(&pThis->u.s.inq.ifq_mtx);
411 if (m == NULL)
412 break;
413
414 for (m0 = m; m0 != NULL; m0 = m0->m_next)
415 if (m0->m_len > 0)
416 cSegs++;
417
418#ifdef PADD_RUNT_FRAMES_FROM_HOST
419 if (m_length(m, NULL) < 60)
420 cSegs++;
421#endif
422
423 /* Create a copy and deliver to the virtual switch */
424 pSG = RTMemTmpAlloc(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
425 vboxNetFltFreeBSDMBufToSG(pThis, m, pSG, cSegs, 0);
426 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_WIRE);
427 RTMemTmpFree(pSG);
428 if (fDropIt)
429 m_freem(m);
430 else
431 ether_demux(ifp, m);
432 }
433 vboxNetFltRelease(pThis, true /* fBusy */);
434}
435
436/**
437 * Output processing task, handles outgoing frames
438 */
439static void vboxNetFltFreeBSDoutput(void *arg, int pending)
440{
441 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)arg;
442 struct mbuf *m, *m0;
443 struct ifnet *ifp = pThis->u.s.ifp;
444 unsigned int cSegs = 0;
445 bool fDropIt = false, fActive;
446 PINTNETSG pSG;
447
448 vboxNetFltRetain(pThis, true /* fBusy */);
449 for (;;)
450 {
451 mtx_lock_spin(&pThis->u.s.outq.ifq_mtx);
452 _IF_DEQUEUE(&pThis->u.s.outq, m);
453 mtx_unlock_spin(&pThis->u.s.outq.ifq_mtx);
454 if (m == NULL)
455 break;
456
457 for (m0 = m; m0 != NULL; m0 = m0->m_next)
458 if (m0->m_len > 0)
459 cSegs++;
460
461#ifdef PADD_RUNT_FRAMES_FROM_HOST
462 if (m_length(m, NULL) < 60)
463 cSegs++;
464#endif
465 /* Create a copy and deliver to the virtual switch */
466 pSG = RTMemTmpAlloc(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
467 vboxNetFltFreeBSDMBufToSG(pThis, m, pSG, cSegs, 0);
468 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_HOST);
469 RTMemTmpFree(pSG);
470
471 if (fDropIt)
472 m_freem(m);
473 else
474 ether_output_frame(ifp, m);
475 }
476 vboxNetFltRelease(pThis, true /* fBusy */);
477}
478
479/**
480 * Called to deliver a frame to either the host, the wire or both.
481 */
482int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst)
483{
484 void (*input_f)(struct ifnet *, struct mbuf *);
485 struct ifnet *ifp;
486 struct mbuf *m;
487 struct m_tag *mtag;
488 bool fActive;
489 int error;
490
491 ifp = (void *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
492
493 if (fDst & INTNETTRUNKDIR_WIRE)
494 {
495 m = vboxNetFltFreeBSDSGMBufFromSG(pThis, pSG);
496 if (m == NULL)
497 return VERR_NO_MEMORY;
498 m = m_pullup(m, ETHER_HDR_LEN);
499 if (m == NULL)
500 return VERR_NO_MEMORY;
501
502 m->m_flags |= M_PKTHDR;
503 ether_output_frame(ifp, m);
504 }
505
506 if (fDst & INTNETTRUNKDIR_HOST)
507 {
508 m = vboxNetFltFreeBSDSGMBufFromSG(pThis, pSG);
509 if (m == NULL)
510 return VERR_NO_MEMORY;
511 m = m_pullup(m, ETHER_HDR_LEN);
512 if (m == NULL)
513 return VERR_NO_MEMORY;
514 /*
515 * Delivering packets to the host will be captured by the
516 * input hook. Tag the packet with a mbuf tag so that we
517 * can skip re-delivery of the packet to the guest during
518 * input hook processing.
519 */
520 mtag = m_tag_alloc(MTAG_VBOX, PACKET_TAG_VBOX, 0, M_NOWAIT);
521 if (mtag == NULL)
522 {
523 m_freem(m);
524 return VERR_NO_MEMORY;
525 }
526
527 m_tag_init(m);
528 m_tag_prepend(m, mtag);
529 m->m_flags |= M_PKTHDR;
530 m->m_pkthdr.rcvif = ifp;
531 ifp->if_input(ifp, m);
532 }
533 return VINF_SUCCESS;
534}
535
536static bool vboxNetFltFreeBsdIsPromiscuous(PVBOXNETFLTINS pThis)
537{
538 /** @todo This isn't taking into account that we put the interface in
539 * promiscuous mode. */
540 return (pThis->u.s.flags & IFF_PROMISC) ? true : false;
541}
542
543int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
544{
545 char nam[NG_NODESIZ];
546 struct ifnet *ifp;
547 node_p node;
548 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
549
550 NOREF(pvContext);
551 ifp = ifunit(pThis->szName);
552 if (ifp == NULL)
553 return VERR_INTNET_FLT_IF_NOT_FOUND;
554
555 /* Create a new netgraph node for this instance */
556 if (ng_make_node_common(&ng_vboxnetflt_typestruct, &node) != 0)
557 return VERR_INTERNAL_ERROR;
558
559 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
560
561 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.ifp, ifp);
562 pThis->u.s.node = node;
563 bcopy(IF_LLADDR(ifp), &pThis->u.s.MacAddr, ETHER_ADDR_LEN);
564 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
565
566 /* Initialize deferred input queue */
567 bzero(&pThis->u.s.inq, sizeof(struct ifqueue));
568 mtx_init(&pThis->u.s.inq.ifq_mtx, "vboxnetflt inq", NULL, MTX_SPIN);
569 TASK_INIT(&pThis->u.s.tskin, 0, vboxNetFltFreeBSDinput, pThis);
570
571 /* Initialize deferred output queue */
572 bzero(&pThis->u.s.outq, sizeof(struct ifqueue));
573 mtx_init(&pThis->u.s.outq.ifq_mtx, "vboxnetflt outq", NULL, MTX_SPIN);
574 TASK_INIT(&pThis->u.s.tskout, 0, vboxNetFltFreeBSDoutput, pThis);
575
576 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
577
578 NG_NODE_SET_PRIVATE(node, pThis);
579
580 /* Attempt to name it vboxnetflt_<ifname> */
581 snprintf(nam, NG_NODESIZ, "vboxnetflt_%s", pThis->szName);
582 ng_name_node(node, nam);
583
584 /* Report MAC address, promiscuous mode and GSO capabilities. */
585 /** @todo keep these reports up to date, either by polling for changes or
586 * intercept some control flow if possible. */
587 if (vboxNetFltTryRetainBusyNotDisconnected(pThis))
588 {
589 Assert(pThis->pSwitchPort);
590 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
591 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltFreeBsdIsPromiscuous(pThis));
592 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
593 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
594 vboxNetFltRelease(pThis, true /*fBusy*/);
595 }
596
597 return VINF_SUCCESS;
598}
599
600bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
601{
602 struct ifnet *ifp, *ifp0;
603
604 ifp = (struct ifnet *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
605 /*
606 * Attempt to check if the interface is still there and re-initialize if
607 * something has changed.
608 */
609 ifp0 = ifunit(pThis->szName);
610 if (ifp != ifp0)
611 {
612 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, true);
613 ng_rmnode_self(pThis->u.s.node);
614 pThis->u.s.node = NULL;
615 }
616
617 if (ifp0 != NULL)
618 {
619 vboxNetFltOsDeleteInstance(pThis);
620 vboxNetFltOsInitInstance(pThis, NULL);
621 }
622
623 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
624}
625
626void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
627{
628
629 taskqueue_drain(taskqueue_fast, &pThis->u.s.tskin);
630 taskqueue_drain(taskqueue_fast, &pThis->u.s.tskout);
631
632 mtx_destroy(&pThis->u.s.inq.ifq_mtx);
633 mtx_destroy(&pThis->u.s.outq.ifq_mtx);
634
635 if (pThis->u.s.node != NULL)
636 ng_rmnode_self(pThis->u.s.node);
637 pThis->u.s.node = NULL;
638}
639
640int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
641{
642
643 pThis->u.s.ifp = NULL;
644 pThis->u.s.flags = 0;
645 pThis->u.s.node = NULL;
646 return VINF_SUCCESS;
647}
648
649void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
650{
651 struct ifnet *ifp;
652 struct ifreq ifreq;
653 int error;
654 node_p node;
655 struct ng_mesg *msg;
656 struct ngm_connect *con;
657 struct ngm_rmhook *rm;
658 char path[NG_PATHSIZ];
659
660 Log(("%s: fActive:%d\n", __func__, fActive));
661
662 ifp = (struct ifnet *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
663 node = (node_p)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.node);
664
665 memset(&ifreq, 0, sizeof(struct ifreq));
666 /* Activate interface */
667 if (fActive)
668 {
669 pThis->u.s.flags = ifp->if_flags;
670 ifpromisc(ifp, 1);
671
672 /* ng_ether nodes are named after the interface name */
673 snprintf(path, sizeof(path), "%s:", ifp->if_xname);
674
675 /*
676 * Send a netgraph connect message to the ng_ether node
677 * assigned to the bridged interface. Connecting
678 * the hooks 'lower' (ng_ether) to out 'input'.
679 */
680 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_CONNECT,
681 sizeof(struct ngm_connect), M_NOWAIT);
682 if (msg == NULL)
683 return;
684 con = (struct ngm_connect *)msg->data;
685 snprintf(con->path, NG_PATHSIZ, "vboxnetflt_%s:", ifp->if_xname);
686 strlcpy(con->ourhook, "lower", NG_HOOKSIZ);
687 strlcpy(con->peerhook, "input", NG_HOOKSIZ);
688 NG_SEND_MSG_PATH(error, node, msg, path, 0);
689
690 /*
691 * Do the same for the hooks 'upper' (ng_ether) and our
692 * 'output' hook.
693 */
694 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_CONNECT,
695 sizeof(struct ngm_connect), M_NOWAIT);
696 if (msg == NULL)
697 return;
698 con = (struct ngm_connect *)msg->data;
699 snprintf(con->path, NG_PATHSIZ, "vboxnetflt_%s:",
700 ifp->if_xname);
701 strlcpy(con->ourhook, "upper", sizeof(con->ourhook));
702 strlcpy(con->peerhook, "output", sizeof(con->peerhook));
703 NG_SEND_MSG_PATH(error, node, msg, path, 0);
704 }
705 else
706 {
707 /* De-activate interface */
708 pThis->u.s.flags = 0;
709 ifpromisc(ifp, 0);
710
711 /* Disconnect msgs are addressed to ourself */
712 snprintf(path, sizeof(path), "vboxnetflt_%s:", ifp->if_xname);
713
714 /*
715 * Send a netgraph message to disconnect our 'input' hook
716 */
717 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_RMHOOK,
718 sizeof(struct ngm_rmhook), M_NOWAIT);
719 if (msg == NULL)
720 return;
721 rm = (struct ngm_rmhook *)msg->data;
722 strlcpy(rm->ourhook, "input", NG_HOOKSIZ);
723 NG_SEND_MSG_PATH(error, node, msg, path, 0);
724
725 /*
726 * Send a netgraph message to disconnect our 'output' hook
727 */
728 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_RMHOOK,
729 sizeof(struct ngm_rmhook), M_NOWAIT);
730 if (msg == NULL)
731 return;
732 rm = (struct ngm_rmhook *)msg->data;
733 strlcpy(rm->ourhook, "output", NG_HOOKSIZ);
734 NG_SEND_MSG_PATH(error, node, msg, path, 0);
735 }
736}
737
738int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
739{
740 return VINF_SUCCESS;
741}
742
743int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
744{
745 return VINF_SUCCESS;
746}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette