1 | /* $Id: VBoxNetFlt-linux.c 85588 2020-07-31 17:36:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetFlt - Network Filter Driver (Host), Linux Specific Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP LOG_GROUP_NET_FLT_DRV
|
---|
32 | #define VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
33 | #include "the-linux-kernel.h"
|
---|
34 | #include "version-generated.h"
|
---|
35 | #include "revision-generated.h"
|
---|
36 | #include "product-generated.h"
|
---|
37 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
|
---|
38 | #include <linux/nsproxy.h>
|
---|
39 | #endif
|
---|
40 | #include <linux/netdevice.h>
|
---|
41 | #include <linux/etherdevice.h>
|
---|
42 | #include <linux/rtnetlink.h>
|
---|
43 | #include <linux/miscdevice.h>
|
---|
44 | #include <linux/inetdevice.h>
|
---|
45 | #include <linux/in.h>
|
---|
46 | #include <linux/ip.h>
|
---|
47 | #include <linux/if_vlan.h>
|
---|
48 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
|
---|
49 | #include <uapi/linux/pkt_cls.h>
|
---|
50 | #endif
|
---|
51 | #include <net/ipv6.h>
|
---|
52 | #include <net/if_inet6.h>
|
---|
53 | #include <net/addrconf.h>
|
---|
54 |
|
---|
55 | #include <VBox/log.h>
|
---|
56 | #include <VBox/err.h>
|
---|
57 | #include <VBox/intnetinline.h>
|
---|
58 | #include <VBox/vmm/pdmnetinline.h>
|
---|
59 | #include <VBox/param.h>
|
---|
60 | #include <iprt/alloca.h>
|
---|
61 | #include <iprt/assert.h>
|
---|
62 | #include <iprt/spinlock.h>
|
---|
63 | #include <iprt/semaphore.h>
|
---|
64 | #include <iprt/initterm.h>
|
---|
65 | #include <iprt/process.h>
|
---|
66 | #include <iprt/mem.h>
|
---|
67 | #include <iprt/net.h>
|
---|
68 | #include <iprt/log.h>
|
---|
69 | #include <iprt/mp.h>
|
---|
70 | #include <iprt/mem.h>
|
---|
71 | #include <iprt/time.h>
|
---|
72 |
|
---|
73 | #define VBOXNETFLT_OS_SPECFIC 1
|
---|
74 | #include "../VBoxNetFltInternal.h"
|
---|
75 |
|
---|
76 | typedef struct VBOXNETFLTNOTIFIER {
|
---|
77 | struct notifier_block Notifier;
|
---|
78 | PVBOXNETFLTINS pThis;
|
---|
79 | } VBOXNETFLTNOTIFIER;
|
---|
80 | typedef struct VBOXNETFLTNOTIFIER *PVBOXNETFLTNOTIFIER;
|
---|
81 |
|
---|
82 |
|
---|
83 | /*********************************************************************************************************************************
|
---|
84 | * Defined Constants And Macros *
|
---|
85 | *********************************************************************************************************************************/
|
---|
86 | #define VBOX_FLT_NB_TO_INST(pNB) RT_FROM_MEMBER(pNB, VBOXNETFLTINS, u.s.Notifier)
|
---|
87 | #define VBOX_FLT_PT_TO_INST(pPT) RT_FROM_MEMBER(pPT, VBOXNETFLTINS, u.s.PacketType)
|
---|
88 | #ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
89 | # define VBOX_FLT_XT_TO_INST(pXT) RT_FROM_MEMBER(pXT, VBOXNETFLTINS, u.s.XmitTask)
|
---|
90 | #endif
|
---|
91 |
|
---|
92 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
93 | # define VBOX_NETDEV_NOTIFIER_INFO_TO_DEV(ptr) netdev_notifier_info_to_dev(ptr)
|
---|
94 | #else
|
---|
95 | # define VBOX_NETDEV_NOTIFIER_INFO_TO_DEV(ptr) ((struct net_device *)ptr)
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
99 | # define VBOX_SKB_KMAP_FRAG(frag) kmap_atomic(skb_frag_page(frag))
|
---|
100 | # define VBOX_SKB_KUNMAP_FRAG(vaddr) kunmap_atomic(vaddr)
|
---|
101 | #else
|
---|
102 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
---|
103 | # define VBOX_SKB_KMAP_FRAG(frag) kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
|
---|
104 | # define VBOX_SKB_KUNMAP_FRAG(vaddr) kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ)
|
---|
105 | # else
|
---|
106 | # define VBOX_SKB_KMAP_FRAG(frag) kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
|
---|
107 | # define VBOX_SKB_KUNMAP_FRAG(vaddr) kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ)
|
---|
108 | # endif
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
|
---|
112 | # define VBOX_NETDEV_NAME(dev) netdev_name(dev)
|
---|
113 | #else
|
---|
114 | # define VBOX_NETDEV_NAME(dev) ((dev)->reg_state != NETREG_REGISTERED ? "(unregistered net_device)" : (dev)->name)
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
|
---|
118 | # define VBOX_IPV4_IS_LOOPBACK(addr) ipv4_is_loopback(addr)
|
---|
119 | # define VBOX_IPV4_IS_LINKLOCAL_169(addr) ipv4_is_linklocal_169(addr)
|
---|
120 | #else
|
---|
121 | # define VBOX_IPV4_IS_LOOPBACK(addr) ((addr & htonl(IN_CLASSA_NET)) == htonl(0x7f000000))
|
---|
122 | # define VBOX_IPV4_IS_LINKLOCAL_169(addr) ((addr & htonl(IN_CLASSB_NET)) == htonl(0xa9fe0000))
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
126 | # define VBOX_SKB_RESET_NETWORK_HDR(skb) skb_reset_network_header(skb)
|
---|
127 | # define VBOX_SKB_RESET_MAC_HDR(skb) skb_reset_mac_header(skb)
|
---|
128 | # define VBOX_SKB_CSUM_OFFSET(skb) skb->csum_offset
|
---|
129 | #else
|
---|
130 | # define VBOX_SKB_RESET_NETWORK_HDR(skb) skb->nh.raw = skb->data
|
---|
131 | # define VBOX_SKB_RESET_MAC_HDR(skb) skb->mac.raw = skb->data
|
---|
132 | # define VBOX_SKB_CSUM_OFFSET(skb) skb->csum
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
|
---|
136 | # define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb)
|
---|
137 | #else
|
---|
138 | # define CHECKSUM_PARTIAL CHECKSUM_HW
|
---|
139 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
|
---|
140 | # define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(skb, 0)
|
---|
141 | # else
|
---|
142 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 7)
|
---|
143 | # define VBOX_SKB_CHECKSUM_HELP(skb) skb_checksum_help(&skb, 0)
|
---|
144 | # else
|
---|
145 | # define VBOX_SKB_CHECKSUM_HELP(skb) (!skb_checksum_help(skb))
|
---|
146 | # endif
|
---|
147 | /* Versions prior 2.6.10 use stats for both bstats and qstats */
|
---|
148 | # define bstats stats
|
---|
149 | # define qstats stats
|
---|
150 | # endif
|
---|
151 | #endif
|
---|
152 |
|
---|
153 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 20, 0)
|
---|
154 | # define VBOX_HAVE_SKB_VLAN
|
---|
155 | #else
|
---|
156 | # ifdef RHEL_RELEASE_CODE
|
---|
157 | # if (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(7, 2) && RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(8, 0)) || \
|
---|
158 | (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6, 8) && RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(7, 0))
|
---|
159 | # define VBOX_HAVE_SKB_VLAN
|
---|
160 | # endif
|
---|
161 | # endif
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | #ifdef VBOX_HAVE_SKB_VLAN
|
---|
165 | # define vlan_tx_tag_get(skb) skb_vlan_tag_get(skb)
|
---|
166 | # define vlan_tx_tag_present(skb) skb_vlan_tag_present(skb)
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | #ifndef NET_IP_ALIGN
|
---|
170 | # define NET_IP_ALIGN 2
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | #if 1
|
---|
174 | /** Create scatter / gather segments for fragments. When not used, we will
|
---|
175 | * linearize the socket buffer before creating the internal networking SG. */
|
---|
176 | # define VBOXNETFLT_SG_SUPPORT 1
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
|
---|
180 |
|
---|
181 | /** Indicates that the linux kernel may send us GSO frames. */
|
---|
182 | # define VBOXNETFLT_WITH_GSO 1
|
---|
183 |
|
---|
184 | /** This enables or disables the transmitting of GSO frame from the internal
|
---|
185 | * network and to the host. */
|
---|
186 | # define VBOXNETFLT_WITH_GSO_XMIT_HOST 1
|
---|
187 |
|
---|
188 | # if 0 /** @todo This is currently disable because it causes performance loss of 5-10%. */
|
---|
189 | /** This enables or disables the transmitting of GSO frame from the internal
|
---|
190 | * network and to the wire. */
|
---|
191 | # define VBOXNETFLT_WITH_GSO_XMIT_WIRE 1
|
---|
192 | # endif
|
---|
193 |
|
---|
194 | /** This enables or disables the forwarding/flooding of GSO frame from the host
|
---|
195 | * to the internal network. */
|
---|
196 | # define VBOXNETFLT_WITH_GSO_RECV 1
|
---|
197 |
|
---|
198 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) */
|
---|
199 |
|
---|
200 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
201 | /** This enables or disables handling of GSO frames coming from the wire (GRO). */
|
---|
202 | # define VBOXNETFLT_WITH_GRO 1
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * GRO support was backported to RHEL 5.4
|
---|
207 | */
|
---|
208 | #ifdef RHEL_RELEASE_CODE
|
---|
209 | # if RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5, 4)
|
---|
210 | # define VBOXNETFLT_WITH_GRO 1
|
---|
211 | # endif
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | #if defined(CONFIG_SUSE_VERSION)
|
---|
215 | # if CONFIG_SUSE_VERSION == 15 && CONFIG_SUSE_PATCHLEVEL == 2
|
---|
216 | # define OPENSUSE_152
|
---|
217 | # endif
|
---|
218 | #endif
|
---|
219 |
|
---|
220 |
|
---|
221 | /*********************************************************************************************************************************
|
---|
222 | * Internal Functions *
|
---|
223 | *********************************************************************************************************************************/
|
---|
224 | static int __init VBoxNetFltLinuxInit(void);
|
---|
225 | static void __exit VBoxNetFltLinuxUnload(void);
|
---|
226 | static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf);
|
---|
227 |
|
---|
228 |
|
---|
229 | /*********************************************************************************************************************************
|
---|
230 | * Global Variables *
|
---|
231 | *********************************************************************************************************************************/
|
---|
232 | /**
|
---|
233 | * The (common) global data.
|
---|
234 | */
|
---|
235 | static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
|
---|
236 |
|
---|
237 | module_init(VBoxNetFltLinuxInit);
|
---|
238 | module_exit(VBoxNetFltLinuxUnload);
|
---|
239 |
|
---|
240 | MODULE_AUTHOR(VBOX_VENDOR);
|
---|
241 | MODULE_DESCRIPTION(VBOX_PRODUCT " Network Filter Driver");
|
---|
242 | MODULE_LICENSE("GPL");
|
---|
243 | #ifdef MODULE_VERSION
|
---|
244 | MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV) " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
|
---|
245 | #endif
|
---|
246 |
|
---|
247 |
|
---|
248 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) && defined(LOG_ENABLED)
|
---|
249 | unsigned dev_get_flags(const struct net_device *dev)
|
---|
250 | {
|
---|
251 | unsigned flags;
|
---|
252 |
|
---|
253 | flags = (dev->flags & ~(IFF_PROMISC |
|
---|
254 | IFF_ALLMULTI |
|
---|
255 | IFF_RUNNING)) |
|
---|
256 | (dev->gflags & (IFF_PROMISC |
|
---|
257 | IFF_ALLMULTI));
|
---|
258 |
|
---|
259 | if (netif_running(dev) && netif_carrier_ok(dev))
|
---|
260 | flags |= IFF_RUNNING;
|
---|
261 |
|
---|
262 | return flags;
|
---|
263 | }
|
---|
264 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) */
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Initialize module.
|
---|
269 | *
|
---|
270 | * @returns appropriate status code.
|
---|
271 | */
|
---|
272 | static int __init VBoxNetFltLinuxInit(void)
|
---|
273 | {
|
---|
274 | int rc;
|
---|
275 | /*
|
---|
276 | * Initialize IPRT.
|
---|
277 | */
|
---|
278 | rc = RTR0Init(0);
|
---|
279 | if (RT_SUCCESS(rc))
|
---|
280 | {
|
---|
281 | Log(("VBoxNetFltLinuxInit\n"));
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Initialize the globals and connect to the support driver.
|
---|
285 | *
|
---|
286 | * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
|
---|
287 | * for establishing the connect to the support driver.
|
---|
288 | */
|
---|
289 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
290 | rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
|
---|
291 | if (RT_SUCCESS(rc))
|
---|
292 | {
|
---|
293 | LogRel(("VBoxNetFlt: Successfully started.\n"));
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 |
|
---|
297 | LogRel(("VBoxNetFlt: failed to initialize device extension (rc=%d)\n", rc));
|
---|
298 | RTR0Term();
|
---|
299 | }
|
---|
300 | else
|
---|
301 | LogRel(("VBoxNetFlt: failed to initialize IPRT (rc=%d)\n", rc));
|
---|
302 |
|
---|
303 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
304 | return -RTErrConvertToErrno(rc);
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Unload the module.
|
---|
310 | *
|
---|
311 | * @todo We have to prevent this if we're busy!
|
---|
312 | */
|
---|
313 | static void __exit VBoxNetFltLinuxUnload(void)
|
---|
314 | {
|
---|
315 | int rc;
|
---|
316 | Log(("VBoxNetFltLinuxUnload\n"));
|
---|
317 | Assert(vboxNetFltCanUnload(&g_VBoxNetFltGlobals));
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * Undo the work done during start (in reverse order).
|
---|
321 | */
|
---|
322 | rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
|
---|
323 | AssertRC(rc); NOREF(rc);
|
---|
324 |
|
---|
325 | RTR0Term();
|
---|
326 |
|
---|
327 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
328 |
|
---|
329 | Log(("VBoxNetFltLinuxUnload - done\n"));
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * We filter traffic from the host to the internal network
|
---|
335 | * before it reaches the NIC driver.
|
---|
336 | *
|
---|
337 | * The current code uses a very ugly hack overriding hard_start_xmit
|
---|
338 | * callback in the device structure, but it has been shown to give us a
|
---|
339 | * performance boost of 60-100% though. Eventually we have to find some
|
---|
340 | * less hacky way of getting this job done.
|
---|
341 | */
|
---|
342 | #define VBOXNETFLT_WITH_HOST2WIRE_FILTER
|
---|
343 |
|
---|
344 | #ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
|
---|
345 |
|
---|
346 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
|
---|
347 |
|
---|
348 | # include <linux/ethtool.h>
|
---|
349 |
|
---|
350 | typedef struct ethtool_ops OVR_OPSTYPE;
|
---|
351 | # define OVR_OPS ethtool_ops
|
---|
352 | # define OVR_XMIT pfnStartXmit
|
---|
353 |
|
---|
354 | # else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
|
---|
355 |
|
---|
356 | typedef struct net_device_ops OVR_OPSTYPE;
|
---|
357 | # define OVR_OPS netdev_ops
|
---|
358 | # define OVR_XMIT pOrgOps->ndo_start_xmit
|
---|
359 |
|
---|
360 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * The overridden net_device_ops of the device we're attached to.
|
---|
364 | *
|
---|
365 | * As there is no net_device_ops structure in pre-2.6.29 kernels we override
|
---|
366 | * ethtool_ops instead along with hard_start_xmit callback in net_device
|
---|
367 | * structure.
|
---|
368 | *
|
---|
369 | * This is a very dirty hack that was created to explore how much we can improve
|
---|
370 | * the host to guest transfers by not CC'ing the NIC. It turns out to be
|
---|
371 | * the only way to filter outgoing packets for devices without TX queue.
|
---|
372 | */
|
---|
373 | typedef struct VBoxNetDeviceOpsOverride
|
---|
374 | {
|
---|
375 | /** Our overridden ops. */
|
---|
376 | OVR_OPSTYPE Ops;
|
---|
377 | /** Magic word. */
|
---|
378 | uint32_t u32Magic;
|
---|
379 | /** Pointer to the original ops. */
|
---|
380 | OVR_OPSTYPE const *pOrgOps;
|
---|
381 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
|
---|
382 | /** Pointer to the original hard_start_xmit function. */
|
---|
383 | int (*pfnStartXmit)(struct sk_buff *pSkb, struct net_device *pDev);
|
---|
384 | # endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
|
---|
385 | /** Pointer to the net filter instance. */
|
---|
386 | PVBOXNETFLTINS pVBoxNetFlt;
|
---|
387 | /** The number of filtered packages. */
|
---|
388 | uint64_t cFiltered;
|
---|
389 | /** The total number of packets */
|
---|
390 | uint64_t cTotal;
|
---|
391 | } VBOXNETDEVICEOPSOVERRIDE, *PVBOXNETDEVICEOPSOVERRIDE;
|
---|
392 | /** VBOXNETDEVICEOPSOVERRIDE::u32Magic value. */
|
---|
393 | #define VBOXNETDEVICEOPSOVERRIDE_MAGIC UINT32_C(0x00c0ffee)
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * ndo_start_xmit wrapper that drops packets that shouldn't go to the wire
|
---|
397 | * because they belong on the internal network.
|
---|
398 | *
|
---|
399 | * @returns NETDEV_TX_XXX.
|
---|
400 | * @param pSkb The socket buffer to transmit.
|
---|
401 | * @param pDev The net device.
|
---|
402 | */
|
---|
403 | static int vboxNetFltLinuxStartXmitFilter(struct sk_buff *pSkb, struct net_device *pDev)
|
---|
404 | {
|
---|
405 | PVBOXNETDEVICEOPSOVERRIDE pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->OVR_OPS;
|
---|
406 | uint8_t abHdrBuf[sizeof(RTNETETHERHDR) + sizeof(uint32_t) + RTNETIPV4_MIN_LEN];
|
---|
407 | PCRTNETETHERHDR pEtherHdr;
|
---|
408 | PINTNETTRUNKSWPORT pSwitchPort;
|
---|
409 | uint32_t cbHdrs;
|
---|
410 |
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * Validate the override structure.
|
---|
414 | *
|
---|
415 | * Note! We're racing vboxNetFltLinuxUnhookDev here. If this was supposed
|
---|
416 | * to be production quality code, we would have to be much more
|
---|
417 | * careful here and avoid the race.
|
---|
418 | */
|
---|
419 | if ( !VALID_PTR(pOverride)
|
---|
420 | || pOverride->u32Magic != VBOXNETDEVICEOPSOVERRIDE_MAGIC
|
---|
421 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
422 | || !VALID_PTR(pOverride->pOrgOps)
|
---|
423 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
|
---|
424 | )
|
---|
425 | {
|
---|
426 | printk("vboxNetFltLinuxStartXmitFilter: bad override %p\n", pOverride);
|
---|
427 | dev_kfree_skb(pSkb);
|
---|
428 | return NETDEV_TX_OK;
|
---|
429 | }
|
---|
430 | pOverride->cTotal++;
|
---|
431 |
|
---|
432 | /*
|
---|
433 | * Do the filtering base on the default OUI of our virtual NICs
|
---|
434 | *
|
---|
435 | * Note! In a real solution, we would ask the switch whether the
|
---|
436 | * destination MAC is 100% to be on the internal network and then
|
---|
437 | * drop it.
|
---|
438 | */
|
---|
439 | cbHdrs = skb_headlen(pSkb);
|
---|
440 | cbHdrs = RT_MIN(cbHdrs, sizeof(abHdrBuf));
|
---|
441 | pEtherHdr = (PCRTNETETHERHDR)skb_header_pointer(pSkb, 0, cbHdrs, &abHdrBuf[0]);
|
---|
442 | if ( pEtherHdr
|
---|
443 | && VALID_PTR(pOverride->pVBoxNetFlt)
|
---|
444 | && (pSwitchPort = pOverride->pVBoxNetFlt->pSwitchPort) != NULL
|
---|
445 | && VALID_PTR(pSwitchPort)
|
---|
446 | && cbHdrs >= 6)
|
---|
447 | {
|
---|
448 | INTNETSWDECISION enmDecision;
|
---|
449 |
|
---|
450 | /** @todo consider reference counting, etc. */
|
---|
451 | enmDecision = pSwitchPort->pfnPreRecv(pSwitchPort, pEtherHdr, cbHdrs, INTNETTRUNKDIR_HOST);
|
---|
452 | if (enmDecision == INTNETSWDECISION_INTNET)
|
---|
453 | {
|
---|
454 | dev_kfree_skb(pSkb);
|
---|
455 | pOverride->cFiltered++;
|
---|
456 | return NETDEV_TX_OK;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | return pOverride->OVR_XMIT(pSkb, pDev);
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Hooks the device ndo_start_xmit operation of the device.
|
---|
465 | *
|
---|
466 | * @param pThis The net filter instance.
|
---|
467 | * @param pDev The net device.
|
---|
468 | */
|
---|
469 | static void vboxNetFltLinuxHookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
470 | {
|
---|
471 | PVBOXNETDEVICEOPSOVERRIDE pOverride;
|
---|
472 |
|
---|
473 | /* Cancel override if ethtool_ops is missing (host-only case, @bugref{5712}) */
|
---|
474 | if (!VALID_PTR(pDev->OVR_OPS))
|
---|
475 | return;
|
---|
476 | pOverride = RTMemAlloc(sizeof(*pOverride));
|
---|
477 | if (!pOverride)
|
---|
478 | return;
|
---|
479 | pOverride->pOrgOps = pDev->OVR_OPS;
|
---|
480 | pOverride->Ops = *pDev->OVR_OPS;
|
---|
481 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
|
---|
482 | pOverride->pfnStartXmit = pDev->hard_start_xmit;
|
---|
483 | # else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
|
---|
484 | pOverride->Ops.ndo_start_xmit = vboxNetFltLinuxStartXmitFilter;
|
---|
485 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) */
|
---|
486 | pOverride->u32Magic = VBOXNETDEVICEOPSOVERRIDE_MAGIC;
|
---|
487 | pOverride->cTotal = 0;
|
---|
488 | pOverride->cFiltered = 0;
|
---|
489 | pOverride->pVBoxNetFlt = pThis;
|
---|
490 |
|
---|
491 | RTSpinlockAcquire(pThis->hSpinlock); /* (this isn't necessary, but so what) */
|
---|
492 | ASMAtomicWritePtr((void * volatile *)&pDev->OVR_OPS, pOverride);
|
---|
493 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
|
---|
494 | ASMAtomicXchgPtr((void * volatile *)&pDev->hard_start_xmit, vboxNetFltLinuxStartXmitFilter);
|
---|
495 | # endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
|
---|
496 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * Undos what vboxNetFltLinuxHookDev did.
|
---|
501 | *
|
---|
502 | * @param pThis The net filter instance.
|
---|
503 | * @param pDev The net device. Can be NULL, in which case
|
---|
504 | * we'll try retrieve it from @a pThis.
|
---|
505 | */
|
---|
506 | static void vboxNetFltLinuxUnhookDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
507 | {
|
---|
508 | PVBOXNETDEVICEOPSOVERRIDE pOverride;
|
---|
509 |
|
---|
510 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
511 | if (!pDev)
|
---|
512 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
513 | if (VALID_PTR(pDev))
|
---|
514 | {
|
---|
515 | pOverride = (PVBOXNETDEVICEOPSOVERRIDE)pDev->OVR_OPS;
|
---|
516 | if ( VALID_PTR(pOverride)
|
---|
517 | && pOverride->u32Magic == VBOXNETDEVICEOPSOVERRIDE_MAGIC
|
---|
518 | && VALID_PTR(pOverride->pOrgOps)
|
---|
519 | )
|
---|
520 | {
|
---|
521 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
|
---|
522 | ASMAtomicWritePtr((void * volatile *)&pDev->hard_start_xmit, pOverride->pfnStartXmit);
|
---|
523 | # endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
|
---|
524 | ASMAtomicWritePtr((void const * volatile *)&pDev->OVR_OPS, pOverride->pOrgOps);
|
---|
525 | ASMAtomicWriteU32(&pOverride->u32Magic, 0);
|
---|
526 | }
|
---|
527 | else
|
---|
528 | pOverride = NULL;
|
---|
529 | }
|
---|
530 | else
|
---|
531 | pOverride = NULL;
|
---|
532 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
533 |
|
---|
534 | if (pOverride)
|
---|
535 | {
|
---|
536 | printk("vboxnetflt: %llu out of %llu packets were not sent (directed to host)\n", pOverride->cFiltered, pOverride->cTotal);
|
---|
537 | RTMemFree(pOverride);
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | #endif /* VBOXNETFLT_WITH_HOST2WIRE_FILTER */
|
---|
542 |
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * Reads and retains the host interface handle.
|
---|
546 | *
|
---|
547 | * @returns The handle, NULL if detached.
|
---|
548 | * @param pThis
|
---|
549 | */
|
---|
550 | DECLINLINE(struct net_device *) vboxNetFltLinuxRetainNetDev(PVBOXNETFLTINS pThis)
|
---|
551 | {
|
---|
552 | #if 0
|
---|
553 | struct net_device *pDev = NULL;
|
---|
554 |
|
---|
555 | Log(("vboxNetFltLinuxRetainNetDev\n"));
|
---|
556 | /*
|
---|
557 | * Be careful here to avoid problems racing the detached callback.
|
---|
558 | */
|
---|
559 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
560 | if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
|
---|
561 | {
|
---|
562 | pDev = (struct net_device *)ASMAtomicUoReadPtr((void * volatile *)&pThis->u.s.pDev);
|
---|
563 | if (pDev)
|
---|
564 | {
|
---|
565 | dev_hold(pDev);
|
---|
566 | Log(("vboxNetFltLinuxRetainNetDev: Device %p(%s) retained. ref=%d\n",
|
---|
567 | pDev, pDev->name,
|
---|
568 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
569 | netdev_refcnt_read(pDev)
|
---|
570 | #else
|
---|
571 | atomic_read(&pDev->refcnt)
|
---|
572 | #endif
|
---|
573 | ));
|
---|
574 | }
|
---|
575 | }
|
---|
576 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
577 |
|
---|
578 | Log(("vboxNetFltLinuxRetainNetDev - done\n"));
|
---|
579 | return pDev;
|
---|
580 | #else
|
---|
581 | return ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
582 | #endif
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Release the host interface handle previously retained
|
---|
588 | * by vboxNetFltLinuxRetainNetDev.
|
---|
589 | *
|
---|
590 | * @param pThis The instance.
|
---|
591 | * @param pDev The vboxNetFltLinuxRetainNetDev
|
---|
592 | * return value, NULL is fine.
|
---|
593 | */
|
---|
594 | DECLINLINE(void) vboxNetFltLinuxReleaseNetDev(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
595 | {
|
---|
596 | #if 0
|
---|
597 | Log(("vboxNetFltLinuxReleaseNetDev\n"));
|
---|
598 | NOREF(pThis);
|
---|
599 | if (pDev)
|
---|
600 | {
|
---|
601 | dev_put(pDev);
|
---|
602 | Log(("vboxNetFltLinuxReleaseNetDev: Device %p(%s) released. ref=%d\n",
|
---|
603 | pDev, pDev->name,
|
---|
604 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
605 | netdev_refcnt_read(pDev)
|
---|
606 | #else
|
---|
607 | atomic_read(&pDev->refcnt)
|
---|
608 | #endif
|
---|
609 | ));
|
---|
610 | }
|
---|
611 | Log(("vboxNetFltLinuxReleaseNetDev - done\n"));
|
---|
612 | #endif
|
---|
613 | }
|
---|
614 |
|
---|
615 | #define VBOXNETFLT_CB_TAG(skb) (0xA1C90000 | (skb->dev->ifindex & 0xFFFF))
|
---|
616 | #define VBOXNETFLT_SKB_TAG(skb) (*(uint32_t*)&((skb)->cb[sizeof((skb)->cb)-sizeof(uint32_t)]))
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Checks whether this is an mbuf created by vboxNetFltLinuxMBufFromSG,
|
---|
620 | * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
|
---|
621 | *
|
---|
622 | * @returns true / false accordingly.
|
---|
623 | * @param pBuf The sk_buff.
|
---|
624 | */
|
---|
625 | DECLINLINE(bool) vboxNetFltLinuxSkBufIsOur(struct sk_buff *pBuf)
|
---|
626 | {
|
---|
627 | return VBOXNETFLT_SKB_TAG(pBuf) == VBOXNETFLT_CB_TAG(pBuf);
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Checks whether this SG list contains a GSO packet.
|
---|
633 | *
|
---|
634 | * @returns true / false accordingly.
|
---|
635 | * @param pSG The (scatter/)gather list.
|
---|
636 | */
|
---|
637 | DECLINLINE(bool) vboxNetFltLinuxIsGso(PINTNETSG pSG)
|
---|
638 | {
|
---|
639 | #if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
|
---|
640 | return !((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID);
|
---|
641 | #else /* !VBOXNETFLT_WITH_GSO_XMIT_WIRE && !VBOXNETFLT_WITH_GSO_XMIT_HOST */
|
---|
642 | return false;
|
---|
643 | #endif /* !VBOXNETFLT_WITH_GSO_XMIT_WIRE && !VBOXNETFLT_WITH_GSO_XMIT_HOST */
|
---|
644 | }
|
---|
645 |
|
---|
646 |
|
---|
647 | /**
|
---|
648 | * Find out the frame size (of a single segment in case of GSO frames).
|
---|
649 | *
|
---|
650 | * @returns the frame size.
|
---|
651 | * @param pSG The (scatter/)gather list.
|
---|
652 | */
|
---|
653 | DECLINLINE(uint32_t) vboxNetFltLinuxFrameSize(PINTNETSG pSG)
|
---|
654 | {
|
---|
655 | uint16_t u16Type = 0;
|
---|
656 | uint32_t cbVlanTag = 0;
|
---|
657 | if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR))
|
---|
658 | u16Type = RT_BE2H_U16(((PCRTNETETHERHDR)pSG->aSegs[0].pv)->EtherType);
|
---|
659 | else if (pSG->cbTotal >= sizeof(RTNETETHERHDR))
|
---|
660 | {
|
---|
661 | uint32_t off = RT_UOFFSETOF(RTNETETHERHDR, EtherType);
|
---|
662 | uint32_t i;
|
---|
663 | for (i = 0; i < pSG->cSegsUsed; ++i)
|
---|
664 | {
|
---|
665 | if (off <= pSG->aSegs[i].cb)
|
---|
666 | {
|
---|
667 | if (off + sizeof(uint16_t) <= pSG->aSegs[i].cb)
|
---|
668 | u16Type = RT_BE2H_U16(*(uint16_t *)((uintptr_t)pSG->aSegs[i].pv + off));
|
---|
669 | else if (i + 1 < pSG->cSegsUsed)
|
---|
670 | u16Type = RT_BE2H_U16( ((uint16_t)( ((uint8_t *)pSG->aSegs[i].pv)[off] ) << 8)
|
---|
671 | + *(uint8_t *)pSG->aSegs[i + 1].pv); /* ASSUMES no empty segments! */
|
---|
672 | /* else: frame is too short. */
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | off -= pSG->aSegs[i].cb;
|
---|
676 | }
|
---|
677 | }
|
---|
678 | if (u16Type == RTNET_ETHERTYPE_VLAN)
|
---|
679 | cbVlanTag = 4;
|
---|
680 | return (vboxNetFltLinuxIsGso(pSG) ? (uint32_t)pSG->GsoCtx.cbMaxSeg + pSG->GsoCtx.cbHdrsTotal : pSG->cbTotal) - cbVlanTag;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * Internal worker that create a linux sk_buff for a
|
---|
686 | * (scatter/)gather list.
|
---|
687 | *
|
---|
688 | * @returns Pointer to the sk_buff.
|
---|
689 | * @param pThis The instance.
|
---|
690 | * @param pSG The (scatter/)gather list.
|
---|
691 | * @param fDstWire Set if the destination is the wire.
|
---|
692 | */
|
---|
693 | static struct sk_buff *vboxNetFltLinuxSkBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG, bool fDstWire)
|
---|
694 | {
|
---|
695 | struct sk_buff *pPkt;
|
---|
696 | struct net_device *pDev;
|
---|
697 | #if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
|
---|
698 | unsigned fGsoType = 0;
|
---|
699 | #endif
|
---|
700 |
|
---|
701 | if (pSG->cbTotal == 0)
|
---|
702 | {
|
---|
703 | LogRel(("VBoxNetFlt: Dropped empty packet coming from internal network.\n"));
|
---|
704 | return NULL;
|
---|
705 | }
|
---|
706 | Log5(("VBoxNetFlt: Packet to %s of %d bytes (frame=%d).\n", fDstWire?"wire":"host", pSG->cbTotal, vboxNetFltLinuxFrameSize(pSG)));
|
---|
707 | if (fDstWire && (vboxNetFltLinuxFrameSize(pSG) > ASMAtomicReadU32(&pThis->u.s.cbMtu) + 14))
|
---|
708 | {
|
---|
709 | static bool s_fOnce = true;
|
---|
710 | if (s_fOnce)
|
---|
711 | {
|
---|
712 | s_fOnce = false;
|
---|
713 | printk("VBoxNetFlt: Dropped over-sized packet (%d bytes) coming from internal network.\n", vboxNetFltLinuxFrameSize(pSG));
|
---|
714 | }
|
---|
715 | return NULL;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /** @todo We should use fragments mapping the SG buffers with large packets.
|
---|
719 | * 256 bytes seems to be the a threshold used a lot for this. It
|
---|
720 | * requires some nasty work on the intnet side though... */
|
---|
721 | /*
|
---|
722 | * Allocate a packet and copy over the data.
|
---|
723 | */
|
---|
724 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
725 | pPkt = dev_alloc_skb(pSG->cbTotal + NET_IP_ALIGN);
|
---|
726 | if (RT_UNLIKELY(!pPkt))
|
---|
727 | {
|
---|
728 | Log(("vboxNetFltLinuxSkBufFromSG: Failed to allocate sk_buff(%u).\n", pSG->cbTotal));
|
---|
729 | pSG->pvUserData = NULL;
|
---|
730 | return NULL;
|
---|
731 | }
|
---|
732 | pPkt->dev = pDev;
|
---|
733 | pPkt->ip_summed = CHECKSUM_NONE;
|
---|
734 |
|
---|
735 | /* Align IP header on 16-byte boundary: 2 + 14 (ethernet hdr size). */
|
---|
736 | skb_reserve(pPkt, NET_IP_ALIGN);
|
---|
737 |
|
---|
738 | /* Copy the segments. */
|
---|
739 | skb_put(pPkt, pSG->cbTotal);
|
---|
740 | IntNetSgRead(pSG, pPkt->data);
|
---|
741 |
|
---|
742 | #if defined(VBOXNETFLT_WITH_GSO_XMIT_WIRE) || defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
|
---|
743 | /*
|
---|
744 | * Setup GSO if used by this packet.
|
---|
745 | */
|
---|
746 | switch ((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type)
|
---|
747 | {
|
---|
748 | default:
|
---|
749 | AssertMsgFailed(("%u (%s)\n", pSG->GsoCtx.u8Type, PDMNetGsoTypeName((PDMNETWORKGSOTYPE)pSG->GsoCtx.u8Type) ));
|
---|
750 | /* fall thru */
|
---|
751 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
752 | fGsoType = 0;
|
---|
753 | break;
|
---|
754 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
755 | fGsoType = SKB_GSO_TCPV4;
|
---|
756 | break;
|
---|
757 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
758 | fGsoType = SKB_GSO_TCPV6;
|
---|
759 | break;
|
---|
760 | }
|
---|
761 | if (fGsoType)
|
---|
762 | {
|
---|
763 | struct skb_shared_info *pShInfo = skb_shinfo(pPkt);
|
---|
764 |
|
---|
765 | pShInfo->gso_type = fGsoType | SKB_GSO_DODGY;
|
---|
766 | pShInfo->gso_size = pSG->GsoCtx.cbMaxSeg;
|
---|
767 | pShInfo->gso_segs = PDMNetGsoCalcSegmentCount(&pSG->GsoCtx, pSG->cbTotal);
|
---|
768 |
|
---|
769 | /*
|
---|
770 | * We need to set checksum fields even if the packet goes to the host
|
---|
771 | * directly as it may be immediately forwarded by IP layer @bugref{5020}.
|
---|
772 | */
|
---|
773 | Assert(skb_headlen(pPkt) >= pSG->GsoCtx.cbHdrsTotal);
|
---|
774 | pPkt->ip_summed = CHECKSUM_PARTIAL;
|
---|
775 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
776 | pPkt->csum_start = skb_headroom(pPkt) + pSG->GsoCtx.offHdr2;
|
---|
777 | if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
|
---|
778 | pPkt->csum_offset = RT_UOFFSETOF(RTNETTCP, th_sum);
|
---|
779 | else
|
---|
780 | pPkt->csum_offset = RT_UOFFSETOF(RTNETUDP, uh_sum);
|
---|
781 | # else
|
---|
782 | pPkt->h.raw = pPkt->data + pSG->GsoCtx.offHdr2;
|
---|
783 | if (fGsoType & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
|
---|
784 | pPkt->csum = RT_UOFFSETOF(RTNETTCP, th_sum);
|
---|
785 | else
|
---|
786 | pPkt->csum = RT_UOFFSETOF(RTNETUDP, uh_sum);
|
---|
787 | # endif
|
---|
788 | if (!fDstWire)
|
---|
789 | PDMNetGsoPrepForDirectUse(&pSG->GsoCtx, pPkt->data, pSG->cbTotal, PDMNETCSUMTYPE_PSEUDO);
|
---|
790 | }
|
---|
791 | #endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE || VBOXNETFLT_WITH_GSO_XMIT_HOST */
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Finish up the socket buffer.
|
---|
795 | */
|
---|
796 | pPkt->protocol = eth_type_trans(pPkt, pDev);
|
---|
797 | if (fDstWire)
|
---|
798 | {
|
---|
799 | VBOX_SKB_RESET_NETWORK_HDR(pPkt);
|
---|
800 |
|
---|
801 | /* Restore ethernet header back. */
|
---|
802 | skb_push(pPkt, ETH_HLEN); /** @todo VLAN: +4 if VLAN? */
|
---|
803 | VBOX_SKB_RESET_MAC_HDR(pPkt);
|
---|
804 | }
|
---|
805 | VBOXNETFLT_SKB_TAG(pPkt) = VBOXNETFLT_CB_TAG(pPkt);
|
---|
806 |
|
---|
807 | return pPkt;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 | /**
|
---|
812 | * Return the offset where to start checksum computation from.
|
---|
813 | *
|
---|
814 | * @returns the offset relative to pBuf->data.
|
---|
815 | * @param pBuf The socket buffer.
|
---|
816 | */
|
---|
817 | DECLINLINE(unsigned) vboxNetFltLinuxGetChecksumStartOffset(struct sk_buff *pBuf)
|
---|
818 | {
|
---|
819 | # if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 21)
|
---|
820 | unsigned char *pTransportHdr = pBuf->h.raw;
|
---|
821 | # if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
|
---|
822 | /*
|
---|
823 | * Try to work around the problem with CentOS 4.7 and 5.2 (2.6.9
|
---|
824 | * and 2.6.18 kernels), they pass wrong 'h' pointer down. We take IP
|
---|
825 | * header length from the header itself and reconstruct 'h' pointer
|
---|
826 | * to TCP (or whatever) header.
|
---|
827 | */
|
---|
828 | if (pBuf->h.raw == pBuf->nh.raw && pBuf->protocol == htons(ETH_P_IP))
|
---|
829 | pTransportHdr = pBuf->nh.raw + pBuf->nh.iph->ihl * 4;
|
---|
830 | # endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
|
---|
831 | return pTransportHdr - pBuf->data;
|
---|
832 | # else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 21) */
|
---|
833 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
|
---|
834 | return pBuf->csum_start - skb_headroom(pBuf);
|
---|
835 | # else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38) */
|
---|
836 | return skb_checksum_start_offset(pBuf);
|
---|
837 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38) */
|
---|
838 | # endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 21) */
|
---|
839 | }
|
---|
840 |
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * Initializes a SG list from an sk_buff.
|
---|
844 | *
|
---|
845 | * @returns Number of segments.
|
---|
846 | * @param pThis The instance.
|
---|
847 | * @param pBuf The sk_buff.
|
---|
848 | * @param pSG The SG.
|
---|
849 | * @param cbExtra The number of bytes of extra space allocated immediately after the SG.
|
---|
850 | * @param cSegs The number of segments allocated for the SG.
|
---|
851 | * This should match the number in the mbuf exactly!
|
---|
852 | * @param fSrc The source of the frame.
|
---|
853 | * @param pGsoCtx Pointer to the GSO context if it's a GSO
|
---|
854 | * internal network frame. NULL if regular frame.
|
---|
855 | */
|
---|
856 | static void vboxNetFltLinuxSkBufToSG(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, PINTNETSG pSG,
|
---|
857 | unsigned cbExtra, unsigned cSegs, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
|
---|
858 | {
|
---|
859 | int i;
|
---|
860 | NOREF(pThis);
|
---|
861 |
|
---|
862 | #ifndef VBOXNETFLT_SG_SUPPORT
|
---|
863 | Assert(!skb_shinfo(pBuf)->frag_list);
|
---|
864 | #else /* VBOXNETFLT_SG_SUPPORT */
|
---|
865 | uint8_t *pExtra = (uint8_t *)&pSG->aSegs[cSegs];
|
---|
866 | unsigned cbConsumed = 0;
|
---|
867 | unsigned cbProduced = 0;
|
---|
868 |
|
---|
869 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
870 | /* Restore VLAN tag stripped by host hardware */
|
---|
871 | if (vlan_tx_tag_present(pBuf))
|
---|
872 | {
|
---|
873 | uint8_t *pMac = pBuf->data;
|
---|
874 | struct vlan_ethhdr *pVHdr = (struct vlan_ethhdr *)pExtra;
|
---|
875 | Assert(ETH_ALEN * 2 + VLAN_HLEN <= cbExtra);
|
---|
876 | memmove(pVHdr, pMac, ETH_ALEN * 2);
|
---|
877 | /* Consume whole Ethernet header: 2 addresses + EtherType (see @bugref{8599}) */
|
---|
878 | cbConsumed += ETH_ALEN * 2 + sizeof(uint16_t);
|
---|
879 | pVHdr->h_vlan_proto = RT_H2N_U16(ETH_P_8021Q);
|
---|
880 | pVHdr->h_vlan_TCI = RT_H2N_U16(vlan_tx_tag_get(pBuf));
|
---|
881 | pVHdr->h_vlan_encapsulated_proto = *(uint16_t*)(pMac + ETH_ALEN * 2);
|
---|
882 | cbProduced += VLAN_ETH_HLEN;
|
---|
883 | }
|
---|
884 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) */
|
---|
885 |
|
---|
886 | if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
|
---|
887 | {
|
---|
888 | unsigned uCsumStartOffset = vboxNetFltLinuxGetChecksumStartOffset(pBuf);
|
---|
889 | unsigned uCsumStoreOffset = uCsumStartOffset + VBOX_SKB_CSUM_OFFSET(pBuf) - cbConsumed;
|
---|
890 | Log3(("cbConsumed=%u cbProduced=%u uCsumStartOffset=%u uCsumStoreOffset=%u\n",
|
---|
891 | cbConsumed, cbProduced, uCsumStartOffset, uCsumStoreOffset));
|
---|
892 | Assert(cbProduced + uCsumStoreOffset + sizeof(uint16_t) <= cbExtra);
|
---|
893 | /*
|
---|
894 | * We assume that the checksum is stored at the very end of the transport header
|
---|
895 | * so we will have all headers in a single fragment. If our assumption is wrong
|
---|
896 | * we may see suboptimal performance.
|
---|
897 | */
|
---|
898 | memmove(pExtra + cbProduced,
|
---|
899 | pBuf->data + cbConsumed,
|
---|
900 | uCsumStoreOffset);
|
---|
901 | unsigned uChecksum = skb_checksum(pBuf, uCsumStartOffset, pBuf->len - uCsumStartOffset, 0);
|
---|
902 | *(uint16_t*)(pExtra + cbProduced + uCsumStoreOffset) = csum_fold(uChecksum);
|
---|
903 | cbProduced += uCsumStoreOffset + sizeof(uint16_t);
|
---|
904 | cbConsumed += uCsumStoreOffset + sizeof(uint16_t);
|
---|
905 | }
|
---|
906 | #endif /* VBOXNETFLT_SG_SUPPORT */
|
---|
907 |
|
---|
908 | if (!pGsoCtx)
|
---|
909 | IntNetSgInitTempSegs(pSG, pBuf->len + cbProduced - cbConsumed, cSegs, 0 /*cSegsUsed*/);
|
---|
910 | else
|
---|
911 | IntNetSgInitTempSegsGso(pSG, pBuf->len + cbProduced - cbConsumed, cSegs, 0 /*cSegsUsed*/, pGsoCtx);
|
---|
912 |
|
---|
913 | int iSeg = 0;
|
---|
914 | #ifdef VBOXNETFLT_SG_SUPPORT
|
---|
915 | if (cbProduced)
|
---|
916 | {
|
---|
917 | pSG->aSegs[iSeg].cb = cbProduced;
|
---|
918 | pSG->aSegs[iSeg].pv = pExtra;
|
---|
919 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
920 | }
|
---|
921 | pSG->aSegs[iSeg].cb = skb_headlen(pBuf) - cbConsumed;
|
---|
922 | pSG->aSegs[iSeg].pv = pBuf->data + cbConsumed;
|
---|
923 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
924 | Assert(iSeg <= pSG->cSegsAlloc);
|
---|
925 |
|
---|
926 | # ifdef LOG_ENABLED
|
---|
927 | if (pBuf->data_len)
|
---|
928 | Log6((" kmap_atomic:"));
|
---|
929 | # endif /* LOG_ENABLED */
|
---|
930 | for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
|
---|
931 | {
|
---|
932 | skb_frag_t *pFrag = &skb_shinfo(pBuf)->frags[i];
|
---|
933 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0) || defined(OPENSUSE_152)
|
---|
934 | pSG->aSegs[iSeg].cb = pFrag->bv_len;
|
---|
935 | pSG->aSegs[iSeg].pv = VBOX_SKB_KMAP_FRAG(pFrag) + pFrag->bv_offset;
|
---|
936 | # else /* < KERNEL_VERSION(5, 4, 0) */
|
---|
937 | pSG->aSegs[iSeg].cb = pFrag->size;
|
---|
938 | pSG->aSegs[iSeg].pv = VBOX_SKB_KMAP_FRAG(pFrag) + pFrag->page_offset;
|
---|
939 | # endif /* >= KERNEL_VERSION(5, 4, 0) */
|
---|
940 | Log6((" %p", pSG->aSegs[iSeg].pv));
|
---|
941 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
942 | Assert(iSeg <= pSG->cSegsAlloc);
|
---|
943 | }
|
---|
944 | struct sk_buff *pFragBuf;
|
---|
945 | for (pFragBuf = skb_shinfo(pBuf)->frag_list; pFragBuf; pFragBuf = pFragBuf->next)
|
---|
946 | {
|
---|
947 | pSG->aSegs[iSeg].cb = skb_headlen(pFragBuf);
|
---|
948 | pSG->aSegs[iSeg].pv = pFragBuf->data;
|
---|
949 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
950 | Assert(iSeg <= pSG->cSegsAlloc);
|
---|
951 | for (i = 0; i < skb_shinfo(pFragBuf)->nr_frags; i++)
|
---|
952 | {
|
---|
953 | skb_frag_t *pFrag = &skb_shinfo(pFragBuf)->frags[i];
|
---|
954 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0) || defined(OPENSUSE_152)
|
---|
955 | pSG->aSegs[iSeg].cb = pFrag->bv_len;
|
---|
956 | pSG->aSegs[iSeg].pv = VBOX_SKB_KMAP_FRAG(pFrag) + pFrag->bv_offset;
|
---|
957 | # else /* < KERNEL_VERSION(5, 4, 0) */
|
---|
958 | pSG->aSegs[iSeg].cb = pFrag->size;
|
---|
959 | pSG->aSegs[iSeg].pv = VBOX_SKB_KMAP_FRAG(pFrag) + pFrag->page_offset;
|
---|
960 | # endif /* >= KERNEL_VERSION(5, 4, 0) */
|
---|
961 | Log6((" %p", pSG->aSegs[iSeg].pv));
|
---|
962 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
963 | Assert(iSeg <= pSG->cSegsAlloc);
|
---|
964 | }
|
---|
965 | }
|
---|
966 | # ifdef LOG_ENABLED
|
---|
967 | if (pBuf->data_len)
|
---|
968 | Log6(("\n"));
|
---|
969 | # endif /* LOG_ENABLED */
|
---|
970 | #else
|
---|
971 | pSG->aSegs[iSeg].cb = pBuf->len;
|
---|
972 | pSG->aSegs[iSeg].pv = pBuf->data;
|
---|
973 | pSG->aSegs[iSeg++].Phys = NIL_RTHCPHYS;
|
---|
974 | #endif
|
---|
975 |
|
---|
976 | pSG->cSegsUsed = iSeg;
|
---|
977 |
|
---|
978 | #if 0
|
---|
979 | if (cbProduced)
|
---|
980 | {
|
---|
981 | LogRel(("vboxNetFltLinuxSkBufToSG: original packet dump:\n%.*Rhxd\n", pBuf->len-pBuf->data_len, skb_mac_header(pBuf)));
|
---|
982 | LogRel(("vboxNetFltLinuxSkBufToSG: cbConsumed=%u cbProduced=%u cbExtra=%u\n", cbConsumed, cbProduced, cbExtra));
|
---|
983 | uint32_t offset = 0;
|
---|
984 | for (i = 0; i < pSG->cSegsUsed; ++i)
|
---|
985 | {
|
---|
986 | LogRel(("vboxNetFltLinuxSkBufToSG: seg#%d (%d bytes, starting at 0x%x):\n%.*Rhxd\n",
|
---|
987 | i, pSG->aSegs[i].cb, offset, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
|
---|
988 | offset += pSG->aSegs[i].cb;
|
---|
989 | }
|
---|
990 | }
|
---|
991 | #endif
|
---|
992 |
|
---|
993 | #ifdef PADD_RUNT_FRAMES_FROM_HOST
|
---|
994 | /*
|
---|
995 | * Add a trailer if the frame is too small.
|
---|
996 | *
|
---|
997 | * Since we're getting to the packet before it is framed, it has not
|
---|
998 | * yet been padded. The current solution is to add a segment pointing
|
---|
999 | * to a buffer containing all zeros and pray that works for all frames...
|
---|
1000 | */
|
---|
1001 | if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
|
---|
1002 | {
|
---|
1003 | Assert(pBuf->data_len == 0); /* Packets with fragments are never small! */
|
---|
1004 | static uint8_t const s_abZero[128] = {0};
|
---|
1005 |
|
---|
1006 | AssertReturnVoid(iSeg < cSegs);
|
---|
1007 |
|
---|
1008 | pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
|
---|
1009 | pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
|
---|
1010 | pSG->aSegs[iSeg++].cb = 60 - pSG->cbTotal;
|
---|
1011 | pSG->cbTotal = 60;
|
---|
1012 | pSG->cSegsUsed++;
|
---|
1013 | Assert(iSeg <= pSG->cSegsAlloc)
|
---|
1014 | }
|
---|
1015 | #endif
|
---|
1016 |
|
---|
1017 | Log6(("vboxNetFltLinuxSkBufToSG: allocated=%d, segments=%d frags=%d next=%p frag_list=%p pkt_type=%x fSrc=%x\n",
|
---|
1018 | pSG->cSegsAlloc, pSG->cSegsUsed, skb_shinfo(pBuf)->nr_frags, pBuf->next, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, fSrc));
|
---|
1019 | for (i = 0; i < pSG->cSegsUsed; i++)
|
---|
1020 | Log6(("vboxNetFltLinuxSkBufToSG: #%d: cb=%d pv=%p\n",
|
---|
1021 | i, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /**
|
---|
1025 | * Packet handler; not really documented - figure it out yourself.
|
---|
1026 | *
|
---|
1027 | * @returns 0 or EJUSTRETURN - this is probably copy & pastry and thus wrong.
|
---|
1028 | */
|
---|
1029 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
|
---|
1030 | static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
|
---|
1031 | struct net_device *pSkbDev,
|
---|
1032 | struct packet_type *pPacketType,
|
---|
1033 | struct net_device *pOrigDev)
|
---|
1034 | #else
|
---|
1035 | static int vboxNetFltLinuxPacketHandler(struct sk_buff *pBuf,
|
---|
1036 | struct net_device *pSkbDev,
|
---|
1037 | struct packet_type *pPacketType)
|
---|
1038 | #endif
|
---|
1039 | {
|
---|
1040 | PVBOXNETFLTINS pThis;
|
---|
1041 | struct net_device *pDev;
|
---|
1042 | LogFlow(("vboxNetFltLinuxPacketHandler: pBuf=%p pSkbDev=%p pPacketType=%p\n",
|
---|
1043 | pBuf, pSkbDev, pPacketType));
|
---|
1044 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
|
---|
1045 | Log3(("vboxNetFltLinuxPacketHandler: skb len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
|
---|
1046 | pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size, skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
|
---|
1047 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
1048 | Log6(("vboxNetFltLinuxPacketHandler: packet dump follows:\n%.*Rhxd\n", pBuf->len-pBuf->data_len, skb_mac_header(pBuf)));
|
---|
1049 | # endif
|
---|
1050 | #else
|
---|
1051 | Log3(("vboxNetFltLinuxPacketHandler: skb len=%u data_len=%u truesize=%u next=%p nr_frags=%u tso_size=%u tso_seqs=%u frag_list=%p pkt_type=%x\n",
|
---|
1052 | pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->tso_size, skb_shinfo(pBuf)->tso_segs, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
|
---|
1053 | #endif
|
---|
1054 | /*
|
---|
1055 | * Drop it immediately?
|
---|
1056 | */
|
---|
1057 | if (!pBuf)
|
---|
1058 | return 0;
|
---|
1059 |
|
---|
1060 | if (pBuf->pkt_type == PACKET_LOOPBACK)
|
---|
1061 | {
|
---|
1062 | /*
|
---|
1063 | * We are not interested in loopbacked packets as they will always have
|
---|
1064 | * another copy going to the wire.
|
---|
1065 | */
|
---|
1066 | Log2(("vboxNetFltLinuxPacketHandler: dropped loopback packet (cb=%u)\n", pBuf->len));
|
---|
1067 | dev_kfree_skb(pBuf); /* We must 'consume' all packets we get (@bugref{6539})! */
|
---|
1068 | return 0;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | pThis = VBOX_FLT_PT_TO_INST(pPacketType);
|
---|
1072 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
1073 | if (pDev != pSkbDev)
|
---|
1074 | {
|
---|
1075 | Log(("vboxNetFltLinuxPacketHandler: Devices do not match, pThis may be wrong! pThis=%p\n", pThis));
|
---|
1076 | kfree_skb(pBuf); /* This is a failure, so we use kfree_skb instead of dev_kfree_skb. */
|
---|
1077 | return 0;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | Log6(("vboxNetFltLinuxPacketHandler: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
|
---|
1081 | if (vboxNetFltLinuxSkBufIsOur(pBuf))
|
---|
1082 | {
|
---|
1083 | Log2(("vboxNetFltLinuxPacketHandler: got our own sk_buff, drop it.\n"));
|
---|
1084 | dev_kfree_skb(pBuf);
|
---|
1085 | return 0;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | #ifndef VBOXNETFLT_SG_SUPPORT
|
---|
1089 | {
|
---|
1090 | /*
|
---|
1091 | * Get rid of fragmented packets, they cause too much trouble.
|
---|
1092 | */
|
---|
1093 | unsigned int uMacLen = pBuf->mac_len;
|
---|
1094 | struct sk_buff *pCopy = skb_copy(pBuf, GFP_ATOMIC);
|
---|
1095 | dev_kfree_skb(pBuf);
|
---|
1096 | if (!pCopy)
|
---|
1097 | {
|
---|
1098 | LogRel(("VBoxNetFlt: Failed to allocate packet buffer, dropping the packet.\n"));
|
---|
1099 | return 0;
|
---|
1100 | }
|
---|
1101 | pBuf = pCopy;
|
---|
1102 | /* Somehow skb_copy ignores mac_len */
|
---|
1103 | pBuf->mac_len = uMacLen;
|
---|
1104 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
1105 | /* Restore VLAN tag stripped by host hardware */
|
---|
1106 | if (vlan_tx_tag_present(pBuf) && skb_headroom(pBuf) >= VLAN_ETH_HLEN)
|
---|
1107 | {
|
---|
1108 | uint8_t *pMac = (uint8_t*)skb_mac_header(pBuf);
|
---|
1109 | struct vlan_ethhdr *pVHdr = (struct vlan_ethhdr *)(pMac - VLAN_HLEN);
|
---|
1110 | memmove(pVHdr, pMac, ETH_ALEN * 2);
|
---|
1111 | pVHdr->h_vlan_proto = RT_H2N_U16(ETH_P_8021Q);
|
---|
1112 | pVHdr->h_vlan_TCI = RT_H2N_U16(vlan_tx_tag_get(pBuf));
|
---|
1113 | pBuf->mac_header -= VLAN_HLEN;
|
---|
1114 | pBuf->mac_len += VLAN_HLEN;
|
---|
1115 | }
|
---|
1116 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) */
|
---|
1117 |
|
---|
1118 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
|
---|
1119 | Log3(("vboxNetFltLinuxPacketHandler: skb copy len=%u data_len=%u truesize=%u next=%p nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
|
---|
1120 | pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size, skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
|
---|
1121 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
1122 | Log6(("vboxNetFltLinuxPacketHandler: packet dump follows:\n%.*Rhxd\n", pBuf->len-pBuf->data_len, skb_mac_header(pBuf)));
|
---|
1123 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) */
|
---|
1124 | # else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
|
---|
1125 | Log3(("vboxNetFltLinuxPacketHandler: skb copy len=%u data_len=%u truesize=%u next=%p nr_frags=%u tso_size=%u tso_seqs=%u frag_list=%p pkt_type=%x\n",
|
---|
1126 | pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next, skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->tso_size, skb_shinfo(pBuf)->tso_segs, skb_shinfo(pBuf)->frag_list, pBuf->pkt_type));
|
---|
1127 | # endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
|
---|
1128 | }
|
---|
1129 | #endif /* !VBOXNETFLT_SG_SUPPORT */
|
---|
1130 |
|
---|
1131 | #ifdef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
1132 | /* Forward it to the internal network. */
|
---|
1133 | vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
|
---|
1134 | #else /* !VBOXNETFLT_LINUX_NO_XMIT_QUEUE */
|
---|
1135 | /* Add the packet to transmit queue and schedule the bottom half. */
|
---|
1136 | skb_queue_tail(&pThis->u.s.XmitQueue, pBuf);
|
---|
1137 | schedule_work(&pThis->u.s.XmitTask);
|
---|
1138 | Log6(("vboxNetFltLinuxPacketHandler: scheduled work %p for sk_buff %p\n",
|
---|
1139 | &pThis->u.s.XmitTask, pBuf));
|
---|
1140 | #endif /* !VBOXNETFLT_LINUX_NO_XMIT_QUEUE */
|
---|
1141 |
|
---|
1142 | /* It does not really matter what we return, it is ignored by the kernel. */
|
---|
1143 | return 0;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Calculate the number of INTNETSEG segments the socket buffer will need.
|
---|
1148 | *
|
---|
1149 | * @returns Segment count.
|
---|
1150 | * @param pBuf The socket buffer.
|
---|
1151 | * @param pcbTemp Where to store the number of bytes of the part
|
---|
1152 | * of the socket buffer that will be copied to
|
---|
1153 | * a temporary storage.
|
---|
1154 | */
|
---|
1155 | DECLINLINE(unsigned) vboxNetFltLinuxCalcSGSegments(struct sk_buff *pBuf, unsigned *pcbTemp)
|
---|
1156 | {
|
---|
1157 | *pcbTemp = 0;
|
---|
1158 | #ifdef VBOXNETFLT_SG_SUPPORT
|
---|
1159 | unsigned cSegs = 1 + skb_shinfo(pBuf)->nr_frags;
|
---|
1160 | if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
|
---|
1161 | {
|
---|
1162 | *pcbTemp = vboxNetFltLinuxGetChecksumStartOffset(pBuf) + VBOX_SKB_CSUM_OFFSET(pBuf) + sizeof(uint16_t);
|
---|
1163 | }
|
---|
1164 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
1165 | if (vlan_tx_tag_present(pBuf))
|
---|
1166 | {
|
---|
1167 | if (*pcbTemp)
|
---|
1168 | *pcbTemp += VLAN_HLEN;
|
---|
1169 | else
|
---|
1170 | *pcbTemp = VLAN_ETH_HLEN;
|
---|
1171 | }
|
---|
1172 | # endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) */
|
---|
1173 | if (*pcbTemp)
|
---|
1174 | ++cSegs;
|
---|
1175 | struct sk_buff *pFrag;
|
---|
1176 | for (pFrag = skb_shinfo(pBuf)->frag_list; pFrag; pFrag = pFrag->next)
|
---|
1177 | {
|
---|
1178 | Log6(("vboxNetFltLinuxCalcSGSegments: frag=%p len=%d data_len=%d frags=%d frag_list=%p next=%p\n",
|
---|
1179 | pFrag, pFrag->len, pFrag->data_len, skb_shinfo(pFrag)->nr_frags, skb_shinfo(pFrag)->frag_list, pFrag->next));
|
---|
1180 | cSegs += 1 + skb_shinfo(pFrag)->nr_frags;
|
---|
1181 | }
|
---|
1182 | #else
|
---|
1183 | unsigned cSegs = 1;
|
---|
1184 | #endif
|
---|
1185 | #ifdef PADD_RUNT_FRAMES_FROM_HOST
|
---|
1186 | /* vboxNetFltLinuxSkBufToSG adds a padding segment if it's a runt. */
|
---|
1187 | if (pBuf->len < 60)
|
---|
1188 | cSegs++;
|
---|
1189 | #endif
|
---|
1190 | return cSegs;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Destroy the intnet scatter / gather buffer created by
|
---|
1196 | * vboxNetFltLinuxSkBufToSG.
|
---|
1197 | *
|
---|
1198 | * @param pSG The (scatter/)gather list.
|
---|
1199 | * @param pBuf The original socket buffer that was used to create
|
---|
1200 | * the scatter/gather list.
|
---|
1201 | */
|
---|
1202 | static void vboxNetFltLinuxDestroySG(PINTNETSG pSG, struct sk_buff *pBuf)
|
---|
1203 | {
|
---|
1204 | #ifdef VBOXNETFLT_SG_SUPPORT
|
---|
1205 | int i, iSeg = 1; /* Skip non-paged part of SKB */
|
---|
1206 | /* Check if the extra buffer behind SG structure was used for modified packet header */
|
---|
1207 | if (pBuf->data != pSG->aSegs[0].pv)
|
---|
1208 | ++iSeg; /* Skip it as well */
|
---|
1209 | # ifdef LOG_ENABLED
|
---|
1210 | if (pBuf->data_len)
|
---|
1211 | Log6(("kunmap_atomic:"));
|
---|
1212 | # endif /* LOG_ENABLED */
|
---|
1213 | /* iSeg now points to the first mapped fragment if there are any */
|
---|
1214 | for (i = 0; i < skb_shinfo(pBuf)->nr_frags; i++)
|
---|
1215 | {
|
---|
1216 | Log6((" %p", pSG->aSegs[iSeg].pv));
|
---|
1217 | VBOX_SKB_KUNMAP_FRAG(pSG->aSegs[iSeg++].pv);
|
---|
1218 | }
|
---|
1219 | struct sk_buff *pFragBuf;
|
---|
1220 | for (pFragBuf = skb_shinfo(pBuf)->frag_list; pFragBuf; pFragBuf = pFragBuf->next)
|
---|
1221 | {
|
---|
1222 | ++iSeg; /* Non-fragment (unmapped) portion of chained SKB */
|
---|
1223 | for (i = 0; i < skb_shinfo(pFragBuf)->nr_frags; i++)
|
---|
1224 | {
|
---|
1225 | Log6((" %p", pSG->aSegs[iSeg].pv));
|
---|
1226 | VBOX_SKB_KUNMAP_FRAG(pSG->aSegs[iSeg++].pv);
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 | # ifdef LOG_ENABLED
|
---|
1230 | if (pBuf->data_len)
|
---|
1231 | Log6(("\n"));
|
---|
1232 | # endif /* LOG_ENABLED */
|
---|
1233 | #endif
|
---|
1234 | NOREF(pSG);
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | #ifdef LOG_ENABLED
|
---|
1238 | /**
|
---|
1239 | * Logging helper.
|
---|
1240 | */
|
---|
1241 | static void vboxNetFltDumpPacket(PINTNETSG pSG, bool fEgress, const char *pszWhere, int iIncrement)
|
---|
1242 | {
|
---|
1243 | int i, offSeg;
|
---|
1244 | uint8_t *pInt, *pExt;
|
---|
1245 | static int iPacketNo = 1;
|
---|
1246 | iPacketNo += iIncrement;
|
---|
1247 | if (fEgress)
|
---|
1248 | {
|
---|
1249 | pExt = pSG->aSegs[0].pv;
|
---|
1250 | pInt = pExt + 6;
|
---|
1251 | }
|
---|
1252 | else
|
---|
1253 | {
|
---|
1254 | pInt = pSG->aSegs[0].pv;
|
---|
1255 | pExt = pInt + 6;
|
---|
1256 | }
|
---|
1257 | Log(("VBoxNetFlt: (int)%02x:%02x:%02x:%02x:%02x:%02x"
|
---|
1258 | " %s (%s)%02x:%02x:%02x:%02x:%02x:%02x (%u bytes) packet #%u\n",
|
---|
1259 | pInt[0], pInt[1], pInt[2], pInt[3], pInt[4], pInt[5],
|
---|
1260 | fEgress ? "-->" : "<--", pszWhere,
|
---|
1261 | pExt[0], pExt[1], pExt[2], pExt[3], pExt[4], pExt[5],
|
---|
1262 | pSG->cbTotal, iPacketNo));
|
---|
1263 | if (pSG->cSegsUsed == 1)
|
---|
1264 | {
|
---|
1265 | Log4(("%.*Rhxd\n", pSG->aSegs[0].cb, pSG->aSegs[0].pv));
|
---|
1266 | }
|
---|
1267 | else
|
---|
1268 | {
|
---|
1269 | for (i = 0, offSeg = 0; i < pSG->cSegsUsed; i++)
|
---|
1270 | {
|
---|
1271 | Log4(("-- segment %d at 0x%x (%d bytes)\n --\n%.*Rhxd\n",
|
---|
1272 | i, offSeg, pSG->aSegs[i].cb, pSG->aSegs[i].cb, pSG->aSegs[i].pv));
|
---|
1273 | offSeg += pSG->aSegs[i].cb;
|
---|
1274 | }
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 | #else
|
---|
1278 | # define vboxNetFltDumpPacket(a, b, c, d) do {} while (0)
|
---|
1279 | #endif
|
---|
1280 |
|
---|
1281 | #ifdef VBOXNETFLT_WITH_GSO_RECV
|
---|
1282 |
|
---|
1283 | /**
|
---|
1284 | * Worker for vboxNetFltLinuxForwardToIntNet that checks if we can forwards a
|
---|
1285 | * GSO socket buffer without having to segment it.
|
---|
1286 | *
|
---|
1287 | * @returns true on success, false if needs segmenting.
|
---|
1288 | * @param pThis The net filter instance.
|
---|
1289 | * @param pSkb The GSO socket buffer.
|
---|
1290 | * @param fSrc The source.
|
---|
1291 | * @param pGsoCtx Where to return the GSO context on success.
|
---|
1292 | */
|
---|
1293 | static bool vboxNetFltLinuxCanForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc,
|
---|
1294 | PPDMNETWORKGSO pGsoCtx)
|
---|
1295 | {
|
---|
1296 | PDMNETWORKGSOTYPE enmGsoType;
|
---|
1297 | uint16_t uEtherType;
|
---|
1298 | unsigned int cbTransport;
|
---|
1299 | unsigned int offTransport;
|
---|
1300 | unsigned int cbTransportHdr;
|
---|
1301 | unsigned uProtocol;
|
---|
1302 | union
|
---|
1303 | {
|
---|
1304 | RTNETIPV4 IPv4;
|
---|
1305 | RTNETIPV6 IPv6;
|
---|
1306 | RTNETTCP Tcp;
|
---|
1307 | uint8_t ab[40];
|
---|
1308 | uint16_t au16[40/2];
|
---|
1309 | uint32_t au32[40/4];
|
---|
1310 | } Buf;
|
---|
1311 |
|
---|
1312 | /*
|
---|
1313 | * Check the GSO properties of the socket buffer and make sure it fits.
|
---|
1314 | */
|
---|
1315 | /** @todo Figure out how to handle SKB_GSO_TCP_ECN! */
|
---|
1316 | if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_type & ~(SKB_GSO_DODGY | SKB_GSO_TCPV6 | SKB_GSO_TCPV4) ))
|
---|
1317 | {
|
---|
1318 | Log5(("vboxNetFltLinuxCanForwardAsGso: gso_type=%#x\n", skb_shinfo(pSkb)->gso_type));
|
---|
1319 | return false;
|
---|
1320 | }
|
---|
1321 | if (RT_UNLIKELY( skb_shinfo(pSkb)->gso_size < 1
|
---|
1322 | || pSkb->len > VBOX_MAX_GSO_SIZE ))
|
---|
1323 | {
|
---|
1324 | Log5(("vboxNetFltLinuxCanForwardAsGso: gso_size=%#x skb_len=%#x (max=%#x)\n", skb_shinfo(pSkb)->gso_size, pSkb->len, VBOX_MAX_GSO_SIZE));
|
---|
1325 | return false;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | /*
|
---|
1329 | * Switch on the ethertype.
|
---|
1330 | */
|
---|
1331 | uEtherType = pSkb->protocol;
|
---|
1332 | if ( uEtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_VLAN)
|
---|
1333 | && pSkb->mac_len == sizeof(RTNETETHERHDR) + sizeof(uint32_t))
|
---|
1334 | {
|
---|
1335 | uint16_t const *puEtherType = skb_header_pointer(pSkb, sizeof(RTNETETHERHDR) + sizeof(uint16_t), sizeof(uint16_t), &Buf);
|
---|
1336 | if (puEtherType)
|
---|
1337 | uEtherType = *puEtherType;
|
---|
1338 | }
|
---|
1339 | switch (uEtherType)
|
---|
1340 | {
|
---|
1341 | case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4):
|
---|
1342 | {
|
---|
1343 | unsigned int cbHdr;
|
---|
1344 | PCRTNETIPV4 pIPv4 = (PCRTNETIPV4)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv4), &Buf);
|
---|
1345 | if (RT_UNLIKELY(!pIPv4))
|
---|
1346 | {
|
---|
1347 | Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv4 hdr\n"));
|
---|
1348 | return false;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | cbHdr = pIPv4->ip_hl * 4;
|
---|
1352 | cbTransport = RT_N2H_U16(pIPv4->ip_len);
|
---|
1353 | if (RT_UNLIKELY( cbHdr < RTNETIPV4_MIN_LEN
|
---|
1354 | || cbHdr > cbTransport ))
|
---|
1355 | {
|
---|
1356 | Log5(("vboxNetFltLinuxCanForwardAsGso: invalid IPv4 lengths: ip_hl=%u ip_len=%u\n", pIPv4->ip_hl, RT_N2H_U16(pIPv4->ip_len)));
|
---|
1357 | return false;
|
---|
1358 | }
|
---|
1359 | cbTransport -= cbHdr;
|
---|
1360 | offTransport = pSkb->mac_len + cbHdr;
|
---|
1361 | uProtocol = pIPv4->ip_p;
|
---|
1362 | if (uProtocol == RTNETIPV4_PROT_TCP)
|
---|
1363 | enmGsoType = PDMNETWORKGSOTYPE_IPV4_TCP;
|
---|
1364 | else if (uProtocol == RTNETIPV4_PROT_UDP)
|
---|
1365 | enmGsoType = PDMNETWORKGSOTYPE_IPV4_UDP;
|
---|
1366 | else /** @todo IPv6: 4to6 tunneling */
|
---|
1367 | enmGsoType = PDMNETWORKGSOTYPE_INVALID;
|
---|
1368 | break;
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | case RT_H2N_U16_C(RTNET_ETHERTYPE_IPV6):
|
---|
1372 | {
|
---|
1373 | PCRTNETIPV6 pIPv6 = (PCRTNETIPV6)skb_header_pointer(pSkb, pSkb->mac_len, sizeof(Buf.IPv6), &Buf);
|
---|
1374 | if (RT_UNLIKELY(!pIPv6))
|
---|
1375 | {
|
---|
1376 | Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access IPv6 hdr\n"));
|
---|
1377 | return false;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | cbTransport = RT_N2H_U16(pIPv6->ip6_plen);
|
---|
1381 | offTransport = pSkb->mac_len + sizeof(RTNETIPV6);
|
---|
1382 | uProtocol = pIPv6->ip6_nxt;
|
---|
1383 | /** @todo IPv6: Dig our way out of the other headers. */
|
---|
1384 | if (uProtocol == RTNETIPV4_PROT_TCP)
|
---|
1385 | enmGsoType = PDMNETWORKGSOTYPE_IPV6_TCP;
|
---|
1386 | else if (uProtocol == RTNETIPV4_PROT_UDP)
|
---|
1387 | enmGsoType = PDMNETWORKGSOTYPE_IPV6_UDP;
|
---|
1388 | else
|
---|
1389 | enmGsoType = PDMNETWORKGSOTYPE_INVALID;
|
---|
1390 | break;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | default:
|
---|
1394 | Log5(("vboxNetFltLinuxCanForwardAsGso: uEtherType=%#x\n", RT_H2N_U16(uEtherType)));
|
---|
1395 | return false;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | if (enmGsoType == PDMNETWORKGSOTYPE_INVALID)
|
---|
1399 | {
|
---|
1400 | Log5(("vboxNetFltLinuxCanForwardAsGso: Unsupported protocol %d\n", uProtocol));
|
---|
1401 | return false;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | if (RT_UNLIKELY( offTransport + cbTransport <= offTransport
|
---|
1405 | || offTransport + cbTransport > pSkb->len
|
---|
1406 | || cbTransport < (uProtocol == RTNETIPV4_PROT_TCP ? RTNETTCP_MIN_LEN : RTNETUDP_MIN_LEN)) )
|
---|
1407 | {
|
---|
1408 | Log5(("vboxNetFltLinuxCanForwardAsGso: Bad transport length; off=%#x + cb=%#x => %#x; skb_len=%#x (%s)\n",
|
---|
1409 | offTransport, cbTransport, offTransport + cbTransport, pSkb->len, PDMNetGsoTypeName(enmGsoType) ));
|
---|
1410 | return false;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /*
|
---|
1414 | * Check the TCP/UDP bits.
|
---|
1415 | */
|
---|
1416 | if (uProtocol == RTNETIPV4_PROT_TCP)
|
---|
1417 | {
|
---|
1418 | PCRTNETTCP pTcp = (PCRTNETTCP)skb_header_pointer(pSkb, offTransport, sizeof(Buf.Tcp), &Buf);
|
---|
1419 | if (RT_UNLIKELY(!pTcp))
|
---|
1420 | {
|
---|
1421 | Log5(("vboxNetFltLinuxCanForwardAsGso: failed to access TCP hdr\n"));
|
---|
1422 | return false;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | cbTransportHdr = pTcp->th_off * 4;
|
---|
1426 | pGsoCtx->cbHdrsSeg = offTransport + cbTransportHdr;
|
---|
1427 | if (RT_UNLIKELY( cbTransportHdr < RTNETTCP_MIN_LEN
|
---|
1428 | || cbTransportHdr > cbTransport
|
---|
1429 | || offTransport + cbTransportHdr >= UINT8_MAX
|
---|
1430 | || offTransport + cbTransportHdr >= pSkb->len ))
|
---|
1431 | {
|
---|
1432 | Log5(("vboxNetFltLinuxCanForwardAsGso: No space for TCP header; off=%#x cb=%#x skb_len=%#x\n", offTransport, cbTransportHdr, pSkb->len));
|
---|
1433 | return false;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | }
|
---|
1437 | else
|
---|
1438 | {
|
---|
1439 | Assert(uProtocol == RTNETIPV4_PROT_UDP);
|
---|
1440 | cbTransportHdr = sizeof(RTNETUDP);
|
---|
1441 | pGsoCtx->cbHdrsSeg = offTransport; /* Exclude UDP header */
|
---|
1442 | if (RT_UNLIKELY( offTransport + cbTransportHdr >= UINT8_MAX
|
---|
1443 | || offTransport + cbTransportHdr >= pSkb->len ))
|
---|
1444 | {
|
---|
1445 | Log5(("vboxNetFltLinuxCanForwardAsGso: No space for UDP header; off=%#x skb_len=%#x\n", offTransport, pSkb->len));
|
---|
1446 | return false;
|
---|
1447 | }
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | /*
|
---|
1451 | * We're good, init the GSO context.
|
---|
1452 | */
|
---|
1453 | pGsoCtx->u8Type = enmGsoType;
|
---|
1454 | pGsoCtx->cbHdrsTotal = offTransport + cbTransportHdr;
|
---|
1455 | pGsoCtx->cbMaxSeg = skb_shinfo(pSkb)->gso_size;
|
---|
1456 | pGsoCtx->offHdr1 = pSkb->mac_len;
|
---|
1457 | pGsoCtx->offHdr2 = offTransport;
|
---|
1458 | pGsoCtx->u8Unused = 0;
|
---|
1459 |
|
---|
1460 | return true;
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /**
|
---|
1464 | * Forward the socket buffer as a GSO internal network frame.
|
---|
1465 | *
|
---|
1466 | * @returns IPRT status code.
|
---|
1467 | * @param pThis The net filter instance.
|
---|
1468 | * @param pSkb The GSO socket buffer.
|
---|
1469 | * @param fSrc The source.
|
---|
1470 | * @param pGsoCtx Where to return the GSO context on success.
|
---|
1471 | */
|
---|
1472 | static int vboxNetFltLinuxForwardAsGso(PVBOXNETFLTINS pThis, struct sk_buff *pSkb, uint32_t fSrc, PCPDMNETWORKGSO pGsoCtx)
|
---|
1473 | {
|
---|
1474 | int rc;
|
---|
1475 | unsigned cbExtra;
|
---|
1476 | unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pSkb, &cbExtra);
|
---|
1477 | PINTNETSG pSG = (PINTNETSG)alloca(RT_UOFFSETOF_DYN(INTNETSG, aSegs[cSegs]) + cbExtra);
|
---|
1478 | if (RT_LIKELY(pSG))
|
---|
1479 | {
|
---|
1480 | vboxNetFltLinuxSkBufToSG(pThis, pSkb, pSG, cbExtra, cSegs, fSrc, pGsoCtx);
|
---|
1481 |
|
---|
1482 | vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
|
---|
1483 | pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
|
---|
1484 |
|
---|
1485 | vboxNetFltLinuxDestroySG(pSG, pSkb);
|
---|
1486 | rc = VINF_SUCCESS;
|
---|
1487 | }
|
---|
1488 | else
|
---|
1489 | {
|
---|
1490 | Log(("VBoxNetFlt: Dropping the sk_buff (failure case).\n"));
|
---|
1491 | rc = VERR_NO_MEMORY;
|
---|
1492 | }
|
---|
1493 | return rc;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | #endif /* VBOXNETFLT_WITH_GSO_RECV */
|
---|
1497 |
|
---|
1498 | /**
|
---|
1499 | * Worker for vboxNetFltLinuxForwardToIntNet.
|
---|
1500 | *
|
---|
1501 | * @returns VINF_SUCCESS or VERR_NO_MEMORY.
|
---|
1502 | * @param pThis The net filter instance.
|
---|
1503 | * @param pBuf The socket buffer.
|
---|
1504 | * @param fSrc The source.
|
---|
1505 | */
|
---|
1506 | static int vboxNetFltLinuxForwardSegment(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, uint32_t fSrc)
|
---|
1507 | {
|
---|
1508 | int rc;
|
---|
1509 | unsigned cbExtra;
|
---|
1510 | unsigned cSegs = vboxNetFltLinuxCalcSGSegments(pBuf, &cbExtra);
|
---|
1511 | PINTNETSG pSG = (PINTNETSG)alloca(RT_UOFFSETOF_DYN(INTNETSG, aSegs[cSegs]) + cbExtra);
|
---|
1512 | if (RT_LIKELY(pSG))
|
---|
1513 | {
|
---|
1514 | vboxNetFltLinuxSkBufToSG(pThis, pBuf, pSG, cbExtra, cSegs, fSrc, NULL /*pGsoCtx*/);
|
---|
1515 |
|
---|
1516 | vboxNetFltDumpPacket(pSG, false, (fSrc & INTNETTRUNKDIR_HOST) ? "host" : "wire", 1);
|
---|
1517 | pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
|
---|
1518 |
|
---|
1519 | vboxNetFltLinuxDestroySG(pSG, pBuf);
|
---|
1520 | rc = VINF_SUCCESS;
|
---|
1521 | }
|
---|
1522 | else
|
---|
1523 | {
|
---|
1524 | Log(("VBoxNetFlt: Failed to allocate SG buffer.\n"));
|
---|
1525 | rc = VERR_NO_MEMORY;
|
---|
1526 | }
|
---|
1527 | return rc;
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 |
|
---|
1531 | /**
|
---|
1532 | * I won't disclose what I do, figure it out yourself, including pThis referencing.
|
---|
1533 | *
|
---|
1534 | * @param pThis The net filter instance.
|
---|
1535 | * @param pBuf The socket buffer.
|
---|
1536 | * @param fSrc Where the packet comes from.
|
---|
1537 | */
|
---|
1538 | static void vboxNetFltLinuxForwardToIntNetInner(PVBOXNETFLTINS pThis, struct sk_buff *pBuf, uint32_t fSrc)
|
---|
1539 | {
|
---|
1540 | #ifdef VBOXNETFLT_WITH_GSO
|
---|
1541 | if (skb_is_gso(pBuf))
|
---|
1542 | {
|
---|
1543 | PDMNETWORKGSO GsoCtx;
|
---|
1544 | Log6(("vboxNetFltLinuxForwardToIntNetInner: skb len=%u data_len=%u truesize=%u next=%p"
|
---|
1545 | " nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x ip_summed=%d\n",
|
---|
1546 | pBuf->len, pBuf->data_len, pBuf->truesize, pBuf->next,
|
---|
1547 | skb_shinfo(pBuf)->nr_frags, skb_shinfo(pBuf)->gso_size,
|
---|
1548 | skb_shinfo(pBuf)->gso_segs, skb_shinfo(pBuf)->gso_type,
|
---|
1549 | skb_shinfo(pBuf)->frag_list, pBuf->pkt_type, pBuf->ip_summed));
|
---|
1550 | #ifndef VBOXNETFLT_SG_SUPPORT
|
---|
1551 | if (RT_LIKELY(fSrc & INTNETTRUNKDIR_HOST))
|
---|
1552 | {
|
---|
1553 | /*
|
---|
1554 | * skb_gso_segment does the following. Do we need to do it as well?
|
---|
1555 | */
|
---|
1556 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
1557 | skb_reset_mac_header(pBuf);
|
---|
1558 | pBuf->mac_len = pBuf->network_header - pBuf->mac_header;
|
---|
1559 | # else
|
---|
1560 | pBuf->mac.raw = pBuf->data;
|
---|
1561 | pBuf->mac_len = pBuf->nh.raw - pBuf->data;
|
---|
1562 | # endif
|
---|
1563 | }
|
---|
1564 | #endif /* !VBOXNETFLT_SG_SUPPORT */
|
---|
1565 | # ifdef VBOXNETFLT_WITH_GSO_RECV
|
---|
1566 | if ( (skb_shinfo(pBuf)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4))
|
---|
1567 | && vboxNetFltLinuxCanForwardAsGso(pThis, pBuf, fSrc, &GsoCtx) )
|
---|
1568 | vboxNetFltLinuxForwardAsGso(pThis, pBuf, fSrc, &GsoCtx);
|
---|
1569 | else
|
---|
1570 | # endif /* VBOXNETFLT_WITH_GSO_RECV */
|
---|
1571 | {
|
---|
1572 | /* Need to segment the packet */
|
---|
1573 | struct sk_buff *pNext;
|
---|
1574 | struct sk_buff *pSegment = skb_gso_segment(pBuf, 0 /*supported features*/);
|
---|
1575 | if (IS_ERR(pSegment))
|
---|
1576 | {
|
---|
1577 | LogRel(("VBoxNetFlt: Failed to segment a packet (%d).\n", PTR_ERR(pSegment)));
|
---|
1578 | return;
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | for (; pSegment; pSegment = pNext)
|
---|
1582 | {
|
---|
1583 | Log6(("vboxNetFltLinuxForwardToIntNetInner: segment len=%u data_len=%u truesize=%u next=%p"
|
---|
1584 | " nr_frags=%u gso_size=%u gso_seqs=%u gso_type=%x frag_list=%p pkt_type=%x\n",
|
---|
1585 | pSegment->len, pSegment->data_len, pSegment->truesize, pSegment->next,
|
---|
1586 | skb_shinfo(pSegment)->nr_frags, skb_shinfo(pSegment)->gso_size,
|
---|
1587 | skb_shinfo(pSegment)->gso_segs, skb_shinfo(pSegment)->gso_type,
|
---|
1588 | skb_shinfo(pSegment)->frag_list, pSegment->pkt_type));
|
---|
1589 | pNext = pSegment->next;
|
---|
1590 | pSegment->next = 0;
|
---|
1591 | vboxNetFltLinuxForwardSegment(pThis, pSegment, fSrc);
|
---|
1592 | dev_kfree_skb(pSegment);
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 | }
|
---|
1596 | else
|
---|
1597 | #endif /* VBOXNETFLT_WITH_GSO */
|
---|
1598 | {
|
---|
1599 | Log6(("vboxNetFltLinuxForwardToIntNetInner: ptk_type=%d ip_summed=%d len=%d"
|
---|
1600 | " data_len=%d headroom=%d hdr_len=%d csum_offset=%d\n",
|
---|
1601 | pBuf->pkt_type, pBuf->ip_summed, pBuf->len, pBuf->data_len, skb_headroom(pBuf),
|
---|
1602 | skb_headlen(pBuf), vboxNetFltLinuxGetChecksumStartOffset(pBuf)));
|
---|
1603 | #ifndef VBOXNETFLT_SG_SUPPORT
|
---|
1604 | if (pBuf->ip_summed == CHECKSUM_PARTIAL && pBuf->pkt_type == PACKET_OUTGOING)
|
---|
1605 | {
|
---|
1606 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
|
---|
1607 | /*
|
---|
1608 | * Try to work around the problem with CentOS 4.7 and 5.2 (2.6.9
|
---|
1609 | * and 2.6.18 kernels), they pass wrong 'h' pointer down. We take IP
|
---|
1610 | * header length from the header itself and reconstruct 'h' pointer
|
---|
1611 | * to TCP (or whatever) header.
|
---|
1612 | */
|
---|
1613 | unsigned char *tmp = pBuf->h.raw;
|
---|
1614 | if (pBuf->h.raw == pBuf->nh.raw && pBuf->protocol == htons(ETH_P_IP))
|
---|
1615 | pBuf->h.raw = pBuf->nh.raw + pBuf->nh.iph->ihl * 4;
|
---|
1616 | #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
|
---|
1617 | int rc = VBOX_SKB_CHECKSUM_HELP(pBuf);
|
---|
1618 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
|
---|
1619 | /* Restore the original (wrong) pointer. */
|
---|
1620 | pBuf->h.raw = tmp;
|
---|
1621 | #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) */
|
---|
1622 | if (rc) {
|
---|
1623 | LogRel(("VBoxNetFlt: Failed to compute checksum, dropping the packet.\n"));
|
---|
1624 | return;
|
---|
1625 | }
|
---|
1626 | }
|
---|
1627 | #endif /* !VBOXNETFLT_SG_SUPPORT */
|
---|
1628 | vboxNetFltLinuxForwardSegment(pThis, pBuf, fSrc);
|
---|
1629 | }
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 |
|
---|
1633 | /**
|
---|
1634 | * Temporarily adjust pBuf->data so it always points to the Ethernet header,
|
---|
1635 | * then forward it to the internal network.
|
---|
1636 | *
|
---|
1637 | * @param pThis The net filter instance.
|
---|
1638 | * @param pBuf The socket buffer. This is consumed by this function.
|
---|
1639 | */
|
---|
1640 | static void vboxNetFltLinuxForwardToIntNet(PVBOXNETFLTINS pThis, struct sk_buff *pBuf)
|
---|
1641 | {
|
---|
1642 | uint32_t fSrc = pBuf->pkt_type == PACKET_OUTGOING ? INTNETTRUNKDIR_HOST : INTNETTRUNKDIR_WIRE;
|
---|
1643 |
|
---|
1644 | if (RT_UNLIKELY(fSrc & INTNETTRUNKDIR_WIRE))
|
---|
1645 | {
|
---|
1646 | /*
|
---|
1647 | * The packet came from the wire and the driver has already consumed
|
---|
1648 | * mac header. We need to restore it back. Moreover, after we are
|
---|
1649 | * through with this skb we need to restore its original state!
|
---|
1650 | */
|
---|
1651 | skb_push(pBuf, pBuf->mac_len);
|
---|
1652 | Log5(("vboxNetFltLinuxForwardToIntNet: mac_len=%d data=%p mac_header=%p network_header=%p\n",
|
---|
1653 | pBuf->mac_len, pBuf->data, skb_mac_header(pBuf), skb_network_header(pBuf)));
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | vboxNetFltLinuxForwardToIntNetInner(pThis, pBuf, fSrc);
|
---|
1657 |
|
---|
1658 | /*
|
---|
1659 | * Restore the original state of skb as there are other handlers this skb
|
---|
1660 | * will be provided to.
|
---|
1661 | */
|
---|
1662 | if (RT_UNLIKELY(fSrc & INTNETTRUNKDIR_WIRE))
|
---|
1663 | skb_pull(pBuf, pBuf->mac_len);
|
---|
1664 |
|
---|
1665 | dev_kfree_skb(pBuf);
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 |
|
---|
1669 | #ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
1670 | /**
|
---|
1671 | * Work queue handler that forwards the socket buffers queued by
|
---|
1672 | * vboxNetFltLinuxPacketHandler to the internal network.
|
---|
1673 | *
|
---|
1674 | * @param pWork The work queue.
|
---|
1675 | */
|
---|
1676 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
|
---|
1677 | static void vboxNetFltLinuxXmitTask(struct work_struct *pWork)
|
---|
1678 | # else
|
---|
1679 | static void vboxNetFltLinuxXmitTask(void *pWork)
|
---|
1680 | # endif
|
---|
1681 | {
|
---|
1682 | PVBOXNETFLTINS pThis = VBOX_FLT_XT_TO_INST(pWork);
|
---|
1683 | struct sk_buff *pBuf;
|
---|
1684 |
|
---|
1685 | Log6(("vboxNetFltLinuxXmitTask: Got work %p.\n", pWork));
|
---|
1686 |
|
---|
1687 | /*
|
---|
1688 | * Active? Retain the instance and increment the busy counter.
|
---|
1689 | */
|
---|
1690 | if (vboxNetFltTryRetainBusyActive(pThis))
|
---|
1691 | {
|
---|
1692 | while ((pBuf = skb_dequeue(&pThis->u.s.XmitQueue)) != NULL)
|
---|
1693 | vboxNetFltLinuxForwardToIntNet(pThis, pBuf);
|
---|
1694 |
|
---|
1695 | vboxNetFltRelease(pThis, true /* fBusy */);
|
---|
1696 | }
|
---|
1697 | else
|
---|
1698 | {
|
---|
1699 | /** @todo Shouldn't we just drop the packets here? There is little point in
|
---|
1700 | * making them accumulate when the VM is paused and it'll only waste
|
---|
1701 | * kernel memory anyway... Hmm. maybe wait a short while (2-5 secs)
|
---|
1702 | * before start draining the packets (goes for the intnet ring buf
|
---|
1703 | * too)? */
|
---|
1704 | }
|
---|
1705 | }
|
---|
1706 | #endif /* !VBOXNETFLT_LINUX_NO_XMIT_QUEUE */
|
---|
1707 |
|
---|
1708 | /**
|
---|
1709 | * Reports the GSO capabilities of the hardware NIC.
|
---|
1710 | *
|
---|
1711 | * @param pThis The net filter instance. The caller hold a
|
---|
1712 | * reference to this.
|
---|
1713 | */
|
---|
1714 | static void vboxNetFltLinuxReportNicGsoCapabilities(PVBOXNETFLTINS pThis)
|
---|
1715 | {
|
---|
1716 | #ifdef VBOXNETFLT_WITH_GSO_XMIT_WIRE
|
---|
1717 | if (vboxNetFltTryRetainBusyNotDisconnected(pThis))
|
---|
1718 | {
|
---|
1719 | struct net_device *pDev;
|
---|
1720 | PINTNETTRUNKSWPORT pSwitchPort;
|
---|
1721 | unsigned int fFeatures;
|
---|
1722 |
|
---|
1723 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
1724 |
|
---|
1725 | pSwitchPort = pThis->pSwitchPort; /* this doesn't need to be here, but it doesn't harm. */
|
---|
1726 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
1727 | if (pDev)
|
---|
1728 | fFeatures = pDev->features;
|
---|
1729 | else
|
---|
1730 | fFeatures = 0;
|
---|
1731 |
|
---|
1732 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
1733 |
|
---|
1734 | if (pThis->pSwitchPort)
|
---|
1735 | {
|
---|
1736 | /* Set/update the GSO capabilities of the NIC. */
|
---|
1737 | uint32_t fGsoCapabilites = 0;
|
---|
1738 | if (fFeatures & NETIF_F_TSO)
|
---|
1739 | fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP);
|
---|
1740 | if (fFeatures & NETIF_F_TSO6)
|
---|
1741 | fGsoCapabilites |= RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP);
|
---|
1742 | Log3(("vboxNetFltLinuxReportNicGsoCapabilities: reporting wire %s%s%s%s\n",
|
---|
1743 | (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP)) ? "tso " : "",
|
---|
1744 | (fGsoCapabilites & RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP)) ? "tso6 " : ""));
|
---|
1745 | pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, fGsoCapabilites, INTNETTRUNKDIR_WIRE);
|
---|
1746 | }
|
---|
1747 |
|
---|
1748 | vboxNetFltRelease(pThis, true /*fBusy*/);
|
---|
1749 | }
|
---|
1750 | #endif /* VBOXNETFLT_WITH_GSO_XMIT_WIRE */
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | /**
|
---|
1754 | * Helper that determines whether the host (ignoreing us) is operating the
|
---|
1755 | * interface in promiscuous mode or not.
|
---|
1756 | */
|
---|
1757 | static bool vboxNetFltLinuxPromiscuous(PVBOXNETFLTINS pThis)
|
---|
1758 | {
|
---|
1759 | bool fRc = false;
|
---|
1760 | struct net_device * pDev = vboxNetFltLinuxRetainNetDev(pThis);
|
---|
1761 | if (pDev)
|
---|
1762 | {
|
---|
1763 | fRc = !!(pDev->promiscuity - (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet) & 1));
|
---|
1764 | LogFlow(("vboxNetFltPortOsIsPromiscuous: returns %d, pDev->promiscuity=%d, fPromiscuousSet=%d\n",
|
---|
1765 | fRc, pDev->promiscuity, pThis->u.s.fPromiscuousSet));
|
---|
1766 | vboxNetFltLinuxReleaseNetDev(pThis, pDev);
|
---|
1767 | }
|
---|
1768 | return fRc;
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | /**
|
---|
1772 | * Does this device needs link state change signaled?
|
---|
1773 | * Currently we need it for our own VBoxNetAdp and TAP.
|
---|
1774 | */
|
---|
1775 | static bool vboxNetFltNeedsLinkState(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
1776 | {
|
---|
1777 | if (pDev->ethtool_ops && pDev->ethtool_ops->get_drvinfo)
|
---|
1778 | {
|
---|
1779 | struct ethtool_drvinfo Info;
|
---|
1780 |
|
---|
1781 | memset(&Info, 0, sizeof(Info));
|
---|
1782 | Info.cmd = ETHTOOL_GDRVINFO;
|
---|
1783 | pDev->ethtool_ops->get_drvinfo(pDev, &Info);
|
---|
1784 | Log3(("%s: driver=%.*s version=%.*s bus_info=%.*s\n",
|
---|
1785 | __FUNCTION__,
|
---|
1786 | sizeof(Info.driver), Info.driver,
|
---|
1787 | sizeof(Info.version), Info.version,
|
---|
1788 | sizeof(Info.bus_info), Info.bus_info));
|
---|
1789 |
|
---|
1790 | if (!strncmp(Info.driver, "vboxnet", sizeof(Info.driver)))
|
---|
1791 | return true;
|
---|
1792 |
|
---|
1793 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) /* TAP started doing carrier */
|
---|
1794 | return !strncmp(Info.driver, "tun", 4)
|
---|
1795 | && !strncmp(Info.bus_info, "tap", 4);
|
---|
1796 | #endif
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | return false;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
|
---|
1803 | DECLINLINE(void) netif_tx_lock_bh(struct net_device *pDev)
|
---|
1804 | {
|
---|
1805 | spin_lock_bh(&pDev->xmit_lock);
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | DECLINLINE(void) netif_tx_unlock_bh(struct net_device *pDev)
|
---|
1809 | {
|
---|
1810 | spin_unlock_bh(&pDev->xmit_lock);
|
---|
1811 | }
|
---|
1812 | #endif
|
---|
1813 |
|
---|
1814 | /**
|
---|
1815 | * Some devices need link state change when filter attaches/detaches
|
---|
1816 | * since the filter is their link in a sense.
|
---|
1817 | */
|
---|
1818 | static void vboxNetFltSetLinkState(PVBOXNETFLTINS pThis, struct net_device *pDev, bool fLinkUp)
|
---|
1819 | {
|
---|
1820 | if (vboxNetFltNeedsLinkState(pThis, pDev))
|
---|
1821 | {
|
---|
1822 | Log3(("%s: bringing device link %s\n",
|
---|
1823 | __FUNCTION__, fLinkUp ? "up" : "down"));
|
---|
1824 | netif_tx_lock_bh(pDev);
|
---|
1825 | if (fLinkUp)
|
---|
1826 | netif_carrier_on(pDev);
|
---|
1827 | else
|
---|
1828 | netif_carrier_off(pDev);
|
---|
1829 | netif_tx_unlock_bh(pDev);
|
---|
1830 | }
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | /**
|
---|
1834 | * Internal worker for vboxNetFltLinuxNotifierCallback.
|
---|
1835 | *
|
---|
1836 | * @returns VBox status code.
|
---|
1837 | * @param pThis The instance.
|
---|
1838 | * @param pDev The device to attach to.
|
---|
1839 | */
|
---|
1840 | static int vboxNetFltLinuxAttachToInterface(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
1841 | {
|
---|
1842 | LogFlow(("vboxNetFltLinuxAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
1843 |
|
---|
1844 | /*
|
---|
1845 | * Retain and store the device.
|
---|
1846 | */
|
---|
1847 | dev_hold(pDev);
|
---|
1848 |
|
---|
1849 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
1850 | ASMAtomicUoWritePtr(&pThis->u.s.pDev, pDev);
|
---|
1851 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
1852 |
|
---|
1853 | Log(("vboxNetFltLinuxAttachToInterface: Device %p(%s) retained. ref=%d\n",
|
---|
1854 | pDev, pDev->name,
|
---|
1855 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
1856 | netdev_refcnt_read(pDev)
|
---|
1857 | #else
|
---|
1858 | atomic_read(&pDev->refcnt)
|
---|
1859 | #endif
|
---|
1860 | ));
|
---|
1861 | Log(("vboxNetFltLinuxAttachToInterface: Got pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
|
---|
1862 | pDev, pThis, ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *)));
|
---|
1863 |
|
---|
1864 | /* Get the mac address while we still have a valid net_device reference. */
|
---|
1865 | memcpy(&pThis->u.s.MacAddr, pDev->dev_addr, sizeof(pThis->u.s.MacAddr));
|
---|
1866 | /* Initialize MTU */
|
---|
1867 | pThis->u.s.cbMtu = pDev->mtu;
|
---|
1868 |
|
---|
1869 | /*
|
---|
1870 | * Install a packet filter for this device with a protocol wildcard (ETH_P_ALL).
|
---|
1871 | */
|
---|
1872 | pThis->u.s.PacketType.type = __constant_htons(ETH_P_ALL);
|
---|
1873 | pThis->u.s.PacketType.dev = pDev;
|
---|
1874 | pThis->u.s.PacketType.func = vboxNetFltLinuxPacketHandler;
|
---|
1875 | dev_add_pack(&pThis->u.s.PacketType);
|
---|
1876 | ASMAtomicUoWriteBool(&pThis->u.s.fPacketHandler, true);
|
---|
1877 | Log(("vboxNetFltLinuxAttachToInterface: this=%p: Packet handler installed.\n", pThis));
|
---|
1878 |
|
---|
1879 | #ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
|
---|
1880 | vboxNetFltLinuxHookDev(pThis, pDev);
|
---|
1881 | #endif
|
---|
1882 |
|
---|
1883 | /*
|
---|
1884 | * Are we the "carrier" for this device (e.g. vboxnet or tap)?
|
---|
1885 | */
|
---|
1886 | vboxNetFltSetLinkState(pThis, pDev, true);
|
---|
1887 |
|
---|
1888 | /*
|
---|
1889 | * Set indicators that require the spinlock. Be abit paranoid about racing
|
---|
1890 | * the device notification handle.
|
---|
1891 | */
|
---|
1892 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
1893 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
1894 | if (pDev)
|
---|
1895 | {
|
---|
1896 | ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
|
---|
1897 | ASMAtomicUoWriteBool(&pThis->u.s.fRegistered, true);
|
---|
1898 | pDev = NULL; /* don't dereference it */
|
---|
1899 | }
|
---|
1900 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
1901 |
|
---|
1902 | /*
|
---|
1903 | * Report GSO capabilities
|
---|
1904 | */
|
---|
1905 | Assert(pThis->pSwitchPort);
|
---|
1906 | if (vboxNetFltTryRetainBusyNotDisconnected(pThis))
|
---|
1907 | {
|
---|
1908 | vboxNetFltLinuxReportNicGsoCapabilities(pThis);
|
---|
1909 | pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
|
---|
1910 | pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltLinuxPromiscuous(pThis));
|
---|
1911 | pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
|
---|
1912 | vboxNetFltRelease(pThis, true /*fBusy*/);
|
---|
1913 | }
|
---|
1914 |
|
---|
1915 | LogRel(("VBoxNetFlt: attached to '%s' / %RTmac\n", pThis->szName, &pThis->u.s.MacAddr));
|
---|
1916 | return VINF_SUCCESS;
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 |
|
---|
1920 | static int vboxNetFltLinuxUnregisterDevice(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
1921 | {
|
---|
1922 | bool fRegistered;
|
---|
1923 | Assert(!pThis->fDisconnectedFromHost);
|
---|
1924 |
|
---|
1925 | #ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
|
---|
1926 | vboxNetFltLinuxUnhookDev(pThis, pDev);
|
---|
1927 | #endif
|
---|
1928 |
|
---|
1929 | if (ASMAtomicCmpXchgBool(&pThis->u.s.fPacketHandler, false, true))
|
---|
1930 | {
|
---|
1931 | dev_remove_pack(&pThis->u.s.PacketType);
|
---|
1932 | Log(("vboxNetFltLinuxUnregisterDevice: this=%p: packet handler removed.\n", pThis));
|
---|
1933 | }
|
---|
1934 |
|
---|
1935 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
1936 | fRegistered = ASMAtomicXchgBool(&pThis->u.s.fRegistered, false);
|
---|
1937 | if (fRegistered)
|
---|
1938 | {
|
---|
1939 | ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
|
---|
1940 | ASMAtomicUoWriteNullPtr(&pThis->u.s.pDev);
|
---|
1941 | }
|
---|
1942 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
1943 |
|
---|
1944 | if (fRegistered)
|
---|
1945 | {
|
---|
1946 | #ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
1947 | skb_queue_purge(&pThis->u.s.XmitQueue);
|
---|
1948 | #endif
|
---|
1949 | Log(("vboxNetFltLinuxUnregisterDevice: this=%p: xmit queue purged.\n", pThis));
|
---|
1950 | Log(("vboxNetFltLinuxUnregisterDevice: Device %p(%s) released. ref=%d\n",
|
---|
1951 | pDev, pDev->name,
|
---|
1952 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
1953 | netdev_refcnt_read(pDev)
|
---|
1954 | #else
|
---|
1955 | atomic_read(&pDev->refcnt)
|
---|
1956 | #endif
|
---|
1957 | ));
|
---|
1958 | dev_put(pDev);
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | return NOTIFY_OK;
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | static int vboxNetFltLinuxDeviceIsUp(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
1965 | {
|
---|
1966 | /* Check if we are not suspended and promiscuous mode has not been set. */
|
---|
1967 | if ( pThis->enmTrunkState == INTNETTRUNKIFSTATE_ACTIVE
|
---|
1968 | && !ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
|
---|
1969 | {
|
---|
1970 | /* Note that there is no need for locking as the kernel got hold of the lock already. */
|
---|
1971 | dev_set_promiscuity(pDev, 1);
|
---|
1972 | ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, true);
|
---|
1973 | Log(("vboxNetFltLinuxDeviceIsUp: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
1974 | }
|
---|
1975 | else
|
---|
1976 | Log(("vboxNetFltLinuxDeviceIsUp: no need to enable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
1977 | return NOTIFY_OK;
|
---|
1978 | }
|
---|
1979 |
|
---|
1980 | static int vboxNetFltLinuxDeviceGoingDown(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
1981 | {
|
---|
1982 | /* Undo promiscuous mode if we has set it. */
|
---|
1983 | if (ASMAtomicUoReadBool(&pThis->u.s.fPromiscuousSet))
|
---|
1984 | {
|
---|
1985 | /* Note that there is no need for locking as the kernel got hold of the lock already. */
|
---|
1986 | dev_set_promiscuity(pDev, -1);
|
---|
1987 | ASMAtomicWriteBool(&pThis->u.s.fPromiscuousSet, false);
|
---|
1988 | Log(("vboxNetFltLinuxDeviceGoingDown: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
1989 | }
|
---|
1990 | else
|
---|
1991 | Log(("vboxNetFltLinuxDeviceGoingDown: no need to disable promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
1992 | return NOTIFY_OK;
|
---|
1993 | }
|
---|
1994 |
|
---|
1995 | /**
|
---|
1996 | * Callback for listening to MTU change event.
|
---|
1997 | *
|
---|
1998 | * We need to track changes of host's inteface MTU to discard over-sized frames
|
---|
1999 | * coming from the internal network as they may hang the TX queue of host's
|
---|
2000 | * adapter.
|
---|
2001 | *
|
---|
2002 | * @returns NOTIFY_OK
|
---|
2003 | * @param pThis The netfilter instance.
|
---|
2004 | * @param pDev Pointer to device structure of host's interface.
|
---|
2005 | */
|
---|
2006 | static int vboxNetFltLinuxDeviceMtuChange(PVBOXNETFLTINS pThis, struct net_device *pDev)
|
---|
2007 | {
|
---|
2008 | ASMAtomicWriteU32(&pThis->u.s.cbMtu, pDev->mtu);
|
---|
2009 | Log(("vboxNetFltLinuxDeviceMtuChange: set MTU for %s to %d\n", pThis->szName, pDev->mtu));
|
---|
2010 | return NOTIFY_OK;
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | #ifdef LOG_ENABLED
|
---|
2014 | /** Stringify the NETDEV_XXX constants. */
|
---|
2015 | static const char *vboxNetFltLinuxGetNetDevEventName(unsigned long ulEventType)
|
---|
2016 | {
|
---|
2017 | const char *pszEvent = "NETDEV_<unknown>";
|
---|
2018 | switch (ulEventType)
|
---|
2019 | {
|
---|
2020 | case NETDEV_REGISTER: pszEvent = "NETDEV_REGISTER"; break;
|
---|
2021 | case NETDEV_UNREGISTER: pszEvent = "NETDEV_UNREGISTER"; break;
|
---|
2022 | case NETDEV_UP: pszEvent = "NETDEV_UP"; break;
|
---|
2023 | case NETDEV_DOWN: pszEvent = "NETDEV_DOWN"; break;
|
---|
2024 | case NETDEV_REBOOT: pszEvent = "NETDEV_REBOOT"; break;
|
---|
2025 | case NETDEV_CHANGENAME: pszEvent = "NETDEV_CHANGENAME"; break;
|
---|
2026 | case NETDEV_CHANGE: pszEvent = "NETDEV_CHANGE"; break;
|
---|
2027 | case NETDEV_CHANGEMTU: pszEvent = "NETDEV_CHANGEMTU"; break;
|
---|
2028 | case NETDEV_CHANGEADDR: pszEvent = "NETDEV_CHANGEADDR"; break;
|
---|
2029 | case NETDEV_GOING_DOWN: pszEvent = "NETDEV_GOING_DOWN"; break;
|
---|
2030 | # ifdef NETDEV_FEAT_CHANGE
|
---|
2031 | case NETDEV_FEAT_CHANGE: pszEvent = "NETDEV_FEAT_CHANGE"; break;
|
---|
2032 | # endif
|
---|
2033 | }
|
---|
2034 | return pszEvent;
|
---|
2035 | }
|
---|
2036 | #endif /* LOG_ENABLED */
|
---|
2037 |
|
---|
2038 | /**
|
---|
2039 | * Callback for listening to netdevice events.
|
---|
2040 | *
|
---|
2041 | * This works the rediscovery, clean up on unregistration, promiscuity on
|
---|
2042 | * up/down, and GSO feature changes from ethtool.
|
---|
2043 | *
|
---|
2044 | * @returns NOTIFY_OK
|
---|
2045 | * @param self Pointer to our notifier registration block.
|
---|
2046 | * @param ulEventType The event.
|
---|
2047 | * @param ptr Event specific, but it is usually the device it
|
---|
2048 | * relates to.
|
---|
2049 | */
|
---|
2050 | static int vboxNetFltLinuxNotifierCallback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
|
---|
2051 |
|
---|
2052 | {
|
---|
2053 | PVBOXNETFLTINS pThis = VBOX_FLT_NB_TO_INST(self);
|
---|
2054 | struct net_device *pMyDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
2055 | struct net_device *pDev = VBOX_NETDEV_NOTIFIER_INFO_TO_DEV(ptr);
|
---|
2056 | int rc = NOTIFY_OK;
|
---|
2057 |
|
---|
2058 | Log(("VBoxNetFlt: got event %s(0x%lx) on %s, pDev=%p pThis=%p pThis->u.s.pDev=%p\n",
|
---|
2059 | vboxNetFltLinuxGetNetDevEventName(ulEventType), ulEventType, pDev->name, pDev, pThis, pMyDev));
|
---|
2060 |
|
---|
2061 | if (ulEventType == NETDEV_REGISTER)
|
---|
2062 | {
|
---|
2063 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) /* cgroups/namespaces introduced */
|
---|
2064 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
|
---|
2065 | # define VBOX_DEV_NET(dev) dev_net(dev)
|
---|
2066 | # define VBOX_NET_EQ(n1, n2) net_eq((n1), (n2))
|
---|
2067 | # else
|
---|
2068 | # define VBOX_DEV_NET(dev) ((dev)->nd_net)
|
---|
2069 | # define VBOX_NET_EQ(n1, n2) ((n1) == (n2))
|
---|
2070 | # endif
|
---|
2071 | struct net *pMyNet = current->nsproxy->net_ns;
|
---|
2072 | struct net *pDevNet = VBOX_DEV_NET(pDev);
|
---|
2073 |
|
---|
2074 | if (VBOX_NET_EQ(pDevNet, pMyNet))
|
---|
2075 | #endif /* namespaces */
|
---|
2076 | {
|
---|
2077 | if (strcmp(pDev->name, pThis->szName) == 0)
|
---|
2078 | {
|
---|
2079 | vboxNetFltLinuxAttachToInterface(pThis, pDev);
|
---|
2080 | }
|
---|
2081 | }
|
---|
2082 | }
|
---|
2083 | else
|
---|
2084 | {
|
---|
2085 | if (pDev == pMyDev)
|
---|
2086 | {
|
---|
2087 | switch (ulEventType)
|
---|
2088 | {
|
---|
2089 | case NETDEV_UNREGISTER:
|
---|
2090 | rc = vboxNetFltLinuxUnregisterDevice(pThis, pDev);
|
---|
2091 | break;
|
---|
2092 | case NETDEV_UP:
|
---|
2093 | rc = vboxNetFltLinuxDeviceIsUp(pThis, pDev);
|
---|
2094 | break;
|
---|
2095 | case NETDEV_GOING_DOWN:
|
---|
2096 | rc = vboxNetFltLinuxDeviceGoingDown(pThis, pDev);
|
---|
2097 | break;
|
---|
2098 | case NETDEV_CHANGEMTU:
|
---|
2099 | rc = vboxNetFltLinuxDeviceMtuChange(pThis, pDev);
|
---|
2100 | break;
|
---|
2101 | case NETDEV_CHANGENAME:
|
---|
2102 | break;
|
---|
2103 | #ifdef NETDEV_FEAT_CHANGE
|
---|
2104 | case NETDEV_FEAT_CHANGE:
|
---|
2105 | vboxNetFltLinuxReportNicGsoCapabilities(pThis);
|
---|
2106 | break;
|
---|
2107 | #endif
|
---|
2108 | }
|
---|
2109 | }
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 | return rc;
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | /*
|
---|
2116 | * Initial enumeration of netdevs. Called with NETDEV_REGISTER by
|
---|
2117 | * register_netdevice_notifier() under rtnl lock.
|
---|
2118 | */
|
---|
2119 | static int vboxNetFltLinuxEnumeratorCallback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
|
---|
2120 | {
|
---|
2121 | PVBOXNETFLTINS pThis = ((PVBOXNETFLTNOTIFIER)self)->pThis;
|
---|
2122 | struct net_device *dev = VBOX_NETDEV_NOTIFIER_INFO_TO_DEV(ptr);
|
---|
2123 | struct in_device *in_dev;
|
---|
2124 | struct inet6_dev *in6_dev;
|
---|
2125 |
|
---|
2126 | if (ulEventType != NETDEV_REGISTER)
|
---|
2127 | return NOTIFY_OK;
|
---|
2128 |
|
---|
2129 | if (RT_UNLIKELY(pThis->pSwitchPort->pfnNotifyHostAddress == NULL))
|
---|
2130 | return NOTIFY_OK;
|
---|
2131 |
|
---|
2132 | /*
|
---|
2133 | * IPv4
|
---|
2134 | */
|
---|
2135 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
|
---|
2136 | in_dev = __in_dev_get_rtnl(dev);
|
---|
2137 | #else
|
---|
2138 | in_dev = __in_dev_get(dev);
|
---|
2139 | #endif
|
---|
2140 | if (in_dev != NULL)
|
---|
2141 | {
|
---|
2142 | struct in_ifaddr *ifa;
|
---|
2143 |
|
---|
2144 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
|
---|
2145 | if (VBOX_IPV4_IS_LOOPBACK(ifa->ifa_address))
|
---|
2146 | return NOTIFY_OK;
|
---|
2147 |
|
---|
2148 | if ( dev != pThis->u.s.pDev
|
---|
2149 | && VBOX_IPV4_IS_LINKLOCAL_169(ifa->ifa_address))
|
---|
2150 | continue;
|
---|
2151 |
|
---|
2152 | Log(("%s: %s: IPv4 addr %RTnaipv4 mask %RTnaipv4\n",
|
---|
2153 | __FUNCTION__, VBOX_NETDEV_NAME(dev),
|
---|
2154 | ifa->ifa_address, ifa->ifa_mask));
|
---|
2155 |
|
---|
2156 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
2157 | /* :fAdded */ true, kIntNetAddrType_IPv4, &ifa->ifa_address);
|
---|
2158 | }
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | /*
|
---|
2162 | * IPv6
|
---|
2163 | */
|
---|
2164 | in6_dev = __in6_dev_get(dev);
|
---|
2165 | if (in6_dev != NULL)
|
---|
2166 | {
|
---|
2167 | struct inet6_ifaddr *ifa;
|
---|
2168 |
|
---|
2169 | read_lock_bh(&in6_dev->lock);
|
---|
2170 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
|
---|
2171 | list_for_each_entry(ifa, &in6_dev->addr_list, if_list)
|
---|
2172 | #else
|
---|
2173 | for (ifa = in6_dev->addr_list; ifa != NULL; ifa = ifa->if_next)
|
---|
2174 | #endif
|
---|
2175 | {
|
---|
2176 | if ( dev != pThis->u.s.pDev
|
---|
2177 | && ipv6_addr_type(&ifa->addr) & (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK))
|
---|
2178 | continue;
|
---|
2179 |
|
---|
2180 | Log(("%s: %s: IPv6 addr %RTnaipv6/%u\n",
|
---|
2181 | __FUNCTION__, VBOX_NETDEV_NAME(dev),
|
---|
2182 | &ifa->addr, (unsigned)ifa->prefix_len));
|
---|
2183 |
|
---|
2184 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
2185 | /* :fAdded */ true, kIntNetAddrType_IPv6, &ifa->addr);
|
---|
2186 | }
|
---|
2187 | read_unlock_bh(&in6_dev->lock);
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | return NOTIFY_OK;
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 |
|
---|
2194 | static int vboxNetFltLinuxNotifierIPv4Callback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
|
---|
2195 | {
|
---|
2196 | PVBOXNETFLTINS pThis = RT_FROM_MEMBER(self, VBOXNETFLTINS, u.s.NotifierIPv4);
|
---|
2197 | struct net_device *pDev, *pEventDev;
|
---|
2198 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
|
---|
2199 | bool fMyDev;
|
---|
2200 | int rc = NOTIFY_OK;
|
---|
2201 |
|
---|
2202 | pDev = vboxNetFltLinuxRetainNetDev(pThis);
|
---|
2203 | pEventDev = ifa->ifa_dev->dev;
|
---|
2204 | fMyDev = (pDev == pEventDev);
|
---|
2205 | Log(("VBoxNetFlt: %s: IPv4 event %s(0x%lx) %s: addr %RTnaipv4 mask %RTnaipv4\n",
|
---|
2206 | pDev ? VBOX_NETDEV_NAME(pDev) : "<unknown>",
|
---|
2207 | vboxNetFltLinuxGetNetDevEventName(ulEventType), ulEventType,
|
---|
2208 | pEventDev ? VBOX_NETDEV_NAME(pEventDev) : "<unknown>",
|
---|
2209 | ifa->ifa_address, ifa->ifa_mask));
|
---|
2210 |
|
---|
2211 | if (pDev != NULL)
|
---|
2212 | vboxNetFltLinuxReleaseNetDev(pThis, pDev);
|
---|
2213 |
|
---|
2214 | if (VBOX_IPV4_IS_LOOPBACK(ifa->ifa_address))
|
---|
2215 | return NOTIFY_OK;
|
---|
2216 |
|
---|
2217 | if ( !fMyDev
|
---|
2218 | && VBOX_IPV4_IS_LINKLOCAL_169(ifa->ifa_address))
|
---|
2219 | return NOTIFY_OK;
|
---|
2220 |
|
---|
2221 | if (pThis->pSwitchPort->pfnNotifyHostAddress)
|
---|
2222 | {
|
---|
2223 | bool fAdded;
|
---|
2224 | if (ulEventType == NETDEV_UP)
|
---|
2225 | fAdded = true;
|
---|
2226 | else if (ulEventType == NETDEV_DOWN)
|
---|
2227 | fAdded = false;
|
---|
2228 | else
|
---|
2229 | return NOTIFY_OK;
|
---|
2230 |
|
---|
2231 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, fAdded,
|
---|
2232 | kIntNetAddrType_IPv4, &ifa->ifa_local);
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | return rc;
|
---|
2236 | }
|
---|
2237 |
|
---|
2238 |
|
---|
2239 | static int vboxNetFltLinuxNotifierIPv6Callback(struct notifier_block *self, unsigned long ulEventType, void *ptr)
|
---|
2240 | {
|
---|
2241 | PVBOXNETFLTINS pThis = RT_FROM_MEMBER(self, VBOXNETFLTINS, u.s.NotifierIPv6);
|
---|
2242 | struct net_device *pDev, *pEventDev;
|
---|
2243 | struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
|
---|
2244 | bool fMyDev;
|
---|
2245 | int rc = NOTIFY_OK;
|
---|
2246 |
|
---|
2247 | pDev = vboxNetFltLinuxRetainNetDev(pThis);
|
---|
2248 | pEventDev = ifa->idev->dev;
|
---|
2249 | fMyDev = (pDev == pEventDev);
|
---|
2250 | Log(("VBoxNetFlt: %s: IPv6 event %s(0x%lx) %s: %RTnaipv6\n",
|
---|
2251 | pDev ? VBOX_NETDEV_NAME(pDev) : "<unknown>",
|
---|
2252 | vboxNetFltLinuxGetNetDevEventName(ulEventType), ulEventType,
|
---|
2253 | pEventDev ? VBOX_NETDEV_NAME(pEventDev) : "<unknown>",
|
---|
2254 | &ifa->addr));
|
---|
2255 |
|
---|
2256 | if (pDev != NULL)
|
---|
2257 | vboxNetFltLinuxReleaseNetDev(pThis, pDev);
|
---|
2258 |
|
---|
2259 | if ( !fMyDev
|
---|
2260 | && ipv6_addr_type(&ifa->addr) & (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK))
|
---|
2261 | return NOTIFY_OK;
|
---|
2262 |
|
---|
2263 | if (pThis->pSwitchPort->pfnNotifyHostAddress)
|
---|
2264 | {
|
---|
2265 | bool fAdded;
|
---|
2266 | if (ulEventType == NETDEV_UP)
|
---|
2267 | fAdded = true;
|
---|
2268 | else if (ulEventType == NETDEV_DOWN)
|
---|
2269 | fAdded = false;
|
---|
2270 | else
|
---|
2271 | return NOTIFY_OK;
|
---|
2272 |
|
---|
2273 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, fAdded,
|
---|
2274 | kIntNetAddrType_IPv6, &ifa->addr);
|
---|
2275 | }
|
---|
2276 |
|
---|
2277 | return rc;
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 |
|
---|
2281 | bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
|
---|
2282 | {
|
---|
2283 | return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
|
---|
2284 | }
|
---|
2285 |
|
---|
2286 | int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
|
---|
2287 | {
|
---|
2288 | struct net_device * pDev;
|
---|
2289 | int err;
|
---|
2290 | int rc = VINF_SUCCESS;
|
---|
2291 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2292 | NOREF(pvIfData);
|
---|
2293 |
|
---|
2294 | LogFlow(("vboxNetFltPortOsXmit: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
2295 |
|
---|
2296 | pDev = vboxNetFltLinuxRetainNetDev(pThis);
|
---|
2297 | if (pDev)
|
---|
2298 | {
|
---|
2299 | /*
|
---|
2300 | * Create a sk_buff for the gather list and push it onto the wire.
|
---|
2301 | */
|
---|
2302 | if (fDst & INTNETTRUNKDIR_WIRE)
|
---|
2303 | {
|
---|
2304 | struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, true);
|
---|
2305 | if (pBuf)
|
---|
2306 | {
|
---|
2307 | vboxNetFltDumpPacket(pSG, true, "wire", 1);
|
---|
2308 | Log6(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
|
---|
2309 | Log6(("vboxNetFltPortOsXmit: dev_queue_xmit(%p)\n", pBuf));
|
---|
2310 | err = dev_queue_xmit(pBuf);
|
---|
2311 | if (err)
|
---|
2312 | rc = RTErrConvertFromErrno(err);
|
---|
2313 | }
|
---|
2314 | else
|
---|
2315 | rc = VERR_NO_MEMORY;
|
---|
2316 | }
|
---|
2317 |
|
---|
2318 | /*
|
---|
2319 | * Create a sk_buff for the gather list and push it onto the host stack.
|
---|
2320 | */
|
---|
2321 | if (fDst & INTNETTRUNKDIR_HOST)
|
---|
2322 | {
|
---|
2323 | struct sk_buff *pBuf = vboxNetFltLinuxSkBufFromSG(pThis, pSG, false);
|
---|
2324 | if (pBuf)
|
---|
2325 | {
|
---|
2326 | vboxNetFltDumpPacket(pSG, true, "host", (fDst & INTNETTRUNKDIR_WIRE) ? 0 : 1);
|
---|
2327 | Log6(("vboxNetFltPortOsXmit: pBuf->cb dump:\n%.*Rhxd\n", sizeof(pBuf->cb), pBuf->cb));
|
---|
2328 | Log6(("vboxNetFltPortOsXmit: netif_rx_ni(%p)\n", pBuf));
|
---|
2329 | err = netif_rx_ni(pBuf);
|
---|
2330 | if (err)
|
---|
2331 | rc = RTErrConvertFromErrno(err);
|
---|
2332 | }
|
---|
2333 | else
|
---|
2334 | rc = VERR_NO_MEMORY;
|
---|
2335 | }
|
---|
2336 |
|
---|
2337 | vboxNetFltLinuxReleaseNetDev(pThis, pDev);
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2341 | return rc;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 |
|
---|
2345 | void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
|
---|
2346 | {
|
---|
2347 | struct net_device *pDev;
|
---|
2348 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2349 |
|
---|
2350 | LogFlow(("vboxNetFltPortOsSetActive: pThis=%p (%s), fActive=%RTbool, fDisablePromiscuous=%RTbool\n",
|
---|
2351 | pThis, pThis->szName, fActive, pThis->fDisablePromiscuous));
|
---|
2352 |
|
---|
2353 | if (pThis->fDisablePromiscuous)
|
---|
2354 | return;
|
---|
2355 |
|
---|
2356 | pDev = vboxNetFltLinuxRetainNetDev(pThis);
|
---|
2357 | if (pDev)
|
---|
2358 | {
|
---|
2359 | /*
|
---|
2360 | * This api is a bit weird, the best reference is the code.
|
---|
2361 | *
|
---|
2362 | * Also, we have a bit or race conditions wrt the maintenance of
|
---|
2363 | * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
|
---|
2364 | */
|
---|
2365 | #ifdef LOG_ENABLED
|
---|
2366 | u_int16_t fIf;
|
---|
2367 | unsigned const cPromiscBefore = pDev->promiscuity;
|
---|
2368 | #endif
|
---|
2369 | if (fActive)
|
---|
2370 | {
|
---|
2371 | Assert(!pThis->u.s.fPromiscuousSet);
|
---|
2372 |
|
---|
2373 | rtnl_lock();
|
---|
2374 | dev_set_promiscuity(pDev, 1);
|
---|
2375 | rtnl_unlock();
|
---|
2376 | pThis->u.s.fPromiscuousSet = true;
|
---|
2377 | Log(("vboxNetFltPortOsSetActive: enabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
2378 | }
|
---|
2379 | else
|
---|
2380 | {
|
---|
2381 | if (pThis->u.s.fPromiscuousSet)
|
---|
2382 | {
|
---|
2383 | rtnl_lock();
|
---|
2384 | dev_set_promiscuity(pDev, -1);
|
---|
2385 | rtnl_unlock();
|
---|
2386 | Log(("vboxNetFltPortOsSetActive: disabled promiscuous mode on %s (%d)\n", pThis->szName, pDev->promiscuity));
|
---|
2387 | }
|
---|
2388 | pThis->u.s.fPromiscuousSet = false;
|
---|
2389 |
|
---|
2390 | #ifdef LOG_ENABLED
|
---|
2391 | fIf = dev_get_flags(pDev);
|
---|
2392 | Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, pDev->promiscuity));
|
---|
2393 | #endif
|
---|
2394 | }
|
---|
2395 |
|
---|
2396 | vboxNetFltLinuxReleaseNetDev(pThis, pDev);
|
---|
2397 | }
|
---|
2398 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2399 | }
|
---|
2400 |
|
---|
2401 |
|
---|
2402 | int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
|
---|
2403 | {
|
---|
2404 | /*
|
---|
2405 | * Remove packet handler when we get disconnected from internal switch as
|
---|
2406 | * we don't want the handler to forward packets to disconnected switch.
|
---|
2407 | */
|
---|
2408 | if (ASMAtomicCmpXchgBool(&pThis->u.s.fPacketHandler, false, true))
|
---|
2409 | {
|
---|
2410 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2411 | dev_remove_pack(&pThis->u.s.PacketType);
|
---|
2412 | Log(("vboxNetFltOsDisconnectIt: this=%p: Packet handler removed.\n", pThis));
|
---|
2413 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2414 | }
|
---|
2415 | return VINF_SUCCESS;
|
---|
2416 | }
|
---|
2417 |
|
---|
2418 |
|
---|
2419 | int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
|
---|
2420 | {
|
---|
2421 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2422 |
|
---|
2423 | /*
|
---|
2424 | * Report the GSO capabilities of the host and device (if connected).
|
---|
2425 | * Note! No need to mark ourselves busy here.
|
---|
2426 | */
|
---|
2427 | /** @todo duplicate work here now? Attach */
|
---|
2428 | #if defined(VBOXNETFLT_WITH_GSO_XMIT_HOST)
|
---|
2429 | Log3(("vboxNetFltOsConnectIt: reporting host tso tso6\n"));
|
---|
2430 | pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort,
|
---|
2431 | 0
|
---|
2432 | | RT_BIT_32(PDMNETWORKGSOTYPE_IPV4_TCP)
|
---|
2433 | | RT_BIT_32(PDMNETWORKGSOTYPE_IPV6_TCP)
|
---|
2434 | , INTNETTRUNKDIR_HOST);
|
---|
2435 |
|
---|
2436 | #endif
|
---|
2437 | vboxNetFltLinuxReportNicGsoCapabilities(pThis);
|
---|
2438 |
|
---|
2439 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2440 | return VINF_SUCCESS;
|
---|
2441 | }
|
---|
2442 |
|
---|
2443 |
|
---|
2444 | void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
|
---|
2445 | {
|
---|
2446 | struct net_device *pDev;
|
---|
2447 | bool fRegistered;
|
---|
2448 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2449 |
|
---|
2450 | #ifdef VBOXNETFLT_WITH_HOST2WIRE_FILTER
|
---|
2451 | vboxNetFltLinuxUnhookDev(pThis, NULL);
|
---|
2452 | #endif
|
---|
2453 |
|
---|
2454 | /** @todo This code may race vboxNetFltLinuxUnregisterDevice (very very
|
---|
2455 | * unlikely, but none the less). Since it doesn't actually update the
|
---|
2456 | * state (just reads it), it is likely to panic in some interesting
|
---|
2457 | * ways. */
|
---|
2458 |
|
---|
2459 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
2460 | pDev = ASMAtomicUoReadPtrT(&pThis->u.s.pDev, struct net_device *);
|
---|
2461 | fRegistered = ASMAtomicXchgBool(&pThis->u.s.fRegistered, false);
|
---|
2462 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
2463 |
|
---|
2464 | if (fRegistered)
|
---|
2465 | {
|
---|
2466 | vboxNetFltSetLinkState(pThis, pDev, false);
|
---|
2467 |
|
---|
2468 | #ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
2469 | skb_queue_purge(&pThis->u.s.XmitQueue);
|
---|
2470 | #endif
|
---|
2471 | Log(("vboxNetFltOsDeleteInstance: this=%p: xmit queue purged.\n", pThis));
|
---|
2472 | Log(("vboxNetFltOsDeleteInstance: Device %p(%s) released. ref=%d\n",
|
---|
2473 | pDev, pDev->name,
|
---|
2474 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
2475 | netdev_refcnt_read(pDev)
|
---|
2476 | #else
|
---|
2477 | atomic_read(&pDev->refcnt)
|
---|
2478 | #endif
|
---|
2479 | ));
|
---|
2480 | dev_put(pDev);
|
---|
2481 | }
|
---|
2482 |
|
---|
2483 | unregister_inet6addr_notifier(&pThis->u.s.NotifierIPv6);
|
---|
2484 | unregister_inetaddr_notifier(&pThis->u.s.NotifierIPv4);
|
---|
2485 |
|
---|
2486 | Log(("vboxNetFltOsDeleteInstance: this=%p: Notifier removed.\n", pThis));
|
---|
2487 | unregister_netdevice_notifier(&pThis->u.s.Notifier);
|
---|
2488 | module_put(THIS_MODULE);
|
---|
2489 |
|
---|
2490 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2491 | }
|
---|
2492 |
|
---|
2493 |
|
---|
2494 | int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
|
---|
2495 | {
|
---|
2496 | int err;
|
---|
2497 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2498 | NOREF(pvContext);
|
---|
2499 |
|
---|
2500 | pThis->u.s.Notifier.notifier_call = vboxNetFltLinuxNotifierCallback;
|
---|
2501 | err = register_netdevice_notifier(&pThis->u.s.Notifier);
|
---|
2502 | if (err)
|
---|
2503 | {
|
---|
2504 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2505 | return VERR_INTNET_FLT_IF_FAILED;
|
---|
2506 | }
|
---|
2507 | if (!pThis->u.s.fRegistered)
|
---|
2508 | {
|
---|
2509 | unregister_netdevice_notifier(&pThis->u.s.Notifier);
|
---|
2510 | LogRel(("VBoxNetFlt: failed to find %s.\n", pThis->szName));
|
---|
2511 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2512 | return VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
2513 | }
|
---|
2514 |
|
---|
2515 | Log(("vboxNetFltOsInitInstance: this=%p: Notifier installed.\n", pThis));
|
---|
2516 | if ( pThis->fDisconnectedFromHost
|
---|
2517 | || !try_module_get(THIS_MODULE))
|
---|
2518 | {
|
---|
2519 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2520 | return VERR_INTNET_FLT_IF_FAILED;
|
---|
2521 | }
|
---|
2522 |
|
---|
2523 | if (pThis->pSwitchPort->pfnNotifyHostAddress)
|
---|
2524 | {
|
---|
2525 | VBOXNETFLTNOTIFIER Enumerator;
|
---|
2526 |
|
---|
2527 | /*
|
---|
2528 | * register_inetaddr_notifier() and register_inet6addr_notifier()
|
---|
2529 | * do not call the callback for existing devices. Enumerating
|
---|
2530 | * all network devices explicitly is a bit of an ifdef mess,
|
---|
2531 | * so co-opt register_netdevice_notifier() to do that for us.
|
---|
2532 | */
|
---|
2533 | RT_ZERO(Enumerator);
|
---|
2534 | Enumerator.Notifier.notifier_call = vboxNetFltLinuxEnumeratorCallback;
|
---|
2535 | Enumerator.pThis = pThis;
|
---|
2536 |
|
---|
2537 | err = register_netdevice_notifier(&Enumerator.Notifier);
|
---|
2538 | if (err)
|
---|
2539 | {
|
---|
2540 | LogRel(("%s: failed to enumerate network devices: error %d\n", __FUNCTION__, err));
|
---|
2541 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2542 | return VINF_SUCCESS;
|
---|
2543 | }
|
---|
2544 |
|
---|
2545 | unregister_netdevice_notifier(&Enumerator.Notifier);
|
---|
2546 |
|
---|
2547 | pThis->u.s.NotifierIPv4.notifier_call = vboxNetFltLinuxNotifierIPv4Callback;
|
---|
2548 | err = register_inetaddr_notifier(&pThis->u.s.NotifierIPv4);
|
---|
2549 | if (err)
|
---|
2550 | LogRel(("%s: failed to register IPv4 notifier: error %d\n", __FUNCTION__, err));
|
---|
2551 |
|
---|
2552 | pThis->u.s.NotifierIPv6.notifier_call = vboxNetFltLinuxNotifierIPv6Callback;
|
---|
2553 | err = register_inet6addr_notifier(&pThis->u.s.NotifierIPv6);
|
---|
2554 | if (err)
|
---|
2555 | LogRel(("%s: failed to register IPv6 notifier: error %d\n", __FUNCTION__, err));
|
---|
2556 | }
|
---|
2557 |
|
---|
2558 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2559 | return VINF_SUCCESS;
|
---|
2560 | }
|
---|
2561 |
|
---|
2562 | int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
|
---|
2563 | {
|
---|
2564 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
2565 |
|
---|
2566 | /*
|
---|
2567 | * Init the linux specific members.
|
---|
2568 | */
|
---|
2569 | ASMAtomicUoWriteNullPtr(&pThis->u.s.pDev);
|
---|
2570 | pThis->u.s.fRegistered = false;
|
---|
2571 | pThis->u.s.fPromiscuousSet = false;
|
---|
2572 | pThis->u.s.fPacketHandler = false;
|
---|
2573 | memset(&pThis->u.s.PacketType, 0, sizeof(pThis->u.s.PacketType));
|
---|
2574 | #ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
|
---|
2575 | skb_queue_head_init(&pThis->u.s.XmitQueue);
|
---|
2576 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
|
---|
2577 | INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask);
|
---|
2578 | # else
|
---|
2579 | INIT_WORK(&pThis->u.s.XmitTask, vboxNetFltLinuxXmitTask, &pThis->u.s.XmitTask);
|
---|
2580 | # endif
|
---|
2581 | #endif
|
---|
2582 |
|
---|
2583 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
2584 | return VINF_SUCCESS;
|
---|
2585 | }
|
---|
2586 |
|
---|
2587 |
|
---|
2588 | void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
|
---|
2589 | {
|
---|
2590 | NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
|
---|
2591 | }
|
---|
2592 |
|
---|
2593 |
|
---|
2594 | int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **pvIfData)
|
---|
2595 | {
|
---|
2596 | /* Nothing to do */
|
---|
2597 | NOREF(pThis); NOREF(pvIf); NOREF(pvIfData);
|
---|
2598 | return VINF_SUCCESS;
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 |
|
---|
2602 | int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
|
---|
2603 | {
|
---|
2604 | /* Nothing to do */
|
---|
2605 | NOREF(pThis); NOREF(pvIfData);
|
---|
2606 | return VINF_SUCCESS;
|
---|
2607 | }
|
---|
2608 |
|
---|