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