VirtualBox

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

Last change on this file since 34079 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/* $Id: VBoxNetAdp-linux.c 33540 2010-10-28 09:27:05Z 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), VBOXNETADP_LINUX_NAME, vboxNetAdpNetDevInit);
185 if (pNetDev)
186 {
187 int err;
188
189 if (pNetDev->dev_addr)
190 {
191 memcpy(pNetDev->dev_addr, pMACAddress, ETH_ALEN);
192 Log2(("vboxNetAdpOsCreate: pNetDev->dev_addr = %.6Rhxd\n", pNetDev->dev_addr));
193 err = register_netdev(pNetDev);
194 if (!err)
195 {
196 strncpy(pThis->szName, pNetDev->name, sizeof(pThis->szName));
197 pThis->szName[sizeof(pThis->szName) - 1] = '\0';
198 pThis->u.s.pNetDev = pNetDev;
199 Log2(("vboxNetAdpOsCreate: pThis=%p pThis->szName = %p\n", pThis, pThis->szName));
200 return VINF_SUCCESS;
201 }
202 }
203 else
204 {
205 LogRel(("VBoxNetAdp: failed to set MAC address (dev->dev_addr == NULL)\n"));
206 err = EFAULT;
207 }
208 free_netdev(pNetDev);
209 rc = RTErrConvertFromErrno(err);
210 }
211 return rc;
212}
213
214void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
215{
216 struct net_device *pNetDev = pThis->u.s.pNetDev;
217 AssertPtr(pThis->u.s.pNetDev);
218
219 pThis->u.s.pNetDev = NULL;
220 unregister_netdev(pNetDev);
221 free_netdev(pNetDev);
222}
223
224/**
225 * Device open. Called on open /dev/vboxnetctl
226 *
227 * @param pInode Pointer to inode info structure.
228 * @param pFilp Associated file pointer.
229 */
230static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp)
231{
232 Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm));
233
234 /*
235 * Only root is allowed to access the device, enforce it!
236 */
237 if (!capable(CAP_SYS_ADMIN))
238 {
239 Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n"));
240 return -EPERM;
241 }
242
243 return 0;
244}
245
246
247/**
248 * Close device.
249 *
250 * @param pInode Pointer to inode info structure.
251 * @param pFilp Associated file pointer.
252 */
253static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp)
254{
255 Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n",
256 RTProcSelf(), current->pid, current->comm));
257 pFilp->private_data = NULL;
258 return 0;
259}
260
261/**
262 * Device I/O Control entry point.
263 *
264 * @param pFilp Associated file pointer.
265 * @param uCmd The function specified to ioctl().
266 * @param ulArg The argument specified to ioctl().
267 */
268#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
269static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp,
270 unsigned int uCmd, unsigned long ulArg)
271#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
272static long VBoxNetAdpLinuxIOCtlUnlocked(struct file *pFilp,
273 unsigned int uCmd, unsigned long ulArg)
274#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) */
275{
276 VBOXNETADPREQ Req;
277 PVBOXNETADP pAdp;
278 int rc;
279
280 Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", _IOC_SIZE(uCmd), uCmd, VBOXNETADP_CTL_ADD));
281 if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req))) /* paranoia */
282 {
283 Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), _IOC_SIZE(uCmd), uCmd));
284 return -EINVAL;
285 }
286
287 switch (uCmd)
288 {
289 case VBOXNETADP_CTL_ADD:
290 Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT));
291 rc = vboxNetAdpCreate(&pAdp);
292 if (RT_FAILURE(rc))
293 {
294 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpCreate -> %Rrc\n", rc));
295 return -EINVAL;
296 }
297
298 Assert(strlen(pAdp->szName) < sizeof(Req.szName));
299 strncpy(Req.szName, pAdp->szName, sizeof(Req.szName) - 1);
300 Req.szName[sizeof(Req.szName) - 1] = '\0';
301
302 if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req))))
303 {
304 /* this is really bad! */
305 /** @todo remove the adapter again? */
306 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#zx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd);
307 return -EFAULT;
308 }
309 Log(("VBoxNetAdpLinuxIOCtl: Successfully added '%s'\n", Req.szName));
310 break;
311
312 case VBOXNETADP_CTL_REMOVE:
313 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
314 {
315 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
316 return -EFAULT;
317 }
318 Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName));
319
320 pAdp = vboxNetAdpFindByName(Req.szName);
321 if (!pAdp)
322 {
323 Log(("VBoxNetAdpLinuxIOCtl: '%s' not found\n", Req.szName));
324 return -EINVAL;
325 }
326
327 rc = vboxNetAdpDestroy(pAdp);
328 if (RT_FAILURE(rc))
329 {
330 Log(("VBoxNetAdpLinuxIOCtl: vboxNetAdpDestroy('%s') -> %Rrc\n", Req.szName, rc));
331 return -EINVAL;
332 }
333 Log(("VBoxNetAdpLinuxIOCtl: Successfully removed '%s'\n", Req.szName));
334 break;
335
336 default:
337 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd);
338 return -EINVAL;
339 }
340
341 return 0;
342}
343
344int vboxNetAdpOsInit(PVBOXNETADP pThis)
345{
346 /*
347 * Init linux-specific members.
348 */
349 pThis->u.s.pNetDev = NULL;
350
351 return VINF_SUCCESS;
352}
353
354
355
356/**
357 * Initialize module.
358 *
359 * @returns appropriate status code.
360 */
361static int __init VBoxNetAdpLinuxInit(void)
362{
363 int rc;
364 /*
365 * Initialize IPRT.
366 */
367 rc = RTR0Init(0);
368 if (RT_SUCCESS(rc))
369 {
370 Log(("VBoxNetAdpLinuxInit\n"));
371
372 rc = vboxNetAdpInit();
373 if (RT_SUCCESS(rc))
374 {
375 rc = misc_register(&g_CtlDev);
376 if (rc)
377 {
378 printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc);
379 return rc;
380 }
381 LogRel(("VBoxNetAdp: Successfully started.\n"));
382 return 0;
383 }
384 else
385 LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc));
386 }
387 else
388 LogRel(("VBoxNetAdp: failed to initialize IPRT (rc=%d)\n", rc));
389
390 return -RTErrConvertToErrno(rc);
391}
392
393
394/**
395 * Unload the module.
396 *
397 * @todo We have to prevent this if we're busy!
398 */
399static void __exit VBoxNetAdpLinuxUnload(void)
400{
401 int rc;
402 Log(("VBoxNetAdpLinuxUnload\n"));
403
404 /*
405 * Undo the work done during start (in reverse order).
406 */
407
408 vboxNetAdpShutdown();
409 /* Remove control device */
410 rc = misc_deregister(&g_CtlDev);
411 if (rc < 0)
412 {
413 printk(KERN_ERR "misc_deregister failed with rc=%x\n", rc);
414 }
415
416 RTR0Term();
417
418 Log(("VBoxNetAdpLinuxUnload - done\n"));
419}
420
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