VirtualBox

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

Last change on this file since 47195 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: VBoxNetAdp-darwin.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), Darwin Specific Code.
4 */
5
6/*
7 * Copyright (C) 2008-2012 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* 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 return VINF_SUCCESS;
298 }
299 else
300 Log(("vboxNetAdpDarwinRegisterDevice: Failed to set flags (err=%d).\n", err));
301 ifnet_detach(pThis->u.s.pIface);
302 }
303 else
304 Log(("vboxNetAdpDarwinRegisterDevice: Failed to attach to interface (err=%d).\n", err));
305 ifnet_release(pThis->u.s.pIface);
306 }
307 else
308 Log(("vboxNetAdpDarwinRegisterDevice: Failed to allocate interface (err=%d).\n", err));
309
310 RTSemEventDestroy(pThis->u.s.hEvtDetached);
311 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
312
313 return RTErrConvertFromErrno(err);
314}
315
316void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
317{
318 u_int32_t i;
319 /* Bring down the interface */
320 int rc = VINF_SUCCESS;
321 errno_t err;
322
323 AssertPtr(pThis->u.s.pIface);
324 Assert(pThis->u.s.hEvtDetached != NIL_RTSEMEVENT);
325
326 err = ifnet_set_flags(pThis->u.s.pIface, 0, IFF_UP | IFF_RUNNING);
327 if (err)
328 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to bring down interface "
329 "(err=%d).\n", err));
330 /* Detach all protocols. */
331 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
332 if (pThis->u.s.aAttachedFamilies[i])
333 ifnet_detach_protocol(pThis->u.s.pIface, pThis->u.s.aAttachedFamilies[i]);
334 err = ifnet_detach(pThis->u.s.pIface);
335 if (err)
336 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to detach interface "
337 "(err=%d).\n", err));
338 Log2(("vboxNetAdpDarwinUnregisterDevice: Waiting for 'detached' event...\n"));
339 /* Wait until we get a signal from detach callback. */
340 rc = RTSemEventWait(pThis->u.s.hEvtDetached, VBOXNETADP_DETACH_TIMEOUT);
341 if (rc == VERR_TIMEOUT)
342 LogRel(("VBoxAdpDrv: Failed to detach interface %s%d\n.",
343 VBOXNETADP_NAME, pThis->iUnit));
344 err = ifnet_release(pThis->u.s.pIface);
345 if (err)
346 Log(("vboxNetAdpUnregisterDevice: Failed to release interface (err=%d).\n", err));
347
348 RTSemEventDestroy(pThis->u.s.hEvtDetached);
349 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
350}
351
352/**
353 * Device open. Called on open /dev/vboxnetctl
354 *
355 * @param pInode Pointer to inode info structure.
356 * @param pFilp Associated file pointer.
357 */
358static int VBoxNetAdpDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
359{
360 char szName[128];
361 szName[0] = '\0';
362 proc_name(proc_pid(pProcess), szName, sizeof(szName));
363 Log(("VBoxNetAdpDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
364 return 0;
365}
366
367/**
368 * Close device.
369 */
370static int VBoxNetAdpDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
371{
372 Log(("VBoxNetAdpDarwinClose: pid=%d\n", proc_pid(pProcess)));
373 return 0;
374}
375
376/**
377 * Device I/O Control entry point.
378 *
379 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
380 * @param Dev The device number (major+minor).
381 * @param iCmd The IOCtl command.
382 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
383 * @param fFlags Flag saying we're a character device (like we didn't know already).
384 * @param pProcess The process issuing this request.
385 */
386static int VBoxNetAdpDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
387{
388 uint32_t cbReq = IOCPARM_LEN(iCmd);
389 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)pData;
390 int rc;
391
392 Log(("VBoxNetAdpDarwinIOCtl: param len %#x; iCmd=%#lx\n", cbReq, iCmd));
393 switch (IOCBASECMD(iCmd))
394 {
395 case IOCBASECMD(VBOXNETADP_CTL_ADD):
396 {
397 if ( (IOC_DIRMASK & iCmd) != IOC_INOUT
398 || cbReq < sizeof(VBOXNETADPREQ))
399 return EINVAL;
400
401 PVBOXNETADP pNew;
402 Log(("VBoxNetAdpDarwinIOCtl: szName=%s\n", pReq->szName));
403 rc = vboxNetAdpCreate(&pNew,
404 pReq->szName[0] && RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))) ?
405 pReq->szName : NULL);
406 if (RT_FAILURE(rc))
407 return rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL;
408
409 Assert(strlen(pReq->szName) < sizeof(pReq->szName));
410 strncpy(pReq->szName, pNew->szName, sizeof(pReq->szName) - 1);
411 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
412 Log(("VBoxNetAdpDarwinIOCtl: Added '%s'\n", pReq->szName));
413 break;
414 }
415
416 case IOCBASECMD(VBOXNETADP_CTL_REMOVE):
417 {
418 if (!RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))))
419 return EINVAL;
420
421 PVBOXNETADP pAdp = vboxNetAdpFindByName(pReq->szName);
422 if (!pAdp)
423 return EINVAL;
424
425 rc = vboxNetAdpDestroy(pAdp);
426 if (RT_FAILURE(rc))
427 return EINVAL;
428 Log(("VBoxNetAdpDarwinIOCtl: Removed %s\n", pReq->szName));
429 break;
430 }
431
432 default:
433 printf("VBoxNetAdpDarwinIOCtl: unknown command %lx.\n", IOCBASECMD(iCmd));
434 return EINVAL;
435 }
436
437 return 0;
438}
439
440int vboxNetAdpOsInit(PVBOXNETADP pThis)
441{
442 /*
443 * Init the darwin specific members.
444 */
445 pThis->u.s.pIface = NULL;
446 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
447 memset(pThis->u.s.aAttachedFamilies, 0, sizeof(pThis->u.s.aAttachedFamilies));
448
449 return VINF_SUCCESS;
450}
451
452/**
453 * Start the kernel module.
454 */
455static kern_return_t VBoxNetAdpDarwinStart(struct kmod_info *pKModInfo, void *pvData)
456{
457 int rc;
458
459 /*
460 * Initialize IPRT and find our module tag id.
461 * (IPRT is shared with VBoxDrv, it creates the loggers.)
462 */
463 rc = RTR0Init(0);
464 if (RT_SUCCESS(rc))
465 {
466 Log(("VBoxNetAdpDarwinStart\n"));
467 rc = vboxNetAdpInit();
468 if (RT_SUCCESS(rc))
469 {
470 g_nCtlDev = cdevsw_add(-1, &g_ChDev);
471 if (g_nCtlDev < 0)
472 {
473 LogRel(("VBoxAdp: failed to register control device."));
474 rc = VERR_CANT_CREATE;
475 }
476 else
477 {
478 g_hCtlDev = devfs_make_node(makedev(g_nCtlDev, 0), DEVFS_CHAR,
479 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
480 if (!g_hCtlDev)
481 {
482 LogRel(("VBoxAdp: failed to create FS node for control device."));
483 rc = VERR_CANT_CREATE;
484 }
485 }
486 }
487
488 if (RT_SUCCESS(rc))
489 {
490 LogRel(("VBoxAdpDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
491 return KMOD_RETURN_SUCCESS;
492 }
493
494 LogRel(("VBoxAdpDrv: failed to initialize device extension (rc=%d)\n", rc));
495 RTR0Term();
496 }
497 else
498 printf("VBoxAdpDrv: failed to initialize IPRT (rc=%d)\n", rc);
499
500 return KMOD_RETURN_FAILURE;
501}
502
503
504/**
505 * Stop the kernel module.
506 */
507static kern_return_t VBoxNetAdpDarwinStop(struct kmod_info *pKModInfo, void *pvData)
508{
509 Log(("VBoxNetAdpDarwinStop\n"));
510
511 vboxNetAdpShutdown();
512 /* Remove control device */
513 devfs_remove(g_hCtlDev);
514 cdevsw_remove(g_nCtlDev, &g_ChDev);
515
516 RTR0Term();
517
518 return KMOD_RETURN_SUCCESS;
519}
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