VirtualBox

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

Last change on this file since 20750 was 20691, checked in by vboxsync, 16 years ago

VBoxNetAdp-linux.c: removed warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1/* $Id: VBoxNetAdp-linux.c 20691 2009-06-18 13:48:46Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), Linux Specific Code.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include "the-linux-kernel.h"
26#include "version-generated.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
38/*
39#include <iprt/assert.h>
40#include <iprt/semaphore.h>
41#include <iprt/spinlock.h>
42#include <iprt/string.h>
43#include <iprt/uuid.h>
44#include <iprt/alloca.h>
45*/
46
47#define VBOXNETADP_OS_SPECFIC 1
48#include "../VBoxNetAdpInternal.h"
49
50/*******************************************************************************
51* Defined Constants And Macros *
52*******************************************************************************/
53#define VBOXNETADP_LINUX_NAME "vboxnet%d"
54#define VBOXNETADP_CTL_DEV_NAME "vboxnetctl"
55
56/* debug printf */
57#define OSDBGPRINT(a) printk a
58
59#define VBOXNETADP_FROM_IFACE(iface) ((PVBOXNETADP) ifnet_softc(iface))
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);
69static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
70
71/*******************************************************************************
72* Global Variables *
73*******************************************************************************/
74#ifdef RT_ARCH_AMD64
75/**
76 * Memory for the executable memory heap (in IPRT).
77 */
78extern uint8_t g_abExecMemory[4096]; /* cannot donate less than one page */
79__asm__(".section execmemory, \"awx\", @progbits\n\t"
80 ".align 32\n\t"
81 ".globl g_abExecMemory\n"
82 "g_abExecMemory:\n\t"
83 ".zero 4096\n\t"
84 ".type g_abExecMemory, @object\n\t"
85 ".size g_abExecMemory, 4096\n\t"
86 ".text\n\t");
87#endif
88
89module_init(VBoxNetAdpLinuxInit);
90module_exit(VBoxNetAdpLinuxUnload);
91
92MODULE_AUTHOR("Sun Microsystems, Inc.");
93MODULE_DESCRIPTION("VirtualBox Network Adapter Driver");
94MODULE_LICENSE("GPL");
95#ifdef MODULE_VERSION
96# define xstr(s) str(s)
97# define str(s) #s
98MODULE_VERSION(VBOX_VERSION_STRING " (" xstr(INTNETTRUNKIFPORT_VERSION) ")");
99#endif
100
101/**
102 * The (common) global data.
103 */
104static struct file_operations gFileOpsVBoxNetAdp =
105{
106 owner: THIS_MODULE,
107 open: VBoxNetAdpLinuxOpen,
108 release: VBoxNetAdpLinuxClose,
109 ioctl: VBoxNetAdpLinuxIOCtl,
110};
111
112/** The miscdevice structure. */
113static struct miscdevice g_CtlDev =
114{
115 minor: MISC_DYNAMIC_MINOR,
116 name: VBOXNETADP_CTL_DEV_NAME,
117 fops: &gFileOpsVBoxNetAdp,
118# if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
119 devfs_name: VBOXNETADP_CTL_DEV_NAME
120# endif
121};
122
123struct VBoxNetAdpPriv
124{
125 struct net_device_stats Stats;
126};
127
128typedef struct VBoxNetAdpPriv VBOXNETADPPRIV;
129typedef VBOXNETADPPRIV *PVBOXNETADPPRIV;
130
131static int vboxNetAdpLinuxOpen(struct net_device *pNetDev)
132{
133 netif_start_queue(pNetDev);
134 printk("vboxNetAdpOpen returns 0\n");
135 return 0;
136}
137
138static int vboxNetAdpLinuxStop(struct net_device *pNetDev)
139{
140 netif_stop_queue(pNetDev);
141 return 0;
142}
143
144static int vboxNetAdpLinuxXmit(struct sk_buff *pSkb, struct net_device *pNetDev)
145{
146 PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
147
148 /* Update the stats. */
149 pPriv->Stats.tx_packets++;
150 pPriv->Stats.tx_bytes += pSkb->len;
151 /* Update transmission time stamp. */
152 pNetDev->trans_start = jiffies;
153 /* Nothing else to do, just free the sk_buff. */
154 dev_kfree_skb(pSkb);
155 return 0;
156}
157
158struct net_device_stats *vboxNetAdpLinuxGetStats(struct net_device *pNetDev)
159{
160 PVBOXNETADPPRIV pPriv = netdev_priv(pNetDev);
161 return &pPriv->Stats;
162}
163
164static void vboxNetAdpNetDevInit(struct net_device *pNetDev)
165{
166 PVBOXNETADPPRIV pPriv;
167
168 ether_setup(pNetDev);
169 pNetDev->open = vboxNetAdpLinuxOpen;
170 pNetDev->stop = vboxNetAdpLinuxStop;
171 pNetDev->hard_start_xmit = vboxNetAdpLinuxXmit;
172 pNetDev->get_stats = vboxNetAdpLinuxGetStats;
173
174 pPriv = netdev_priv(pNetDev);
175 memset(pPriv, 0, sizeof(*pPriv));
176}
177
178
179int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMACAddress)
180{
181 int rc = VINF_SUCCESS;
182 struct net_device *pNetDev;
183
184 /* No need for private data. */
185 pNetDev = alloc_netdev(sizeof(VBOXNETADPPRIV), VBOXNETADP_LINUX_NAME, vboxNetAdpNetDevInit);
186 if (pNetDev)
187 {
188 int err;
189
190 memcpy(pNetDev->dev_addr, pMACAddress, ETH_ALEN);
191 Log2(("vboxNetAdpOsCreate: pNetDev->dev_addr = %.6Rhxd\n", pNetDev->dev_addr));
192 err = register_netdev(pNetDev);
193 if (!err)
194 {
195 strncpy(pThis->szName, pNetDev->name, VBOXNETADP_MAX_NAME_LEN);
196 pThis->u.s.pNetDev = pNetDev;
197 Log2(("vboxNetAdpOsCreate: pThis=%p pThis->szName = %p\n", pThis, pThis->szName));
198 return VINF_SUCCESS;
199 }
200 free_netdev(pNetDev);
201 rc = RTErrConvertFromErrno(err);
202 }
203 return rc;
204}
205
206void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
207{
208 struct net_device *pNetDev = pThis->u.s.pNetDev;
209 AssertPtr(pThis->u.s.pNetDev);
210
211 pThis->u.s.pNetDev = NULL;
212 unregister_netdev(pNetDev);
213 free_netdev(pNetDev);
214}
215
216/**
217 * Device open. Called on open /dev/vboxnetctl
218 *
219 * @param pInode Pointer to inode info structure.
220 * @param pFilp Associated file pointer.
221 */
222static int VBoxNetAdpLinuxOpen(struct inode *pInode, struct file *pFilp)
223{
224 Log(("VBoxNetAdpLinuxOpen: pid=%d/%d %s\n", RTProcSelf(), current->pid, current->comm));
225
226 /*
227 * Only root is allowed to access the device, enforce it!
228 */
229 if (!capable(CAP_SYS_ADMIN))
230 {
231 Log(("VBoxNetAdpLinuxOpen: admin privileges required!\n"));
232 return -EPERM;
233 }
234
235 return 0;
236}
237
238
239/**
240 * Close device.
241 *
242 * @param pInode Pointer to inode info structure.
243 * @param pFilp Associated file pointer.
244 */
245static int VBoxNetAdpLinuxClose(struct inode *pInode, struct file *pFilp)
246{
247 Log(("VBoxNetAdpLinuxClose: pid=%d/%d %s\n",
248 RTProcSelf(), current->pid, current->comm));
249 pFilp->private_data = NULL;
250 return 0;
251}
252
253/**
254 * Device I/O Control entry point.
255 *
256 * @param pFilp Associated file pointer.
257 * @param uCmd The function specified to ioctl().
258 * @param ulArg The argument specified to ioctl().
259 */
260static int VBoxNetAdpLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
261{
262 VBOXNETADPREQ Req;
263 PVBOXNETADP pAdp = NULL;
264 int rc = VINF_SUCCESS;
265 uint32_t cbReq = _IOC_SIZE(uCmd);
266
267 Log(("VBoxNetAdpLinuxIOCtl: param len %#x; uCmd=%#x; add=%#x\n", cbReq, uCmd, VBOXNETADP_CTL_ADD));
268 if (RT_UNLIKELY(_IOC_SIZE(uCmd) != sizeof(Req)))
269 {
270 Log(("VBoxNetAdpLinuxIOCtl: bad ioctl sizeof(Req)=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", sizeof(Req), cbReq, uCmd));
271 return -EINVAL;
272 }
273 switch (uCmd)
274 {
275 case VBOXNETADP_CTL_ADD:
276 Log(("VBoxNetAdpLinuxIOCtl: _IOC_DIR(uCmd)=%#x; IOC_OUT=%#x\n", _IOC_DIR(uCmd), IOC_OUT));
277 if (uCmd & IOC_OUT)
278 {
279 rc = vboxNetAdpCreate(&pAdp);
280 if (RT_SUCCESS(rc))
281 {
282 if (cbReq < sizeof(VBOXNETADPREQ))
283 {
284 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: param len %#x < req size %#lx; uCmd=%#x\n", cbReq, sizeof(VBOXNETADPREQ), uCmd);
285 return -EINVAL;
286 }
287 strncpy(Req.szName, pAdp->szName, sizeof(Req.szName));
288 if (RT_UNLIKELY(copy_to_user((void *)ulArg, &Req, sizeof(Req))))
289 {
290 /* this is really bad! */
291 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: copy_to_user(%#lx,,%#lx); uCmd=%#x!\n", ulArg, sizeof(Req), uCmd);
292 rc = -EFAULT;
293 }
294 }
295 }
296 break;
297
298 case VBOXNETADP_CTL_REMOVE:
299 if (RT_UNLIKELY(copy_from_user(&Req, (void *)ulArg, sizeof(Req))))
300 {
301 Log(("VBoxNetAdpLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
302 return -EFAULT;
303 }
304 Log(("VBoxNetAdpLinuxIOCtl: Remove %s\n", Req.szName));
305 pAdp = vboxNetAdpFindByName(Req.szName);
306 if (pAdp)
307 rc = vboxNetAdpDestroy(pAdp);
308 else
309 rc = VERR_NOT_FOUND;
310 break;
311
312 default:
313 printk(KERN_ERR "VBoxNetAdpLinuxIOCtl: unknown command %x.\n", uCmd);
314 rc = VERR_INVALID_PARAMETER;
315 break;
316 }
317
318 Log(("VBoxNetAdpLinuxIOCtl: rc=%Vrc\n", rc));
319 return RT_SUCCESS(rc) ? 0 : -EINVAL;
320}
321
322int vboxNetAdpOsInit(PVBOXNETADP pThis)
323{
324 /*
325 * Init linux-specific members.
326 */
327 pThis->u.s.pNetDev = NULL;
328
329 return VINF_SUCCESS;
330}
331
332
333
334/**
335 * Initialize module.
336 *
337 * @returns appropriate status code.
338 */
339static int __init VBoxNetAdpLinuxInit(void)
340{
341 int rc;
342 /*
343 * Initialize IPRT.
344 */
345 rc = RTR0Init(0);
346 if (RT_SUCCESS(rc))
347 {
348#ifdef RT_ARCH_AMD64
349 rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
350 printk(KERN_DEBUG "VBoxNetAdp: dbg - g_abExecMemory=%p\n", (void *)&g_abExecMemory[0]);
351 if (RT_FAILURE(rc))
352 {
353 printk(KERN_WARNING "VBoxNetAdp: failed to donate exec memory, no logging will be available.\n");
354 }
355#endif
356 Log(("VBoxNetAdpLinuxInit\n"));
357
358 rc = vboxNetAdpInit();
359 if (RT_SUCCESS(rc))
360 {
361 rc = misc_register(&g_CtlDev);
362 if (rc)
363 {
364 printk(KERN_ERR "VBoxNetAdp: Can't register " VBOXNETADP_CTL_DEV_NAME " device! rc=%d\n", rc);
365 return rc;
366 }
367 LogRel(("VBoxNetAdp: Successfully started.\n"));
368 return 0;
369 }
370 else
371 LogRel(("VBoxNetAdp: failed to register vboxnet0 device (rc=%d)\n", rc));
372 }
373 else
374 LogRel(("VBoxNetFlt: failed to initialize IPRT (rc=%d)\n", rc));
375
376 return -RTErrConvertToErrno(rc);
377}
378
379
380/**
381 * Unload the module.
382 *
383 * @todo We have to prevent this if we're busy!
384 */
385static void __exit VBoxNetAdpLinuxUnload(void)
386{
387 int rc;
388 Log(("VBoxNetFltLinuxUnload\n"));
389
390 /*
391 * Undo the work done during start (in reverse order).
392 */
393
394 vboxNetAdpShutdown();
395 /* Remove control device */
396 rc = misc_deregister(&g_CtlDev);
397 if (rc < 0)
398 {
399 printk(KERN_ERR "misc_deregister failed with rc=%x\n", rc);
400 }
401
402 RTR0Term();
403
404 Log(("VBoxNetFltLinuxUnload - done\n"));
405}
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