VirtualBox

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

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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

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