VirtualBox

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

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

IntNet/NetFlt: Added INTNETTRUNKSWPORT::pfnReportGsoCapabilities and enabled ring-0 transmission for linux hosts and wires.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 20.7 KB
 
1/* $Id: VBoxNetFlt-freebsd.c 28723 2010-04-25 21:50:56Z 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 = ASMAtomicUoReadBool(&pThis->fActive);
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 return (0);
341 }
342 mtx_lock_spin(&pThis->u.s.inq.ifq_mtx);
343 _IF_ENQUEUE(&pThis->u.s.inq, m);
344 mtx_unlock_spin(&pThis->u.s.inq.ifq_mtx);
345 taskqueue_enqueue_fast(taskqueue_fast, &pThis->u.s.tskin);
346 }
347 /**
348 * Handle mbufs on the outgoing hook, frames going to the interface
349 */
350 else if (pThis->u.s.output == hook)
351 {
352 if (mtag != NULL || !fActive)
353 return ether_output_frame(ifp, m);
354 mtx_lock_spin(&pThis->u.s.outq.ifq_mtx);
355 _IF_ENQUEUE(&pThis->u.s.outq, m);
356 mtx_unlock_spin(&pThis->u.s.outq.ifq_mtx);
357 taskqueue_enqueue_fast(taskqueue_fast, &pThis->u.s.tskout);
358 }
359 else
360 {
361 m_freem(m);
362 }
363 return (0);
364}
365
366static int ng_vboxnetflt_shutdown(node_p node)
367{
368 PVBOXNETFLTINS pThis = NG_NODE_PRIVATE(node);
369 bool fActive;
370
371 /* Prevent node shutdown if we're active */
372 fActive = ASMAtomicUoReadBool(&pThis->fActive);
373 if (fActive)
374 return (EBUSY);
375 NG_NODE_UNREF(node);
376 return (0);
377}
378
379static int ng_vboxnetflt_disconnect(hook_p hook)
380{
381 return (0);
382}
383
384/**
385 * Input processing task, handles incoming frames
386 */
387static void vboxNetFltFreeBSDinput(void *arg, int pending)
388{
389 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)arg;
390 struct mbuf *m, *m0;
391 struct ifnet *ifp = pThis->u.s.ifp;
392 unsigned int cSegs = 0;
393 bool fDropIt = false, fActive;
394 PINTNETSG pSG;
395
396 vboxNetFltRetain(pThis, true /* fBusy */);
397 for (;;)
398 {
399 mtx_lock_spin(&pThis->u.s.inq.ifq_mtx);
400 _IF_DEQUEUE(&pThis->u.s.inq, m);
401 mtx_unlock_spin(&pThis->u.s.inq.ifq_mtx);
402 if (m == NULL)
403 break;
404
405 for (m0 = m; m0 != NULL; m0 = m0->m_next)
406 if (m0->m_len > 0)
407 cSegs++;
408
409#ifdef PADD_RUNT_FRAMES_FROM_HOST
410 if (m_length(m, NULL) < 60)
411 cSegs++;
412#endif
413
414 /* Create a copy and deliver to the virtual switch */
415 pSG = RTMemTmpAlloc(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
416 vboxNetFltFreeBSDMBufToSG(pThis, m, pSG, cSegs, 0);
417 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_WIRE);
418 RTMemTmpFree(pSG);
419 if (fDropIt)
420 m_freem(m);
421 else
422 ether_demux(ifp, m);
423 }
424 vboxNetFltRelease(pThis, true /* fBusy */);
425}
426
427/**
428 * Output processing task, handles outgoing frames
429 */
430static void vboxNetFltFreeBSDoutput(void *arg, int pending)
431{
432 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)arg;
433 struct mbuf *m, *m0;
434 struct ifnet *ifp = pThis->u.s.ifp;
435 unsigned int cSegs = 0;
436 bool fDropIt = false, fActive;
437 PINTNETSG pSG;
438
439 vboxNetFltRetain(pThis, true /* fBusy */);
440 for (;;)
441 {
442 mtx_lock_spin(&pThis->u.s.outq.ifq_mtx);
443 _IF_DEQUEUE(&pThis->u.s.outq, m);
444 mtx_unlock_spin(&pThis->u.s.outq.ifq_mtx);
445 if (m == NULL)
446 break;
447
448 for (m0 = m; m0 != NULL; m0 = m0->m_next)
449 if (m0->m_len > 0)
450 cSegs++;
451
452#ifdef PADD_RUNT_FRAMES_FROM_HOST
453 if (m_length(m, NULL) < 60)
454 cSegs++;
455#endif
456 /* Create a copy and deliver to the virtual switch */
457 pSG = RTMemTmpAlloc(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
458 vboxNetFltFreeBSDMBufToSG(pThis, m, pSG, cSegs, 0);
459 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_HOST);
460 RTMemTmpFree(pSG);
461
462 if (fDropIt)
463 m_freem(m);
464 else
465 ether_output_frame(ifp, m);
466 }
467 vboxNetFltRelease(pThis, true /* fBusy */);
468}
469
470/**
471 * Called to deliver a frame to either the host, the wire or both.
472 */
473int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst)
474{
475 void (*input_f)(struct ifnet *, struct mbuf *);
476 struct ifnet *ifp;
477 struct mbuf *m;
478 struct m_tag *mtag;
479 bool fActive;
480 int error;
481
482 ifp = (void *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
483
484 if (fDst & INTNETTRUNKDIR_WIRE)
485 {
486 m = vboxNetFltFreeBSDSGMBufFromSG(pThis, pSG);
487 if (m == NULL)
488 return VERR_NO_MEMORY;
489 m = m_pullup(m, ETHER_HDR_LEN);
490 if (m == NULL)
491 return VERR_NO_MEMORY;
492
493 m->m_flags |= M_PKTHDR;
494 ether_output_frame(ifp, m);
495 }
496
497 if (fDst & INTNETTRUNKDIR_HOST)
498 {
499 m = vboxNetFltFreeBSDSGMBufFromSG(pThis, pSG);
500 if (m == NULL)
501 return VERR_NO_MEMORY;
502 m = m_pullup(m, ETHER_HDR_LEN);
503 if (m == NULL)
504 return VERR_NO_MEMORY;
505 /*
506 * Delivering packets to the host will be captured by the
507 * input hook. Tag the packet with a mbuf tag so that we
508 * can skip re-delivery of the packet to the guest during
509 * input hook processing.
510 */
511 mtag = m_tag_alloc(MTAG_VBOX, PACKET_TAG_VBOX, 0, M_NOWAIT);
512 if (mtag == NULL)
513 {
514 m_freem(m);
515 return VERR_NO_MEMORY;
516 }
517
518 m_tag_init(m);
519 m_tag_prepend(m, mtag);
520 m->m_flags |= M_PKTHDR;
521 m->m_pkthdr.rcvif = ifp;
522 ifp->if_input(ifp, m);
523 }
524 return VINF_SUCCESS;
525}
526
527static bool vboxNetFltFreeBsdIsPromiscuous(PVBOXNETFLTINS pThis)
528{
529 /** @todo This isn't taking into account that we put the interface in
530 * promiscuous mode. */
531 return (pThis->u.s.flags & IFF_PROMISC) ? true : false;
532}
533
534int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
535{
536 char nam[NG_NODESIZ];
537 struct ifnet *ifp;
538 node_p node;
539 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
540
541 NOREF(pvContext);
542 ifp = ifunit(pThis->szName);
543 if (ifp == NULL)
544 return VERR_INTNET_FLT_IF_NOT_FOUND;
545
546 /* Create a new netgraph node for this instance */
547 if (ng_make_node_common(&ng_vboxnetflt_typestruct, &node) != 0)
548 return VERR_INTERNAL_ERROR;
549
550 RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
551 ASMAtomicUoWritePtr((void * volatile *)&pThis->u.s.ifp, ifp);
552 pThis->u.s.node = node;
553 bcopy(IF_LLADDR(ifp), &pThis->u.s.MacAddr, ETHER_ADDR_LEN);
554 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
555
556 /* Initialize deferred input queue */
557 bzero(&pThis->u.s.inq, sizeof(struct ifqueue));
558 mtx_init(&pThis->u.s.inq.ifq_mtx, "vboxnetflt inq", NULL, MTX_SPIN);
559 TASK_INIT(&pThis->u.s.tskin, 0, vboxNetFltFreeBSDinput, pThis);
560
561 /* Initialize deferred output queue */
562 bzero(&pThis->u.s.outq, sizeof(struct ifqueue));
563 mtx_init(&pThis->u.s.outq.ifq_mtx, "vboxnetflt outq", NULL, MTX_SPIN);
564 TASK_INIT(&pThis->u.s.tskout, 0, vboxNetFltFreeBSDoutput, pThis);
565
566 RTSpinlockRelease(pThis->hSpinlock, &Tmp);
567
568 NG_NODE_SET_PRIVATE(node, pThis);
569
570 /* Attempt to name it vboxnetflt_<ifname> */
571 snprintf(nam, NG_NODESIZ, "vboxnetflt_%s", pThis->szName);
572 ng_name_node(node, nam);
573
574 /* Report MAC address, promiscuous mode and GSO capabilities. */
575 /** @todo keep these reports up to date, either by polling for changes or
576 * intercept some control flow if possible. */
577 Assert(pThis->pSwitchPort);
578 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
579 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltFreeBsdIsPromiscuous(pThis));
580 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
581 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
582
583 return VINF_SUCCESS;
584}
585
586bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
587{
588 struct ifnet *ifp, *ifp0;
589
590 ifp = (struct ifnet *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
591 /*
592 * Attempt to check if the interface is still there and re-initialize if
593 * something has changed.
594 */
595 ifp0 = ifunit(pThis->szName);
596 if (ifp != ifp0)
597 {
598 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, true);
599 ng_rmnode_self(pThis->u.s.node);
600 pThis->u.s.node = NULL;
601 }
602
603 if (ifp0 != NULL)
604 {
605 vboxNetFltOsDeleteInstance(pThis);
606 vboxNetFltOsInitInstance(pThis, NULL);
607 }
608
609 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
610}
611
612void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
613{
614
615 taskqueue_drain(taskqueue_fast, &pThis->u.s.tskin);
616 taskqueue_drain(taskqueue_fast, &pThis->u.s.tskout);
617
618 mtx_destroy(&pThis->u.s.inq.ifq_mtx);
619 mtx_destroy(&pThis->u.s.outq.ifq_mtx);
620
621 if (pThis->u.s.node != NULL)
622 ng_rmnode_self(pThis->u.s.node);
623 pThis->u.s.node = NULL;
624}
625
626int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
627{
628
629 pThis->u.s.ifp = NULL;
630 pThis->u.s.flags = 0;
631 pThis->u.s.node = NULL;
632 return VINF_SUCCESS;
633}
634
635void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
636{
637 struct ifnet *ifp;
638 struct ifreq ifreq;
639 int error;
640 node_p node;
641 struct ng_mesg *msg;
642 struct ngm_connect *con;
643 struct ngm_rmhook *rm;
644 char path[NG_PATHSIZ];
645
646 Log(("%s: fActive:%d\n", __func__, fActive));
647
648 ifp = (struct ifnet *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.ifp);
649 node = (node_p)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.node);
650
651 memset(&ifreq, 0, sizeof(struct ifreq));
652 /* Activate interface */
653 if (fActive)
654 {
655 pThis->u.s.flags = ifp->if_flags;
656 ifpromisc(ifp, 1);
657
658 /* ng_ether nodes are named after the interface name */
659 snprintf(path, sizeof(path), "%s:", ifp->if_xname);
660
661 /*
662 * Send a netgraph connect message to the ng_ether node
663 * assigned to the bridged interface. Connecting
664 * the hooks 'lower' (ng_ether) to out 'input'.
665 */
666 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_CONNECT,
667 sizeof(struct ngm_connect), M_NOWAIT);
668 if (msg == NULL)
669 return;
670 con = (struct ngm_connect *)msg->data;
671 snprintf(con->path, NG_PATHSIZ, "vboxnetflt_%s:", ifp->if_xname);
672 strlcpy(con->ourhook, "lower", NG_HOOKSIZ);
673 strlcpy(con->peerhook, "input", NG_HOOKSIZ);
674 NG_SEND_MSG_PATH(error, node, msg, path, 0);
675
676 /*
677 * Do the same for the hooks 'upper' (ng_ether) and our
678 * 'output' hook.
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:",
686 ifp->if_xname);
687 strlcpy(con->ourhook, "upper", sizeof(con->ourhook));
688 strlcpy(con->peerhook, "output", sizeof(con->peerhook));
689 NG_SEND_MSG_PATH(error, node, msg, path, 0);
690 }
691 else
692 {
693 /* De-activate interface */
694 pThis->u.s.flags = 0;
695 ifpromisc(ifp, 0);
696
697 /* Disconnect msgs are addressed to ourself */
698 snprintf(path, sizeof(path), "vboxnetflt_%s:", ifp->if_xname);
699
700 /*
701 * Send a netgraph message to disconnect our 'input' hook
702 */
703 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_RMHOOK,
704 sizeof(struct ngm_rmhook), M_NOWAIT);
705 if (msg == NULL)
706 return;
707 rm = (struct ngm_rmhook *)msg->data;
708 strlcpy(rm->ourhook, "input", NG_HOOKSIZ);
709 NG_SEND_MSG_PATH(error, node, msg, path, 0);
710
711 /*
712 * Send a netgraph message to disconnect our 'output' hook
713 */
714 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_RMHOOK,
715 sizeof(struct ngm_rmhook), M_NOWAIT);
716 if (msg == NULL)
717 return;
718 rm = (struct ngm_rmhook *)msg->data;
719 strlcpy(rm->ourhook, "output", NG_HOOKSIZ);
720 NG_SEND_MSG_PATH(error, node, msg, path, 0);
721 }
722}
723
724int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
725{
726 return VINF_SUCCESS;
727}
728
729int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
730{
731 return VINF_SUCCESS;
732}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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