VirtualBox

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

Last change on this file since 38371 was 38371, checked in by vboxsync, 14 years ago

Darwin burn fix

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