VirtualBox

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

Last change on this file since 42914 was 41272, checked in by vboxsync, 13 years ago

Network: Increased the limit of host-only interfaces to 128.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
1/* $Id: VBoxNetAdp-linux.c 41272 2012-05-14 09:02:00Z 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*******************************************************************************/
58static int VBoxNetAdpLinuxInit(void);
59static void VBoxNetAdpLinuxUnload(void);
60
61static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp);
62static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp);
63#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
64static 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) */
67static 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*******************************************************************************/
74module_init(VBoxNetAdpLinuxInit);
75module_exit(VBoxNetAdpLinuxUnload);
76
77MODULE_AUTHOR(VBOX_VENDOR);
78MODULE_DESCRIPTION(VBOX_PRODUCT " Network Adapter Driver");
79MODULE_LICENSE("GPL");
80#ifdef MODULE_VERSION
81MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(INTNETTRUNKIFPORT_VERSION) ")");
82#endif
83
84/**
85 * The (common) global data.
86 */
87static 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. */
100static 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
110struct VBoxNetAdpPriv
111{
112 struct net_device_stats Stats;
113};
114
115typedef struct VBoxNetAdpPriv VBOXNETADPPRIV;
116typedef VBOXNETADPPRIV *PVBOXNETADPPRIV;
117
118static int vboxNetAdpLinuxOpen(struct net_device *pNetDev)
119{
120 netif_start_queue(pNetDev);
121 return 0;
122}
123
124static int vboxNetAdpLinuxStop(struct net_device *pNetDev)
125{
126 netif_stop_queue(pNetDev);
127 return 0;
128}
129
130static 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
144struct 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)
151static 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
159static 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
178int 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
216void 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 */
232static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp)
233{
234 Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm));
235
236#ifdef VBOX_WITH_HARDENING
237 /*
238 * Only root is allowed to access the device, enforce it!
239 */
240 if (!capable(CAP_SYS_ADMIN))
241 {
242 Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n"));
243 return -EPERM;
244 }
245#endif
246
247 return 0;
248}
249
250
251/**
252 * Close device.
253 *
254 * @param pInode Pointer to inode info structure.
255 * @param pFilp Associated file pointer.
256 */
257static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp)
258{
259 Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n",
260 RTProcSelf(), current->pid, current->comm));
261 pFilp->private_data = NULL;
262 return 0;
263}
264
265/**
266 * Device I/O Control entry point.
267 *
268 * @param pFilp Associated file pointer.
269 * @param uCmd The function specified to ioctl().
270 * @param ulArg The argument specified to ioctl().
271 */
272#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
273static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
274 unsigned int uCmd, unsigned long ulArg)
275#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
276static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
277 unsigned int uCmd, unsigned long ulArg)
278#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
279{
280 VBOXNETADPREQ Req;
281 PVBOXNETADP pAdp;
282 int rc;
283 char *pszName = NULL;
284
285 Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", _IOC_SIZE(uCmd), uCmd, VBOXNETADP_CTL_ADD));
286 if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req))) /* paranoia */
287 {
288 Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), _IOC_SIZE(uCmd), uCmd));
289 return -EINVAL;
290 }
291
292 switch (uCmd)
293 {
294 case VBOXNETADP_CTL_ADD:
295 Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT));
296 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
297 {
298 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
299 return -EFAULT;
300 }
301 Log(("VBoxNetAdpLinuxIOCtl: Add %s\n", Req.szName));
302
303 if (Req.szName[0])
304 {
305 pAdp = vboxNetAdpFindByName(Req.szName);
306 if (pAdp)
307 {
308 Log(("VBoxNetAdpLinuxIOCtl: '%s' already exists\n", Req.szName));
309 return -EINVAL;
310 }
311 pszName = Req.szName;
312 }
313 rc = vboxNetAdpCreate(&pAdp, pszName);
314 if (RT_FAILURE(rc))
315 {
316 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpCreate -> %Rrc\n", rc));
317 return -(rc == VERR_OUT_OF_RESOURCES ? ENOMEM : EINVAL);
318 }
319
320 Assert(strlen(pAdp->szName) < sizeof(Req.szName));
321 strncpy(Req.szName, pAdp->szName, sizeof(Req.szName) - 1);
322 Req.szName[sizeof(Req.szName) - 1] = '\0';
323
324 if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req))))
325 {
326 /* this is really bad! */
327 /** @todo remove the adapter again? */
328 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#zx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd);
329 return -EFAULT;
330 }
331 Log(("VBoxNetAdpLinuxIOCtl: Successfully added '%s'\n", Req.szName));
332 break;
333
334 case VBOXNETADP_CTL_REMOVE:
335 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
336 {
337 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
338 return -EFAULT;
339 }
340 Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName));
341
342 pAdp = vboxNetAdpFindByName(Req.szName);
343 if (!pAdp)
344 {
345 Log(("VBoxNetAdpLinuxIOCtl: '%s' not found\n", Req.szName));
346 return -EINVAL;
347 }
348
349 rc = vboxNetAdpDestroy(pAdp);
350 if (RT_FAILURE(rc))
351 {
352 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpDestroy('%s') -> %Rrc\n", Req.szName, rc));
353 return -EINVAL;
354 }
355 Log(("VBoxNetAdpLinuxIOCtl: Successfully removed '%s'\n", Req.szName));
356 break;
357
358 default:
359 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd);
360 return -EINVAL;
361 }
362
363 return 0;
364}
365
366int vboxNetAdpOsInit(PVBOXNETADP pThis)
367{
368 /*
369 * Init linux-specific members.
370 */
371 pThis->u.s.pNetDev = NULL;
372
373 return VINF_SUCCESS;
374}
375
376
377
378/**
379 * Initialize module.
380 *
381 * @returns appropriate status code.
382 */
383static int __init VBoxNetAdpLinuxInit(void)
384{
385 int rc;
386 /*
387 * Initialize IPRT.
388 */
389 rc = RTR0Init(0);
390 if (RT_SUCCESS(rc))
391 {
392 Log(("VBoxNetAdpLinuxInit\n"));
393
394 rc = vboxNetAdpInit();
395 if (RT_SUCCESS(rc))
396 {
397 rc = misc_register(&g_CtlDev);
398 if (rc)
399 {
400 printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc);
401 return rc;
402 }
403 LogRel(("VBoxNetAdp: Successfully started.\n"));
404 return 0;
405 }
406 else
407 LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc));
408 }
409 else
410 LogRel(("VBoxNetAdp: failed to initialize IPRT (rc=%d)\n", rc));
411
412 return -RTErrConvertToErrno(rc);
413}
414
415
416/**
417 * Unload the module.
418 *
419 * @todo We have to prevent this if we're busy!
420 */
421static void __exit VBoxNetAdpLinuxUnload(void)
422{
423 int rc;
424 Log(("VBoxNetAdpLinuxUnload\n"));
425
426 /*
427 * Undo the work done during start (in reverse order).
428 */
429
430 vboxNetAdpShutdown();
431 /* Remove control device */
432 rc = misc_deregister(&g_CtlDev);
433 if (rc < 0)
434 {
435 printk(KERN_ERR "misc_deregister failed with rc=%x\n", rc);
436 }
437
438 RTR0Term();
439
440 Log(("VBoxNetAdpLinuxUnload - done\n"));
441}
442
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