VirtualBox

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

Last change on this file since 57358 was 57358, checked in by vboxsync, 10 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/* $Id: VBoxNetAdp-darwin.cpp 57358 2015-08-14 15:16:38Z 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>
53extern "C" {
54#include <net/bpf.h>
55}
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 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
147 Assert(pThis);
148 if (pThis->u.s.nTapMode & BPF_MODE_OUTPUT)
149 {
150 Log2(("vboxnetadp: out len=%d\n%.*Rhxd\n", mbuf_len(pMBuf), 14, mbuf_data(pMBuf)));
151 bpf_tap_out(pIface, DLT_EN10MB, pMBuf, NULL, 0);
152 }
153 mbuf_freem_list(pMBuf);
154 return 0;
155}
156
157static void vboxNetAdpDarwinAttachFamily(PVBOXNETADP pThis, protocol_family_t Family)
158{
159 u_int32_t i;
160 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
161 if (pThis->u.s.aAttachedFamilies[i] == 0)
162 {
163 pThis->u.s.aAttachedFamilies[i] = Family;
164 break;
165 }
166}
167
168static void vboxNetAdpDarwinDetachFamily(PVBOXNETADP pThis, protocol_family_t Family)
169{
170 u_int32_t i;
171 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
172 if (pThis->u.s.aAttachedFamilies[i] == Family)
173 pThis->u.s.aAttachedFamilies[i] = 0;
174}
175
176static errno_t vboxNetAdpDarwinAddProto(ifnet_t pIface, protocol_family_t Family, const struct ifnet_demux_desc *pDemuxDesc, u_int32_t nDesc)
177{
178 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
179 Assert(pThis);
180 vboxNetAdpDarwinAttachFamily(pThis, Family);
181 LogFlow(("vboxNetAdpAddProto: Family=%d.\n", Family));
182 return ether_add_proto(pIface, Family, pDemuxDesc, nDesc);
183}
184
185static errno_t vboxNetAdpDarwinDelProto(ifnet_t pIface, protocol_family_t Family)
186{
187 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
188 Assert(pThis);
189 LogFlow(("vboxNetAdpDelProto: Family=%d.\n", Family));
190 vboxNetAdpDarwinDetachFamily(pThis, Family);
191 return ether_del_proto(pIface, Family);
192}
193
194static void vboxNetAdpDarwinDetach(ifnet_t pIface)
195{
196 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
197 Assert(pThis);
198 Log2(("vboxNetAdpDarwinDetach: Signaling detach to vboxNetAdpUnregisterDevice.\n"));
199 /* Let vboxNetAdpDarwinUnregisterDevice know that the interface has been detached. */
200 RTSemEventSignal(pThis->u.s.hEvtDetached);
201}
202
203static errno_t vboxNetAdpDarwinDemux(ifnet_t pIface, mbuf_t pMBuf,
204 char *pFrameHeader,
205 protocol_family_t *pProtocolFamily)
206{
207 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
208 Assert(pThis);
209 Log2(("vboxNetAdpDarwinDemux: mode=%d\n", pThis->u.s.nTapMode));
210 if (pThis->u.s.nTapMode & BPF_MODE_INPUT)
211 {
212 Log2(("vboxnetadp: in len=%d\n%.*Rhxd\n", mbuf_len(pMBuf), 14, pFrameHeader));
213 bpf_tap_in(pIface, DLT_EN10MB, pMBuf, pFrameHeader, ETHER_HDR_LEN);
214 }
215 return ether_demux(pIface, pMBuf, pFrameHeader, pProtocolFamily);
216}
217
218static errno_t vboxNetAdpDarwinBpfTap(ifnet_t pIface, u_int32_t uLinkType, bpf_tap_mode nMode)
219{
220 PVBOXNETADP pThis = VBOXNETADP_FROM_IFACE(pIface);
221 Assert(pThis);
222 Log2(("vboxNetAdpDarwinBpfTap: mode=%d\n", nMode));
223 pThis->u.s.nTapMode = nMode;
224 return 0;
225}
226
227static errno_t vboxNetAdpDarwinBpfSend(ifnet_t pIface, u_int32_t uLinkType, mbuf_t pMBuf)
228{
229 LogRel(("vboxnetadp: BPF send function is not implemented (dlt=%d)\n", uLinkType));
230 mbuf_freem_list(pMBuf);
231 return 0;
232}
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 VBoxNetSendDummy(pThis->u.s.pIface);
298 return VINF_SUCCESS;
299 }
300 else
301 Log(("vboxNetAdpDarwinRegisterDevice: Failed to set flags (err=%d).\n", err));
302 ifnet_detach(pThis->u.s.pIface);
303 }
304 else
305 Log(("vboxNetAdpDarwinRegisterDevice: Failed to attach to interface (err=%d).\n", err));
306 ifnet_release(pThis->u.s.pIface);
307 }
308 else
309 Log(("vboxNetAdpDarwinRegisterDevice: Failed to allocate interface (err=%d).\n", err));
310
311 RTSemEventDestroy(pThis->u.s.hEvtDetached);
312 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
313
314 return RTErrConvertFromErrno(err);
315}
316
317void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
318{
319 u_int32_t i;
320 /* Bring down the interface */
321 int rc = VINF_SUCCESS;
322 errno_t err;
323
324 AssertPtr(pThis->u.s.pIface);
325 Assert(pThis->u.s.hEvtDetached != NIL_RTSEMEVENT);
326
327 err = ifnet_set_flags(pThis->u.s.pIface, 0, IFF_UP | IFF_RUNNING);
328 if (err)
329 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to bring down interface "
330 "(err=%d).\n", err));
331 /* Detach all protocols. */
332 for (i = 0; i < VBOXNETADP_MAX_FAMILIES; i++)
333 if (pThis->u.s.aAttachedFamilies[i])
334 ifnet_detach_protocol(pThis->u.s.pIface, pThis->u.s.aAttachedFamilies[i]);
335 err = ifnet_detach(pThis->u.s.pIface);
336 if (err)
337 Log(("vboxNetAdpDarwinUnregisterDevice: Failed to detach interface "
338 "(err=%d).\n", err));
339 Log2(("vboxNetAdpDarwinUnregisterDevice: Waiting for 'detached' event...\n"));
340 /* Wait until we get a signal from detach callback. */
341 rc = RTSemEventWait(pThis->u.s.hEvtDetached, VBOXNETADP_DETACH_TIMEOUT);
342 if (rc == VERR_TIMEOUT)
343 LogRel(("VBoxAdpDrv: Failed to detach interface %s%d\n.",
344 VBOXNETADP_NAME, pThis->iUnit));
345 err = ifnet_release(pThis->u.s.pIface);
346 if (err)
347 Log(("vboxNetAdpUnregisterDevice: Failed to release interface (err=%d).\n", err));
348
349 RTSemEventDestroy(pThis->u.s.hEvtDetached);
350 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
351}
352
353/**
354 * Device open. Called on open /dev/vboxnetctl
355 *
356 * @param pInode Pointer to inode info structure.
357 * @param pFilp Associated file pointer.
358 */
359static int VBoxNetAdpDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
360{
361 char szName[128];
362 szName[0] = '\0';
363 proc_name(proc_pid(pProcess), szName, sizeof(szName));
364 Log(("VBoxNetAdpDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
365 return 0;
366}
367
368/**
369 * Close device.
370 */
371static int VBoxNetAdpDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
372{
373 Log(("VBoxNetAdpDarwinClose: pid=%d\n", proc_pid(pProcess)));
374 return 0;
375}
376
377/**
378 * Device I/O Control entry point.
379 *
380 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
381 * @param Dev The device number (major+minor).
382 * @param iCmd The IOCtl command.
383 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
384 * @param fFlags Flag saying we're a character device (like we didn't know already).
385 * @param pProcess The process issuing this request.
386 */
387static int VBoxNetAdpDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
388{
389 uint32_t cbReq = IOCPARM_LEN(iCmd);
390 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)pData;
391 int rc;
392
393 Log(("VBoxNetAdpDarwinIOCtl: param len %#x; iCmd=%#lx\n", cbReq, iCmd));
394 switch (IOCBASECMD(iCmd))
395 {
396 case IOCBASECMD(VBOXNETADP_CTL_ADD):
397 {
398 if ( (IOC_DIRMASK & iCmd) != IOC_INOUT
399 || cbReq < sizeof(VBOXNETADPREQ))
400 return EINVAL;
401
402 PVBOXNETADP pNew;
403 Log(("VBoxNetAdpDarwinIOCtl: szName=%s\n", pReq->szName));
404 rc = vboxNetAdpCreate(&pNew,
405 pReq->szName[0] && RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))) ?
406 pReq->szName : NULL);
407 if (RT_FAILURE(rc))
408 return rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL;
409
410 Assert(strlen(pReq->szName) < sizeof(pReq->szName));
411 strncpy(pReq->szName, pNew->szName, sizeof(pReq->szName) - 1);
412 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
413 Log(("VBoxNetAdpDarwinIOCtl: Added '%s'\n", pReq->szName));
414 break;
415 }
416
417 case IOCBASECMD(VBOXNETADP_CTL_REMOVE):
418 {
419 if (!RTStrEnd(pReq->szName, RT_MIN(cbReq, sizeof(pReq->szName))))
420 return EINVAL;
421
422 PVBOXNETADP pAdp = vboxNetAdpFindByName(pReq->szName);
423 if (!pAdp)
424 return EINVAL;
425
426 rc = vboxNetAdpDestroy(pAdp);
427 if (RT_FAILURE(rc))
428 return EINVAL;
429 Log(("VBoxNetAdpDarwinIOCtl: Removed %s\n", pReq->szName));
430 break;
431 }
432
433 default:
434 printf("VBoxNetAdpDarwinIOCtl: unknown command %lx.\n", IOCBASECMD(iCmd));
435 return EINVAL;
436 }
437
438 return 0;
439}
440
441int vboxNetAdpOsInit(PVBOXNETADP pThis)
442{
443 /*
444 * Init the darwin specific members.
445 */
446 pThis->u.s.pIface = NULL;
447 pThis->u.s.hEvtDetached = NIL_RTSEMEVENT;
448 memset(pThis->u.s.aAttachedFamilies, 0, sizeof(pThis->u.s.aAttachedFamilies));
449
450 return VINF_SUCCESS;
451}
452
453/**
454 * Start the kernel module.
455 */
456static kern_return_t VBoxNetAdpDarwinStart(struct kmod_info *pKModInfo, void *pvData)
457{
458 int rc;
459
460 /*
461 * Initialize IPRT and find our module tag id.
462 * (IPRT is shared with VBoxDrv, it creates the loggers.)
463 */
464 rc = RTR0Init(0);
465 if (RT_SUCCESS(rc))
466 {
467 Log(("VBoxNetAdpDarwinStart\n"));
468 rc = vboxNetAdpInit();
469 if (RT_SUCCESS(rc))
470 {
471 g_nCtlDev = cdevsw_add(-1, &g_ChDev);
472 if (g_nCtlDev < 0)
473 {
474 LogRel(("VBoxAdp: failed to register control device."));
475 rc = VERR_CANT_CREATE;
476 }
477 else
478 {
479 g_hCtlDev = devfs_make_node(makedev(g_nCtlDev, 0), DEVFS_CHAR,
480 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
481 if (!g_hCtlDev)
482 {
483 LogRel(("VBoxAdp: failed to create FS node for control device."));
484 rc = VERR_CANT_CREATE;
485 }
486 }
487 }
488
489 if (RT_SUCCESS(rc))
490 {
491 LogRel(("VBoxAdpDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
492 return KMOD_RETURN_SUCCESS;
493 }
494
495 LogRel(("VBoxAdpDrv: failed to initialize device extension (rc=%d)\n", rc));
496 RTR0Term();
497 }
498 else
499 printf("VBoxAdpDrv: failed to initialize IPRT (rc=%d)\n", rc);
500
501 return KMOD_RETURN_FAILURE;
502}
503
504
505/**
506 * Stop the kernel module.
507 */
508static kern_return_t VBoxNetAdpDarwinStop(struct kmod_info *pKModInfo, void *pvData)
509{
510 Log(("VBoxNetAdpDarwinStop\n"));
511
512 vboxNetAdpShutdown();
513 /* Remove control device */
514 devfs_remove(g_hCtlDev);
515 cdevsw_remove(g_nCtlDev, &g_ChDev);
516
517 RTR0Term();
518
519 return KMOD_RETURN_SUCCESS;
520}
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