1 | /* $Id: VBoxNetAdp-linux.c 35809 2011-02-01 12:31:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetAdp - Virtual Network Adapter Driver (Host), Linux Specific Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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 | #include "the-linux-kernel.h"
|
---|
22 | #include "version-generated.h"
|
---|
23 | #include "product-generated.h"
|
---|
24 | #include <linux/netdevice.h>
|
---|
25 | #include <linux/etherdevice.h>
|
---|
26 | #include <linux/miscdevice.h>
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_NET_ADP_DRV
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <iprt/process.h>
|
---|
32 | #include <iprt/initterm.h>
|
---|
33 | #include <iprt/mem.h>
|
---|
34 |
|
---|
35 | /*
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/semaphore.h>
|
---|
38 | #include <iprt/spinlock.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/uuid.h>
|
---|
41 | #include <iprt/alloca.h>
|
---|
42 | */
|
---|
43 |
|
---|
44 | #define VBOXNETADP_OS_SPECFIC 1
|
---|
45 | #include "../VBoxNetAdpInternal.h"
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Defined Constants And Macros *
|
---|
49 | *******************************************************************************/
|
---|
50 | #define VBOXNETADP_LINUX_NAME "vboxnet%d"
|
---|
51 | #define VBOXNETADP_CTL_DEV_NAME "vboxnetctl"
|
---|
52 |
|
---|
53 | #define VBOXNETADP_FROM_IFACE(iface) ((PVBOXNETADP) ifnet_softc(iface))
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Internal Functions *
|
---|
57 | *******************************************************************************/
|
---|
58 | static int VBoxNetAdpLinuxInit(void);
|
---|
59 | static void VBoxNetAdpLinuxUnload(void);
|
---|
60 |
|
---|
61 | static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp);
|
---|
62 | static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp);
|
---|
63 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
|
---|
64 | static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
|
---|
65 | unsigned int uCmd, unsigned long ulArg);
|
---|
66 | #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
67 | static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
|
---|
68 | unsigned int uCmd, unsigned long ulArg);
|
---|
69 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
70 |
|
---|
71 | /*******************************************************************************
|
---|
72 | * Global Variables *
|
---|
73 | *******************************************************************************/
|
---|
74 | module_init(VBoxNetAdpLinuxInit);
|
---|
75 | module_exit(VBoxNetAdpLinuxUnload);
|
---|
76 |
|
---|
77 | MODULE_AUTHOR(VBOX_VENDOR);
|
---|
78 | MODULE_DESCRIPTION(VBOX_PRODUCT " Network Adapter Driver");
|
---|
79 | MODULE_LICENSE("GPL");
|
---|
80 | #ifdef MODULE_VERSION
|
---|
81 | MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * The (common) global data.
|
---|
86 | */
|
---|
87 | static struct file_operations gFileOpsVBoxNetAdp =
|
---|
88 | {
|
---|
89 | owner: THIS_MODULE,
|
---|
90 | open: VBoxNetAdpLinuxOpen,
|
---|
91 | release: VBoxNetAdpLinuxClose,
|
---|
92 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
|
---|
93 | ioctl: VBoxNetAdpLinuxIOCtl,
|
---|
94 | #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
95 | unlocked_ioctl: VBoxNetAdpLinuxIOCtlUnlocked,
|
---|
96 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
97 | };
|
---|
98 |
|
---|
99 | /** The miscdevice structure. */
|
---|
100 | static struct miscdevice g_CtlDev =
|
---|
101 | {
|
---|
102 | minor: MISC_DYNAMIC_MINOR,
|
---|
103 | name: VBOXNETADP_CTL_DEV_NAME,
|
---|
104 | fops: &gFileOpsVBoxNetAdp,
|
---|
105 | # if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
|
---|
106 | devfs_name: VBOXNETADP_CTL_DEV_NAME
|
---|
107 | # endif
|
---|
108 | };
|
---|
109 |
|
---|
110 | struct VBoxNetAdpPriv
|
---|
111 | {
|
---|
112 | struct net_device_stats Stats;
|
---|
113 | };
|
---|
114 |
|
---|
115 | typedef struct VBoxNetAdpPriv VBOXNETADPPRIV;
|
---|
116 | typedef VBOXNETADPPRIV *PVBOXNETADPPRIV;
|
---|
117 |
|
---|
118 | static int vboxNetAdpLinuxOpen(struct net_device *pNetDev)
|
---|
119 | {
|
---|
120 | netif_start_queue(pNetDev);
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int vboxNetAdpLinuxStop(struct net_device *pNetDev)
|
---|
125 | {
|
---|
126 | netif_stop_queue(pNetDev);
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static int vboxNetAdpLinuxXmit(struct sk_buff *pSkb, struct net_device *pNetDev)
|
---|
131 | {
|
---|
132 | PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
|
---|
133 |
|
---|
134 | /* Update the stats. */
|
---|
135 | pPriv->Stats.tx_packets++;
|
---|
136 | pPriv->Stats.tx_bytes += pSkb->len;
|
---|
137 | /* Update transmission time stamp. */
|
---|
138 | pNetDev->trans_start = jiffies;
|
---|
139 | /* Nothing else to do, just free the sk_buff. */
|
---|
140 | dev_kfree_skb(pSkb);
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 |
|
---|
144 | struct net_device_stats *vboxNetAdpLinuxGetStats(struct net_device *pNetDev)
|
---|
145 | {
|
---|
146 | PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
|
---|
147 | return &pPriv->Stats;
|
---|
148 | }
|
---|
149 |
|
---|
150 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
151 | static const struct net_device_ops vboxNetAdpNetdevOps = {
|
---|
152 | .ndo_open = vboxNetAdpLinuxOpen,
|
---|
153 | .ndo_stop = vboxNetAdpLinuxStop,
|
---|
154 | .ndo_start_xmit = vboxNetAdpLinuxXmit,
|
---|
155 | .ndo_get_stats = vboxNetAdpLinuxGetStats
|
---|
156 | };
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | static void vboxNetAdpNetDevInit(struct net_device *pNetDev)
|
---|
160 | {
|
---|
161 | PVBOXNETADPPRIV pPriv;
|
---|
162 |
|
---|
163 | ether_setup(pNetDev);
|
---|
164 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
165 | pNetDev->netdev_ops = &vboxNetAdpNetdevOps;
|
---|
166 | #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
|
---|
167 | pNetDev->open = vboxNetAdpLinuxOpen;
|
---|
168 | pNetDev->stop = vboxNetAdpLinuxStop;
|
---|
169 | pNetDev->hard_start_xmit = vboxNetAdpLinuxXmit;
|
---|
170 | pNetDev->get_stats = vboxNetAdpLinuxGetStats;
|
---|
171 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
|
---|
172 |
|
---|
173 | pPriv = netdev_priv(pNetDev);
|
---|
174 | memset(pPriv, 0, sizeof(*pPriv));
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMACAddress)
|
---|
179 | {
|
---|
180 | int rc = VINF_SUCCESS;
|
---|
181 | struct net_device *pNetDev;
|
---|
182 |
|
---|
183 | /* No need for private data. */
|
---|
184 | pNetDev = alloc_netdev(sizeof(VBOXNETADPPRIV),
|
---|
185 | pThis->szName[0] ? pThis->szName : VBOXNETADP_LINUX_NAME,
|
---|
186 | vboxNetAdpNetDevInit);
|
---|
187 | if (pNetDev)
|
---|
188 | {
|
---|
189 | int err;
|
---|
190 |
|
---|
191 | if (pNetDev->dev_addr)
|
---|
192 | {
|
---|
193 | memcpy(pNetDev->dev_addr, pMACAddress, ETH_ALEN);
|
---|
194 | Log2(("vboxNetAdpOsCreate: pNetDev->dev_addr = %.6Rhxd\n", pNetDev->dev_addr));
|
---|
195 | err = register_netdev(pNetDev);
|
---|
196 | if (!err)
|
---|
197 | {
|
---|
198 | strncpy(pThis->szName, pNetDev->name, sizeof(pThis->szName));
|
---|
199 | pThis->szName[sizeof(pThis->szName) - 1] = '\0';
|
---|
200 | pThis->u.s.pNetDev = pNetDev;
|
---|
201 | Log2(("vboxNetAdpOsCreate: pThis=%p pThis->szName = %p\n", pThis, pThis->szName));
|
---|
202 | return VINF_SUCCESS;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else
|
---|
206 | {
|
---|
207 | LogRel(("VBoxNetAdp: failed to set MAC address (dev->dev_addr == NULL)\n"));
|
---|
208 | err = EFAULT;
|
---|
209 | }
|
---|
210 | free_netdev(pNetDev);
|
---|
211 | rc = RTErrConvertFromErrno(err);
|
---|
212 | }
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|
216 | void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
|
---|
217 | {
|
---|
218 | struct net_device *pNetDev = pThis->u.s.pNetDev;
|
---|
219 | AssertPtr(pThis->u.s.pNetDev);
|
---|
220 |
|
---|
221 | pThis->u.s.pNetDev = NULL;
|
---|
222 | unregister_netdev(pNetDev);
|
---|
223 | free_netdev(pNetDev);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Device open. Called on open /dev/vboxnetctl
|
---|
228 | *
|
---|
229 | * @param pInode Pointer to inode info structure.
|
---|
230 | * @param pFilp Associated file pointer.
|
---|
231 | */
|
---|
232 | static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp)
|
---|
233 | {
|
---|
234 | Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm));
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Only root is allowed to access the device, enforce it!
|
---|
238 | */
|
---|
239 | if (!capable(CAP_SYS_ADMIN))
|
---|
240 | {
|
---|
241 | Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n"));
|
---|
242 | return -EPERM;
|
---|
243 | }
|
---|
244 |
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Close device.
|
---|
251 | *
|
---|
252 | * @param pInode Pointer to inode info structure.
|
---|
253 | * @param pFilp Associated file pointer.
|
---|
254 | */
|
---|
255 | static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp)
|
---|
256 | {
|
---|
257 | Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n",
|
---|
258 | RTProcSelf(), current->pid, current->comm));
|
---|
259 | pFilp->private_data = NULL;
|
---|
260 | return 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Device I/O Control entry point.
|
---|
265 | *
|
---|
266 | * @param pFilp Associated file pointer.
|
---|
267 | * @param uCmd The function specified to ioctl().
|
---|
268 | * @param ulArg The argument specified to ioctl().
|
---|
269 | */
|
---|
270 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
|
---|
271 | static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
|
---|
272 | unsigned int uCmd, unsigned long ulArg)
|
---|
273 | #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
274 | static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
|
---|
275 | unsigned int uCmd, unsigned long ulArg)
|
---|
276 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
|
---|
277 | {
|
---|
278 | VBOXNETADPREQ Req;
|
---|
279 | PVBOXNETADP pAdp;
|
---|
280 | int rc;
|
---|
281 | char *pszName = NULL;
|
---|
282 |
|
---|
283 | Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", _IOC_SIZE(uCmd), uCmd, VBOXNETADP_CTL_ADD));
|
---|
284 | if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req))) /* paranoia */
|
---|
285 | {
|
---|
286 | Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), _IOC_SIZE(uCmd), uCmd));
|
---|
287 | return -EINVAL;
|
---|
288 | }
|
---|
289 |
|
---|
290 | switch (uCmd)
|
---|
291 | {
|
---|
292 | case VBOXNETADP_CTL_ADD:
|
---|
293 | Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT));
|
---|
294 | if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
|
---|
295 | {
|
---|
296 | Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
|
---|
297 | return -EFAULT;
|
---|
298 | }
|
---|
299 | Log(("VBoxNetAdpLinuxIOCtl: Add %s\n", Req.szName));
|
---|
300 |
|
---|
301 | if (Req.szName[0])
|
---|
302 | {
|
---|
303 | pAdp = vboxNetAdpFindByName(Req.szName);
|
---|
304 | if (pAdp)
|
---|
305 | {
|
---|
306 | Log(("VBoxNetAdpLinuxIOCtl: '%s' already exists\n", Req.szName));
|
---|
307 | return -EINVAL;
|
---|
308 | }
|
---|
309 | pszName = Req.szName;
|
---|
310 | }
|
---|
311 | rc = vboxNetAdpCreate(&pAdp, pszName);
|
---|
312 | if (RT_FAILURE(rc))
|
---|
313 | {
|
---|
314 | Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpCreate -> %Rrc\n", rc));
|
---|
315 | return -EINVAL;
|
---|
316 | }
|
---|
317 |
|
---|
318 | Assert(strlen(pAdp->szName) < sizeof(Req.szName));
|
---|
319 | strncpy(Req.szName, pAdp->szName, sizeof(Req.szName) - 1);
|
---|
320 | Req.szName[sizeof(Req.szName) - 1] = '\0';
|
---|
321 |
|
---|
322 | if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req))))
|
---|
323 | {
|
---|
324 | /* this is really bad! */
|
---|
325 | /** @todo remove the adapter again? */
|
---|
326 | printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#zx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd);
|
---|
327 | return -EFAULT;
|
---|
328 | }
|
---|
329 | Log(("VBoxNetAdpLinuxIOCtl: Successfully added '%s'\n", Req.szName));
|
---|
330 | break;
|
---|
331 |
|
---|
332 | case VBOXNETADP_CTL_REMOVE:
|
---|
333 | if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
|
---|
334 | {
|
---|
335 | Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
|
---|
336 | return -EFAULT;
|
---|
337 | }
|
---|
338 | Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName));
|
---|
339 |
|
---|
340 | pAdp = vboxNetAdpFindByName(Req.szName);
|
---|
341 | if (!pAdp)
|
---|
342 | {
|
---|
343 | Log(("VBoxNetAdpLinuxIOCtl: '%s' not found\n", Req.szName));
|
---|
344 | return -EINVAL;
|
---|
345 | }
|
---|
346 |
|
---|
347 | rc = vboxNetAdpDestroy(pAdp);
|
---|
348 | if (RT_FAILURE(rc))
|
---|
349 | {
|
---|
350 | Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpDestroy('%s') -> %Rrc\n", Req.szName, rc));
|
---|
351 | return -EINVAL;
|
---|
352 | }
|
---|
353 | Log(("VBoxNetAdpLinuxIOCtl: Successfully removed '%s'\n", Req.szName));
|
---|
354 | break;
|
---|
355 |
|
---|
356 | default:
|
---|
357 | printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd);
|
---|
358 | return -EINVAL;
|
---|
359 | }
|
---|
360 |
|
---|
361 | return 0;
|
---|
362 | }
|
---|
363 |
|
---|
364 | int vboxNetAdpOsInit(PVBOXNETADP pThis)
|
---|
365 | {
|
---|
366 | /*
|
---|
367 | * Init linux-specific members.
|
---|
368 | */
|
---|
369 | pThis->u.s.pNetDev = NULL;
|
---|
370 |
|
---|
371 | return VINF_SUCCESS;
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Initialize module.
|
---|
378 | *
|
---|
379 | * @returns appropriate status code.
|
---|
380 | */
|
---|
381 | static int __init VBoxNetAdpLinuxInit(void)
|
---|
382 | {
|
---|
383 | int rc;
|
---|
384 | /*
|
---|
385 | * Initialize IPRT.
|
---|
386 | */
|
---|
387 | rc = RTR0Init(0);
|
---|
388 | if (RT_SUCCESS(rc))
|
---|
389 | {
|
---|
390 | Log(("VBoxNetAdpLinuxInit\n"));
|
---|
391 |
|
---|
392 | rc = vboxNetAdpInit();
|
---|
393 | if (RT_SUCCESS(rc))
|
---|
394 | {
|
---|
395 | rc = misc_register(&g_CtlDev);
|
---|
396 | if (rc)
|
---|
397 | {
|
---|
398 | printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc);
|
---|
399 | return rc;
|
---|
400 | }
|
---|
401 | LogRel(("VBoxNetAdp: Successfully started.\n"));
|
---|
402 | return 0;
|
---|
403 | }
|
---|
404 | else
|
---|
405 | LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc));
|
---|
406 | }
|
---|
407 | else
|
---|
408 | LogRel(("VBoxNetAdp: failed to initialize IPRT (rc=%d)\n", rc));
|
---|
409 |
|
---|
410 | return -RTErrConvertToErrno(rc);
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Unload the module.
|
---|
416 | *
|
---|
417 | * @todo We have to prevent this if we're busy!
|
---|
418 | */
|
---|
419 | static void __exit VBoxNetAdpLinuxUnload(void)
|
---|
420 | {
|
---|
421 | int rc;
|
---|
422 | Log(("VBoxNetAdpLinuxUnload\n"));
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Undo the work done during start (in reverse order).
|
---|
426 | */
|
---|
427 |
|
---|
428 | vboxNetAdpShutdown();
|
---|
429 | /* Remove control device */
|
---|
430 | rc = misc_deregister(&g_CtlDev);
|
---|
431 | if (rc < 0)
|
---|
432 | {
|
---|
433 | printk(KERN_ERR "misc_deregister failed with rc=%x\n", rc);
|
---|
434 | }
|
---|
435 |
|
---|
436 | RTR0Term();
|
---|
437 |
|
---|
438 | Log(("VBoxNetAdpLinuxUnload - done\n"));
|
---|
439 | }
|
---|
440 |
|
---|