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