VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetAdp/darwin/VBoxNetAdp-darwin.cpp@ 57907

Last change on this file since 57907 was 57907, checked in by vboxsync, 9 years ago

VBoxNetAdp/darwin: use bpfattach() without tap and send callbacks.
Default behavior is fine and we get bpf packet injection for free.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1/* $Id: VBoxNetAdp-darwin.cpp 57907 2015-09-26 03:35:03Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), Darwin Specific Code.
4 */
5
6/*
7 * Copyright (C) 2008-2015 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
23#include "../../../Runtime/r0drv/darwin/the-darwin-kernel.h"
24
25#include <VBox/log.h>
26#include <VBox/err.h>
27#include <VBox/version.h>
28#include <iprt/assert.h>
29#include <iprt/initterm.h>
30#include <iprt/semaphore.h>
31#include <iprt/spinlock.h>
32#include <iprt/string.h>
33#include <iprt/uuid.h>
34#include <iprt/alloca.h>
35
36#include "../../darwin/VBoxNetSend.h"
37
38#include <sys/systm.h>
39RT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
40#include <sys/kpi_mbuf.h>
41RT_C_DECLS_END
42
43#include <net/ethernet.h>
44#include <net/if_ether.h>
45#include <net/if_types.h>
46#include <sys/socket.h>
47#include <net/if.h>
48#include <net/if_dl.h>
49#include <sys/errno.h>
50#include <sys/param.h>
51#include <sys/conf.h>
52#include <miscfs/devfs/devfs.h>
53RT_C_DECLS_BEGIN
54#include <net/bpf.h>
55RT_C_DECLS_END
56
57#define VBOXNETADP_OS_SPECFIC 1
58#include "../VBoxNetAdpInternal.h"
59
60
61/*********************************************************************************************************************************
62* Defined Constants And Macros *
63*********************************************************************************************************************************/
64/** The maximum number of SG segments.
65 * Used to prevent stack overflow and similar bad stuff. */
66#define VBOXNETADP_DARWIN_MAX_SEGS 32
67#define VBOXNETADP_DARWIN_MAX_FAMILIES 4
68#define VBOXNETADP_DARWIN_NAME "vboxnet"
69#define VBOXNETADP_DARWIN_MTU 1500
70#define VBOXNETADP_DARWIN_DETACH_TIMEOUT 500
71
72#define VBOXNETADP_FROM_IFACE(iface) ((PVBOXNETADP) ifnet_softc(iface))
73
74
75/*********************************************************************************************************************************
76* Internal Functions *
77*********************************************************************************************************************************/
78RT_C_DECLS_BEGIN
79static kern_return_t VBoxNetAdpDarwinStart(struct kmod_info *pKModInfo, void *pvData);
80static kern_return_t VBoxNetAdpDarwinStop(struct kmod_info *pKModInfo, void *pvData);
81RT_C_DECLS_END
82
83static int VBoxNetAdpDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
84static int VBoxNetAdpDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
85static int VBoxNetAdpDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
86
87
88/*********************************************************************************************************************************
89* Global Variables *
90*********************************************************************************************************************************/
91/**
92 * Declare the module stuff.
93 */
94RT_C_DECLS_BEGIN
95extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
96extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
97
98KMOD_EXPLICIT_DECL(VBoxNetAdp, VBOX_VERSION_STRING, _start, _stop)
99DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxNetAdpDarwinStart;
100DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxNetAdpDarwinStop;
101DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
102RT_C_DECLS_END
103
104/**
105 * The (common) global data.
106 */
107static int g_nCtlDev = -1; /* Major dev number */
108static void *g_hCtlDev = 0; /* FS dev handle */
109
110/**
111 * The character device switch table for the driver.
112 */
113static struct cdevsw g_ChDev =
114{
115 /*.d_open = */VBoxNetAdpDarwinOpen,
116 /*.d_close = */VBoxNetAdpDarwinClose,
117 /*.d_read = */eno_rdwrt,
118 /*.d_write = */eno_rdwrt,
119 /*.d_ioctl = */VBoxNetAdpDarwinIOCtl,
120 /*.d_stop = */eno_stop,
121 /*.d_reset = */eno_reset,
122 /*.d_ttys = */NULL,
123 /*.d_select = */eno_select,
124 /*.d_mmap = */eno_mmap,
125 /*.d_strategy = */eno_strat,
126 /*.d_getc = */eno_getc,
127 /*.d_putc = */eno_putc,
128 /*.d_type = */0
129};
130
131
132
133static void vboxNetAdpDarwinComposeUUID(PVBOXNETADP pThis, PRTUUID pUuid)
134{
135 /* Generate UUID from name and MAC address. */
136 RTUuidClear(pUuid);
137 memcpy(pUuid->au8, "vboxnet", 7);
138 pUuid->Gen.u8ClockSeqHiAndReserved = (pUuid->Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
139 pUuid->Gen.u16TimeHiAndVersion = (pUuid->Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
140 pUuid->Gen.u8ClockSeqLow = pThis->iUnit;
141 vboxNetAdpComposeMACAddress(pThis, (PRTMAC)pUuid->Gen.au8Node);
142}
143
144static errno_t vboxNetAdpDarwinOutput(ifnet_t pIface, mbuf_t pMBuf)
145{
146 /*
147 * We are a dummy interface with all the real work done in
148 * VBoxNetFlt bridged networking filter. If anything makes it
149 * this far, just drop it, we have nowhere to send it to.
150 */
151 mbuf_freem_list(pMBuf);
152 return 0;
153}
154
155static void vboxNetAdpDarwinAttachFamily(PVBOXNETADP pThis, protocol_family_t Family)
156{
157 u_int32_t i;
158 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
159 if (pThis->u.s.aAttachedFamilies[i] == 0)
160 {
161 pThis->u.s.aAttachedFamilies[i] = Family;
162 break;
163 }
164}
165
166static void vboxNetAdpDarwinDetachFamily(PVBOXNETADP pThis, protocol_family_t Family)
167{
168 u_int32_t i;
169 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
170 if (pThis->u.s.aAttachedFamilies[i] == Family)
171 pThis->u.s.aAttachedFamilies[i] = 0;
172}
173
174static errno_t vboxNetAdpDarwinAddProto(ifnet_t pIface, protocol_family_t Family, const struct ifnet_demux_desc *pDemuxDesc, u_int32_t nDesc)
175{
176 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
177 Assert(pThis);
178 vboxNetAdpDarwinAttachFamily(pThis, Family);
179 LogFlow(("vboxNetAdpAddProto: Family=%d.\n", Family));
180 return ether_add_proto(pIface, Family, pDemuxDesc, nDesc);
181}
182
183static errno_t vboxNetAdpDarwinDelProto(ifnet_t pIface, protocol_family_t Family)
184{
185 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
186 Assert(pThis);
187 LogFlow(("vboxNetAdpDelProto: Family=%d.\n", Family));
188 vboxNetAdpDarwinDetachFamily(pThis, Family);
189 return ether_del_proto(pIface, Family);
190}
191
192static void vboxNetAdpDarwinDetach(ifnet_t pIface)
193{
194 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
195 Assert(pThis);
196 Log2(("vboxNetAdpDarwinDetach: Signaling detach to vboxNetAdpUnregisterDevice.\n"));
197 /* Let vboxNetAdpDarwinUnregisterDevice know that the interface has been detached. */
198 RTSemEventSignal(pThis->u.s.hEvtDetached);
199}
200
201static errno_t vboxNetAdpDarwinDemux(ifnet_t pIface, mbuf_t pMBuf,
202 char *pFrameHeader,
203 protocol_family_t *pProtocolFamily)
204{
205 /*
206 * Anything we get here comes from VBoxNetFlt bridged networking
207 * filter where it has already been accounted for and fed to bpf.
208 */
209 return ether_demux(pIface, pMBuf, pFrameHeader, pProtocolFamily);
210}
211
212int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMACAddress)
213{
214 int rc;
215 struct ifnet_init_params Params;
216 RTUUID uuid;
217 struct sockaddr_dl mac;
218
219 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
220 rc = RTSemEventCreate(&pThis->u.s.hEvtDetached);
221 if (RT_FAILURE(rc))
222 {
223 printf("vboxNetAdpOsCreate: failed to create semaphore (rc=%d).\n", rc);
224 return rc;
225 }
226
227 mac.sdl_len = sizeof(mac);
228 mac.sdl_family = AF_LINK;
229 mac.sdl_alen = ETHER_ADDR_LEN;
230 mac.sdl_nlen = 0;
231 mac.sdl_slen = 0;
232 memcpy(LLADDR(&mac), pMACAddress->au8, mac.sdl_alen);
233
234 RTStrPrintf(pThis->szName, VBOXNETADP_MAX_NAME_LEN, "%s%d", VBOXNETADP_NAME, pThis->iUnit);
235 vboxNetAdpDarwinComposeUUID(pThis, &uuid);
236 Params.uniqueid = uuid.au8;
237 Params.uniqueid_len = sizeof(uuid);
238 Params.name = VBOXNETADP_NAME;
239 Params.unit = pThis->iUnit;
240 Params.family = IFNET_FAMILY_ETHERNET;
241 Params.type = IFT_ETHER;
242 Params.output = vboxNetAdpDarwinOutput;
243 Params.demux = vboxNetAdpDarwinDemux;
244 Params.add_proto = vboxNetAdpDarwinAddProto;
245 Params.del_proto = vboxNetAdpDarwinDelProto;
246 Params.check_multi = ether_check_multi;
247 Params.framer = ether_frameout;
248 Params.softc = pThis;
249 Params.ioctl = (ifnet_ioctl_func)ether_ioctl;
250 Params.set_bpf_tap = NULL;
251 Params.detach = vboxNetAdpDarwinDetach;
252 Params.event = NULL;
253 Params.broadcast_addr = "\xFF\xFF\xFF\xFF\xFF\xFF";
254 Params.broadcast_len = ETHER_ADDR_LEN;
255
256 errno_t err = ifnet_allocate(&Params, &pThis->u.s.pIface);
257 if (!err)
258 {
259 err = ifnet_attach(pThis->u.s.pIface, &mac);
260 if (!err)
261 {
262 bpfattach(pThis->u.s.pIface, DLT_EN10MB, ETHER_HDR_LEN);
263
264 err = ifnet_set_flags(pThis->u.s.pIface, IFF_RUNNING | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST, 0xFFFF);
265 if (!err)
266 {
267 ifnet_set_mtu(pThis->u.s.pIface, VBOXNETADP_MTU);
268 VBoxNetSendDummy(pThis->u.s.pIface);
269 return VINF_SUCCESS;
270 }
271 else
272 Log(("vboxNetAdpDarwinRegisterDevice: Failed to set flags (err=%d).\n", err));
273 ifnet_detach(pThis->u.s.pIface);
274 }
275 else
276 Log(("vboxNetAdpDarwinRegisterDevice: Failed to attach to interface (err=%d).\n", err));
277 ifnet_release(pThis->u.s.pIface);
278 }
279 else
280 Log(("vboxNetAdpDarwinRegisterDevice: Failed to allocate interface (err=%d).\n", err));
281
282 RTSemEventDestroy(pThis->u.s.hEvtDetached);
283 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
284
285 return RTErrConvertFromErrno(err);
286}
287
288void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
289{
290 u_int32_t i;
291 /* Bring down the interface */
292 int rc = VINF_SUCCESS;
293 errno_t err;
294
295 AssertPtr(pThis->u.s.pIface);
296 Assert(pThis->u.s.hEvtDetached != NIL_RTSEMEVENT);
297
298 err = ifnet_set_flags(pThis->u.s.pIface, 0, IFF_UP | IFF_RUNNING);
299 if (err)
300 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to bring down interface "
301 "(err=%d).\n", err));
302 /* Detach all protocols. */
303 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
304 if (pThis->u.s.aAttachedFamilies[i])
305 ifnet_detach_protocol(pThis->u.s.pIface, pThis->u.s.aAttachedFamilies[i]);
306 err = ifnet_detach(pThis->u.s.pIface);
307 if (err)
308 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to detach interface "
309 "(err=%d).\n", err));
310 Log2(("vboxNetAdpDarwinUnregisterDevice: Waiting for 'detached' event...\n"));
311 /* Wait until we get a signal from detach callback. */
312 rc = RTSemEventWait(pThis->u.s.hEvtDetached, VBOXNETADP_DETACH_TIMEOUT);
313 if (rc == VERR_TIMEOUT)
314 LogRel(("VBoxAdpDrv: Failed to detach interface %s%d\n.",
315 VBOXNETADP_NAME, pThis->iUnit));
316 err = ifnet_release(pThis->u.s.pIface);
317 if (err)
318 Log(("vboxNetAdpUnregisterDevice: Failed to release interface (err=%d).\n", err));
319
320 RTSemEventDestroy(pThis->u.s.hEvtDetached);
321 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
322}
323
324/**
325 * Device open. Called on open /dev/vboxnetctl
326 *
327 * @param pInode Pointer to inode info structure.
328 * @param pFilp Associated file pointer.
329 */
330static int VBoxNetAdpDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
331{
332 char szName[128];
333 szName[0] = '\0';
334 proc_name(proc_pid(pProcess), szName, sizeof(szName));
335 Log(("VBoxNetAdpDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
336 return 0;
337}
338
339/**
340 * Close device.
341 */
342static int VBoxNetAdpDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
343{
344 Log(("VBoxNetAdpDarwinClose: pid=%d\n", proc_pid(pProcess)));
345 return 0;
346}
347
348/**
349 * Device I/O Control entry point.
350 *
351 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
352 * @param Dev The device number (major+minor).
353 * @param iCmd The IOCtl command.
354 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
355 * @param fFlags Flag saying we're a character device (like we didn't know already).
356 * @param pProcess The process issuing this request.
357 */
358static int VBoxNetAdpDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
359{
360 uint32_t cbReq = IOCPARM_LEN(iCmd);
361 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)pData;
362 int rc;
363
364 Log(("VBoxNetAdpDarwinIOCtl: param len %#x; iCmd=%#lx\n", cbReq, iCmd));
365 switch (IOCBASECMD(iCmd))
366 {
367 case IOCBASECMD(VBOXNETADP_CTL_ADD):
368 {
369 if ( (IOC_DIRMASK & iCmd) != IOC_INOUT
370 || cbReq < sizeof(VBOXNETADPREQ))
371 return EINVAL;
372
373 PVBOXNETADP pNew;
374 Log(("VBoxNetAdpDarwinIOCtl: szName=%s\n", pReq->szName));
375 rc = vboxNetAdpCreate(&pNew,
376 pReq->szName[0] && RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))) ?
377 pReq->szName : NULL);
378 if (RT_FAILURE(rc))
379 return rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL;
380
381 Assert(strlen(pReq->szName) < sizeof(pReq->szName));
382 strncpy(pReq->szName, pNew->szName, sizeof(pReq->szName) - 1);
383 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
384 Log(("VBoxNetAdpDarwinIOCtl: Added '%s'\n", pReq->szName));
385 break;
386 }
387
388 case IOCBASECMD(VBOXNETADP_CTL_REMOVE):
389 {
390 if (!RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))))
391 return EINVAL;
392
393 PVBOXNETADP pAdp = vboxNetAdpFindByName(pReq->szName);
394 if (!pAdp)
395 return EINVAL;
396
397 rc = vboxNetAdpDestroy(pAdp);
398 if (RT_FAILURE(rc))
399 return EINVAL;
400 Log(("VBoxNetAdpDarwinIOCtl: Removed %s\n", pReq->szName));
401 break;
402 }
403
404 default:
405 printf("VBoxNetAdpDarwinIOCtl: unknown command %lx.\n", IOCBASECMD(iCmd));
406 return EINVAL;
407 }
408
409 return 0;
410}
411
412int vboxNetAdpOsInit(PVBOXNETADP pThis)
413{
414 /*
415 * Init the darwin specific members.
416 */
417 pThis->u.s.pIface = NULL;
418 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
419 memset(pThis->u.s.aAttachedFamilies, 0, sizeof(pThis->u.s.aAttachedFamilies));
420
421 return VINF_SUCCESS;
422}
423
424/**
425 * Start the kernel module.
426 */
427static kern_return_t VBoxNetAdpDarwinStart(struct kmod_info *pKModInfo, void *pvData)
428{
429 int rc;
430
431 /*
432 * Initialize IPRT and find our module tag id.
433 * (IPRT is shared with VBoxDrv, it creates the loggers.)
434 */
435 rc = RTR0Init(0);
436 if (RT_SUCCESS(rc))
437 {
438 Log(("VBoxNetAdpDarwinStart\n"));
439 rc = vboxNetAdpInit();
440 if (RT_SUCCESS(rc))
441 {
442 g_nCtlDev = cdevsw_add(-1, &g_ChDev);
443 if (g_nCtlDev < 0)
444 {
445 LogRel(("VBoxAdp: failed to register control device."));
446 rc = VERR_CANT_CREATE;
447 }
448 else
449 {
450 g_hCtlDev = devfs_make_node(makedev(g_nCtlDev, 0), DEVFS_CHAR,
451 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
452 if (!g_hCtlDev)
453 {
454 LogRel(("VBoxAdp: failed to create FS node for control device."));
455 rc = VERR_CANT_CREATE;
456 }
457 }
458 }
459
460 if (RT_SUCCESS(rc))
461 {
462 LogRel(("VBoxAdpDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
463 return KMOD_RETURN_SUCCESS;
464 }
465
466 LogRel(("VBoxAdpDrv: failed to initialize device extension (rc=%d)\n", rc));
467 RTR0Term();
468 }
469 else
470 printf("VBoxAdpDrv: failed to initialize IPRT (rc=%d)\n", rc);
471
472 return KMOD_RETURN_FAILURE;
473}
474
475
476/**
477 * Stop the kernel module.
478 */
479static kern_return_t VBoxNetAdpDarwinStop(struct kmod_info *pKModInfo, void *pvData)
480{
481 Log(("VBoxNetAdpDarwinStop\n"));
482
483 vboxNetAdpShutdown();
484 /* Remove control device */
485 devfs_remove(g_hCtlDev);
486 cdevsw_remove(g_nCtlDev, &g_ChDev);
487
488 RTR0Term();
489
490 return KMOD_RETURN_SUCCESS;
491}
Note: See TracBrowser for help on using the repository browser.

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