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