VirtualBox

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

Last change on this file since 50322 was 50322, checked in by vboxsync, 11 years ago

VboxNetSendDummy() -> VBoxNetSendDummy().

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