VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetAdp/linux/VBoxNetAdp-linux.c@ 60584

Last change on this file since 60584 was 60584, checked in by vboxsync, 9 years ago

Linux host/guest modules: include the revision into the Linux kernel modules (for modinfo MODULE.ko)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: VBoxNetAdp-linux.c 60584 2016-04-20 09:40:50Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), Linux Specific Code.
4 */
5
6/*
7 * Copyright (C) 2009-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "the-linux-kernel.h"
23#include "version-generated.h"
24#include "revision-generated.h"
25#include "product-generated.h"
26#include <linux/ethtool.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/miscdevice.h>
30
31#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
32#include <VBox/log.h>
33#include <VBox/err.h>
34#include <iprt/process.h>
35#include <iprt/initterm.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38
39/*
40#include <iprt/assert.h>
41#include <iprt/semaphore.h>
42#include <iprt/spinlock.h>
43#include <iprt/string.h>
44#include <iprt/uuid.h>
45#include <iprt/alloca.h>
46*/
47
48#define VBOXNETADP_OS_SPECFIC 1
49#include "../VBoxNetAdpInternal.h"
50
51
52/*********************************************************************************************************************************
53* Defined Constants And Macros *
54*********************************************************************************************************************************/
55#define VBOXNETADP_LINUX_NAME "vboxnet%d"
56#define VBOXNETADP_CTL_DEV_NAME "vboxnetctl"
57
58#define VBOXNETADP_FROM_IFACE(iface) ((PVBOXNETADP) ifnet_softc(iface))
59
60
61/*********************************************************************************************************************************
62* Internal Functions *
63*********************************************************************************************************************************/
64static int VBoxNetAdpLinuxInit(void);
65static void VBoxNetAdpLinuxUnload(void);
66
67static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp);
68static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp);
69#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
70static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
71 unsigned int uCmd, unsigned long ulArg);
72#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
73static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
74 unsigned int uCmd, unsigned long ulArg);
75#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
76
77static void vboxNetAdpEthGetDrvinfo(struct net_device *dev, struct ethtool_drvinfo *info);
78static int vboxNetAdpEthGetSettings(struct net_device *dev, struct ethtool_cmd *cmd);
79
80
81/*********************************************************************************************************************************
82* Global Variables *
83*********************************************************************************************************************************/
84module_init(VBoxNetAdpLinuxInit);
85module_exit(VBoxNetAdpLinuxUnload);
86
87MODULE_AUTHOR(VBOX_VENDOR);
88MODULE_DESCRIPTION(VBOX_PRODUCT " Network Adapter Driver");
89MODULE_LICENSE("GPL");
90#ifdef MODULE_VERSION
91MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV) " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
92#endif
93
94/**
95 * The (common) global data.
96 */
97static struct file_operations gFileOpsVBoxNetAdp =
98{
99 owner: THIS_MODULE,
100 open: VBoxNetAdpLinuxOpen,
101 release: VBoxNetAdpLinuxClose,
102#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
103 ioctl: VBoxNetAdpLinuxIOCtl,
104#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
105 unlocked_ioctl: VBoxNetAdpLinuxIOCtlUnlocked,
106#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
107};
108
109/** The miscdevice structure. */
110static struct miscdevice g_CtlDev =
111{
112 minor: MISC_DYNAMIC_MINOR,
113 name: VBOXNETADP_CTL_DEV_NAME,
114 fops: &gFileOpsVBoxNetAdp,
115# if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
116 devfs_name: VBOXNETADP_CTL_DEV_NAME
117# endif
118};
119
120static const struct ethtool_ops gEthToolOpsVBoxNetAdp =
121{
122 .get_drvinfo = vboxNetAdpEthGetDrvinfo,
123 .get_settings = vboxNetAdpEthGetSettings,
124 .get_link = ethtool_op_get_link,
125};
126
127
128struct VBoxNetAdpPriv
129{
130 struct net_device_stats Stats;
131};
132
133typedef struct VBoxNetAdpPriv VBOXNETADPPRIV;
134typedef VBOXNETADPPRIV *PVBOXNETADPPRIV;
135
136static int vboxNetAdpLinuxOpen(struct net_device *pNetDev)
137{
138 netif_start_queue(pNetDev);
139 return 0;
140}
141
142static int vboxNetAdpLinuxStop(struct net_device *pNetDev)
143{
144 netif_stop_queue(pNetDev);
145 return 0;
146}
147
148static int vboxNetAdpLinuxXmit(struct sk_buff *pSkb, struct net_device *pNetDev)
149{
150 PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
151
152 /* Update the stats. */
153 pPriv->Stats.tx_packets++;
154 pPriv->Stats.tx_bytes += pSkb->len;
155 /* Update transmission time stamp. */
156 pNetDev->trans_start = jiffies;
157 /* Nothing else to do, just free the sk_buff. */
158 dev_kfree_skb(pSkb);
159 return 0;
160}
161
162struct net_device_stats *vboxNetAdpLinuxGetStats(struct net_device *pNetDev)
163{
164 PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
165 return &pPriv->Stats;
166}
167
168
169/* ethtool_ops::get_drvinfo */
170static void vboxNetAdpEthGetDrvinfo(struct net_device *pNetDev, struct ethtool_drvinfo *info)
171{
172 PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
173 NOREF(pPriv);
174
175 RTStrPrintf(info->driver, sizeof(info->driver),
176 "%s", VBOXNETADP_NAME);
177
178 /*
179 * Would be nice to include VBOX_SVN_REV, but it's not available
180 * here. Use file's svn revision via svn keyword?
181 */
182 RTStrPrintf(info->version, sizeof(info->version),
183 "%s", VBOX_VERSION_STRING);
184
185 RTStrPrintf(info->fw_version, sizeof(info->fw_version),
186 "0x%08X", INTNETTRUNKIFPORT_VERSION);
187
188 RTStrPrintf(info->bus_info, sizeof(info->driver),
189 "N/A");
190}
191
192
193/* ethtool_ops::get_settings */
194static int vboxNetAdpEthGetSettings(struct net_device *pNetDev, struct ethtool_cmd *cmd)
195{
196 cmd->supported = 0;
197 cmd->advertising = 0;
198#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
199 ethtool_cmd_speed_set(cmd, SPEED_10);
200#else
201 cmd->speed = SPEED_10;
202#endif
203 cmd->duplex = DUPLEX_FULL;
204 cmd->port = PORT_TP;
205 cmd->phy_address = 0;
206 cmd->transceiver = XCVR_INTERNAL;
207 cmd->autoneg = AUTONEG_DISABLE;
208 cmd->maxtxpkt = 0;
209 cmd->maxrxpkt = 0;
210 return 0;
211}
212
213
214#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
215static const struct net_device_ops vboxNetAdpNetdevOps = {
216 .ndo_open = vboxNetAdpLinuxOpen,
217 .ndo_stop = vboxNetAdpLinuxStop,
218 .ndo_start_xmit = vboxNetAdpLinuxXmit,
219 .ndo_get_stats = vboxNetAdpLinuxGetStats
220};
221#endif
222
223static void vboxNetAdpNetDevInit(struct net_device *pNetDev)
224{
225 PVBOXNETADPPRIV pPriv;
226
227 ether_setup(pNetDev);
228#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
229 pNetDev->netdev_ops = &vboxNetAdpNetdevOps;
230#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
231 pNetDev->open = vboxNetAdpLinuxOpen;
232 pNetDev->stop = vboxNetAdpLinuxStop;
233 pNetDev->hard_start_xmit = vboxNetAdpLinuxXmit;
234 pNetDev->get_stats = vboxNetAdpLinuxGetStats;
235#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) */
236
237 pNetDev->ethtool_ops = &gEthToolOpsVBoxNetAdp;
238
239 pPriv = netdev_priv(pNetDev);
240 memset(pPriv, 0, sizeof(*pPriv));
241}
242
243
244int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMACAddress)
245{
246 int rc = VINF_SUCCESS;
247 struct net_device *pNetDev;
248
249 /* No need for private data. */
250 pNetDev = alloc_netdev(sizeof(VBOXNETADPPRIV),
251 pThis->szName[0] ? pThis->szName : VBOXNETADP_LINUX_NAME,
252#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
253 NET_NAME_UNKNOWN,
254#endif
255 vboxNetAdpNetDevInit);
256 if (pNetDev)
257 {
258 int err;
259
260 if (pNetDev->dev_addr)
261 {
262 memcpy(pNetDev->dev_addr, pMACAddress, ETH_ALEN);
263 Log2(("vboxNetAdpOsCreate: pNetDev->dev_addr = %.6Rhxd\n", pNetDev->dev_addr));
264
265 /*
266 * We treat presence of VBoxNetFlt filter as our "carrier",
267 * see vboxNetFltSetLinkState().
268 *
269 * operstates.txt: "On device allocation, networking core
270 * sets the flags equivalent to netif_carrier_ok() and
271 * !netif_dormant()" - so turn carrier off here.
272 */
273 netif_carrier_off(pNetDev);
274
275 err = register_netdev(pNetDev);
276 if (!err)
277 {
278 strncpy(pThis->szName, pNetDev->name, sizeof(pThis->szName));
279 pThis->szName[sizeof(pThis->szName) - 1] = '\0';
280 pThis->u.s.pNetDev = pNetDev;
281 Log2(("vboxNetAdpOsCreate: pThis=%p pThis->szName = %p\n", pThis, pThis->szName));
282 return VINF_SUCCESS;
283 }
284 }
285 else
286 {
287 LogRel(("VBoxNetAdp: failed to set MAC address (dev->dev_addr == NULL)\n"));
288 err = EFAULT;
289 }
290 free_netdev(pNetDev);
291 rc = RTErrConvertFromErrno(err);
292 }
293 return rc;
294}
295
296void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
297{
298 struct net_device *pNetDev = pThis->u.s.pNetDev;
299 AssertPtr(pThis->u.s.pNetDev);
300
301 pThis->u.s.pNetDev = NULL;
302 unregister_netdev(pNetDev);
303 free_netdev(pNetDev);
304}
305
306/**
307 * Device open. Called on open /dev/vboxnetctl
308 *
309 * @param pInode Pointer to inode info structure.
310 * @param pFilp Associated file pointer.
311 */
312static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp)
313{
314 Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm));
315
316#ifdef VBOX_WITH_HARDENING
317 /*
318 * Only root is allowed to access the device, enforce it!
319 */
320 if (!capable(CAP_SYS_ADMIN))
321 {
322 Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n"));
323 return -EPERM;
324 }
325#endif
326
327 return 0;
328}
329
330
331/**
332 * Close device.
333 *
334 * @param pInode Pointer to inode info structure.
335 * @param pFilp Associated file pointer.
336 */
337static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp)
338{
339 Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n",
340 RTProcSelf(), current->pid, current->comm));
341 pFilp->private_data = NULL;
342 return 0;
343}
344
345/**
346 * Device I/O Control entry point.
347 *
348 * @param pFilp Associated file pointer.
349 * @param uCmd The function specified to ioctl().
350 * @param ulArg The argument specified to ioctl().
351 */
352#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
353static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
354 unsigned int uCmd, unsigned long ulArg)
355#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
356static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
357 unsigned int uCmd, unsigned long ulArg)
358#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
359{
360 VBOXNETADPREQ Req;
361 PVBOXNETADP pAdp;
362 int rc;
363 char *pszName = NULL;
364
365 Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", _IOC_SIZE(uCmd), uCmd, VBOXNETADP_CTL_ADD));
366 if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req))) /* paranoia */
367 {
368 Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), _IOC_SIZE(uCmd), uCmd));
369 return -EINVAL;
370 }
371
372 switch (uCmd)
373 {
374 case VBOXNETADP_CTL_ADD:
375 Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT));
376 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
377 {
378 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
379 return -EFAULT;
380 }
381 Log(("VBoxNetAdpLinuxIOCtl: Add %s\n", Req.szName));
382
383 if (Req.szName[0])
384 {
385 pAdp = vboxNetAdpFindByName(Req.szName);
386 if (pAdp)
387 {
388 Log(("VBoxNetAdpLinuxIOCtl: '%s' already exists\n", Req.szName));
389 return -EINVAL;
390 }
391 pszName = Req.szName;
392 }
393 rc = vboxNetAdpCreate(&pAdp, pszName);
394 if (RT_FAILURE(rc))
395 {
396 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpCreate -> %Rrc\n", rc));
397 return -(rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL);
398 }
399
400 Assert(strlen(pAdp->szName) < sizeof(Req.szName));
401 strncpy(Req.szName, pAdp->szName, sizeof(Req.szName) - 1);
402 Req.szName[sizeof(Req.szName) - 1] = '\0';
403
404 if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req))))
405 {
406 /* this is really bad! */
407 /** @todo remove the adapter again? */
408 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#zx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd);
409 return -EFAULT;
410 }
411 Log(("VBoxNetAdpLinuxIOCtl: Successfully added '%s'\n", Req.szName));
412 break;
413
414 case VBOXNETADP_CTL_REMOVE:
415 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
416 {
417 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
418 return -EFAULT;
419 }
420 Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName));
421
422 pAdp = vboxNetAdpFindByName(Req.szName);
423 if (!pAdp)
424 {
425 Log(("VBoxNetAdpLinuxIOCtl: '%s' not found\n", Req.szName));
426 return -EINVAL;
427 }
428
429 rc = vboxNetAdpDestroy(pAdp);
430 if (RT_FAILURE(rc))
431 {
432 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpDestroy('%s') -> %Rrc\n", Req.szName, rc));
433 return -EINVAL;
434 }
435 Log(("VBoxNetAdpLinuxIOCtl: Successfully removed '%s'\n", Req.szName));
436 break;
437
438 default:
439 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd);
440 return -EINVAL;
441 }
442
443 return 0;
444}
445
446int vboxNetAdpOsInit(PVBOXNETADP pThis)
447{
448 /*
449 * Init linux-specific members.
450 */
451 pThis->u.s.pNetDev = NULL;
452
453 return VINF_SUCCESS;
454}
455
456
457
458/**
459 * Initialize module.
460 *
461 * @returns appropriate status code.
462 */
463static int __init VBoxNetAdpLinuxInit(void)
464{
465 int rc;
466 /*
467 * Initialize IPRT.
468 */
469 rc = RTR0Init(0);
470 if (RT_SUCCESS(rc))
471 {
472 Log(("VBoxNetAdpLinuxInit\n"));
473
474 rc = vboxNetAdpInit();
475 if (RT_SUCCESS(rc))
476 {
477 rc = misc_register(&g_CtlDev);
478 if (rc)
479 {
480 printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc);
481 return rc;
482 }
483 LogRel(("VBoxNetAdp: Successfully started.\n"));
484 return 0;
485 }
486 else
487 LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc));
488 }
489 else
490 LogRel(("VBoxNetAdp: failed to initialize IPRT (rc=%d)\n", rc));
491
492 return -RTErrConvertToErrno(rc);
493}
494
495
496/**
497 * Unload the module.
498 *
499 * @todo We have to prevent this if we're busy!
500 */
501static void __exit VBoxNetAdpLinuxUnload(void)
502{
503 Log(("VBoxNetAdpLinuxUnload\n"));
504
505 /*
506 * Undo the work done during start (in reverse order).
507 */
508
509 vboxNetAdpShutdown();
510 /* Remove control device */
511 misc_deregister(&g_CtlDev);
512
513 RTR0Term();
514
515 Log(("VBoxNetAdpLinuxUnload - done\n"));
516}
517
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette