1 | /* $Id: VBoxNetSend.h 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * A place to share code and definitions between VBoxNetAdp and VBoxNetFlt host drivers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | /** @todo move this to src/VBox/HostDrivers/darwin as a .cpp file. */
|
---|
38 |
|
---|
39 | #ifndef VBOX_INCLUDED_SRC_darwin_VBoxNetSend_h
|
---|
40 | #define VBOX_INCLUDED_SRC_darwin_VBoxNetSend_h
|
---|
41 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
42 | # pragma once
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #if defined(RT_OS_DARWIN)
|
---|
46 |
|
---|
47 | # include <iprt/err.h>
|
---|
48 | # include <iprt/assert.h>
|
---|
49 | # include <iprt/string.h>
|
---|
50 |
|
---|
51 | # include <sys/socket.h>
|
---|
52 | # if MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 /* The 10.15 SDK has a slightly butchered API deprecation attempt. */
|
---|
53 | # pragma clang diagnostic push
|
---|
54 | # pragma clang diagnostic ignored "-Wmacro-redefined" /* Each header redefines __NKE_API_DEPRECATED. */
|
---|
55 | # pragma clang diagnostic ignored "-Wmissing-declarations" /* Misplaced __NKE_API_DEPRECATED; in kpi_mbuf.h. */
|
---|
56 | # include <net/kpi_interface.h>
|
---|
57 | # include <sys/kpi_mbuf.h>
|
---|
58 | # include <net/if.h>
|
---|
59 | # pragma clang diagnostic pop
|
---|
60 | # else /* < 10.15 */
|
---|
61 | # include <net/kpi_interface.h>
|
---|
62 | RT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
|
---|
63 | # include <sys/kpi_mbuf.h>
|
---|
64 | RT_C_DECLS_END
|
---|
65 | # include <net/if.h>
|
---|
66 | # endif /* < 10.15 */
|
---|
67 |
|
---|
68 |
|
---|
69 | RT_C_DECLS_BEGIN
|
---|
70 |
|
---|
71 | # if defined(IN_RING0)
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Constructs and submits a dummy packet to ifnet_input().
|
---|
75 | *
|
---|
76 | * This is a workaround for "stuck dock icon" issue. When the first packet goes
|
---|
77 | * through the interface DLIL grabs a reference to the thread that submits the
|
---|
78 | * packet and holds it until the interface is destroyed. By submitting this
|
---|
79 | * dummy we make DLIL grab the thread of a non-GUI process.
|
---|
80 | *
|
---|
81 | * Most of this function was copied from vboxNetFltDarwinMBufFromSG().
|
---|
82 | *
|
---|
83 | * @returns VBox status code.
|
---|
84 | * @param pIfNet The interface that will hold the reference to the calling
|
---|
85 | * thread. We submit dummy as if it was coming from this interface.
|
---|
86 | */
|
---|
87 | DECLINLINE(int) VBoxNetSendDummy(ifnet_t pIfNet)
|
---|
88 | {
|
---|
89 | int rc = VINF_SUCCESS;
|
---|
90 |
|
---|
91 | size_t cbTotal = 50; /* No Ethernet header */
|
---|
92 | mbuf_how_t How = MBUF_WAITOK;
|
---|
93 | mbuf_t pPkt = NULL;
|
---|
94 | errno_t err = mbuf_allocpacket(How, cbTotal, NULL, &pPkt);
|
---|
95 | if (!err)
|
---|
96 | {
|
---|
97 | /* Skip zero sized memory buffers (paranoia). */
|
---|
98 | mbuf_t pCur = pPkt;
|
---|
99 | while (pCur && !mbuf_maxlen(pCur))
|
---|
100 | pCur = mbuf_next(pCur);
|
---|
101 | Assert(pCur);
|
---|
102 |
|
---|
103 | /* Set the required packet header attributes. */
|
---|
104 | mbuf_pkthdr_setlen(pPkt, cbTotal);
|
---|
105 | mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
|
---|
106 |
|
---|
107 | mbuf_setlen(pCur, cbTotal);
|
---|
108 | memset(mbuf_data(pCur), 0, cbTotal);
|
---|
109 |
|
---|
110 | mbuf_pkthdr_setrcvif(pPkt, pIfNet); /* will crash without this. */
|
---|
111 |
|
---|
112 | err = ifnet_input(pIfNet, pPkt, NULL);
|
---|
113 | if (err)
|
---|
114 | {
|
---|
115 | rc = RTErrConvertFromErrno(err);
|
---|
116 | mbuf_freem(pPkt);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | else
|
---|
120 | rc = RTErrConvertFromErrno(err);
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | # endif /* IN_RING0 */
|
---|
126 |
|
---|
127 | RT_C_DECLS_END
|
---|
128 |
|
---|
129 | #endif /* RT_OS_DARWIN */
|
---|
130 |
|
---|
131 | #endif /* !VBOX_INCLUDED_SRC_darwin_VBoxNetSend_h */
|
---|
132 |
|
---|