VirtualBox

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

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

*: scm cleanup run.

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