blob: e354501ab29792317ecdc7a5c0435f6db6270008 [file] [log] [blame]
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001#include <linux/etherdevice.h>
2#include <linux/if_macvlan.h>
3#include <linux/interrupt.h>
4#include <linux/nsproxy.h>
5#include <linux/compat.h>
6#include <linux/if_tun.h>
7#include <linux/module.h>
8#include <linux/skbuff.h>
9#include <linux/cache.h>
10#include <linux/sched.h>
11#include <linux/types.h>
12#include <linux/init.h>
13#include <linux/wait.h>
14#include <linux/cdev.h>
15#include <linux/fs.h>
16
17#include <net/net_namespace.h>
18#include <net/rtnetlink.h>
19#include <net/sock.h>
20
21/*
22 * A macvtap queue is the central object of this driver, it connects
23 * an open character device to a macvlan interface. There can be
24 * multiple queues on one interface, which map back to queues
25 * implemented in hardware on the underlying device.
26 *
27 * macvtap_proto is used to allocate queues through the sock allocation
28 * mechanism.
29 *
30 * TODO: multiqueue support is currently not implemented, even though
31 * macvtap is basically prepared for that. We will need to add this
32 * here as well as in virtio-net and qemu to get line rate on 10gbit
33 * adapters from a guest.
34 */
35struct macvtap_queue {
36 struct sock sk;
37 struct socket sock;
38 struct macvlan_dev *vlan;
39 struct file *file;
40};
41
42static struct proto macvtap_proto = {
43 .name = "macvtap",
44 .owner = THIS_MODULE,
45 .obj_size = sizeof (struct macvtap_queue),
46};
47
48/*
49 * Minor number matches netdev->ifindex, so need a potentially
50 * large value. This also makes it possible to split the
51 * tap functionality out again in the future by offering it
52 * from other drivers besides macvtap. As long as every device
53 * only has one tap, the interface numbers assure that the
54 * device nodes are unique.
55 */
56static unsigned int macvtap_major;
57#define MACVTAP_NUM_DEVS 65536
58static struct class *macvtap_class;
59static struct cdev macvtap_cdev;
60
Arnd Bergmann501c7742010-02-18 05:46:50 +000061static const struct proto_ops macvtap_socket_ops;
62
Arnd Bergmann20d29d72010-01-30 12:24:26 +000063/*
64 * RCU usage:
Arnd Bergmann02df55d2010-02-18 05:45:36 +000065 * The macvtap_queue and the macvlan_dev are loosely coupled, the
66 * pointers from one to the other can only be read while rcu_read_lock
67 * or macvtap_lock is held.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000068 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +000069 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
70 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
71 * q->vlan becomes inaccessible. When the files gets closed,
72 * macvtap_get_queue() fails.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000073 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +000074 * There may still be references to the struct sock inside of the
75 * queue from outbound SKBs, but these never reference back to the
76 * file or the dev. The data structure is freed through __sk_free
77 * when both our references and any pending SKBs are gone.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000078 */
79static DEFINE_SPINLOCK(macvtap_lock);
80
81/*
82 * Choose the next free queue, for now there is only one
83 */
84static int macvtap_set_queue(struct net_device *dev, struct file *file,
85 struct macvtap_queue *q)
86{
87 struct macvlan_dev *vlan = netdev_priv(dev);
88 int err = -EBUSY;
89
90 spin_lock(&macvtap_lock);
91 if (rcu_dereference(vlan->tap))
92 goto out;
93
94 err = 0;
Arnd Bergmann02df55d2010-02-18 05:45:36 +000095 rcu_assign_pointer(q->vlan, vlan);
Arnd Bergmann20d29d72010-01-30 12:24:26 +000096 rcu_assign_pointer(vlan->tap, q);
Arnd Bergmann02df55d2010-02-18 05:45:36 +000097 sock_hold(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +000098
99 q->file = file;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000100 file->private_data = q;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000101
102out:
103 spin_unlock(&macvtap_lock);
104 return err;
105}
106
107/*
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000108 * The file owning the queue got closed, give up both
109 * the reference that the files holds as well as the
110 * one from the macvlan_dev if that still exists.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000111 *
112 * Using the spinlock makes sure that we don't get
113 * to the queue again after destroying it.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000114 */
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000115static void macvtap_put_queue(struct macvtap_queue *q)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000116{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000117 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000118
119 spin_lock(&macvtap_lock);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000120 vlan = rcu_dereference(q->vlan);
121 if (vlan) {
122 rcu_assign_pointer(vlan->tap, NULL);
123 rcu_assign_pointer(q->vlan, NULL);
124 sock_put(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000125 }
126
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000127 spin_unlock(&macvtap_lock);
128
129 synchronize_rcu();
130 sock_put(&q->sk);
131}
132
133/*
134 * Since we only support one queue, just dereference the pointer.
135 */
136static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
137 struct sk_buff *skb)
138{
139 struct macvlan_dev *vlan = netdev_priv(dev);
140
141 return rcu_dereference(vlan->tap);
142}
143
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000144/*
145 * The net_device is going away, give up the reference
146 * that it holds on the queue (all the queues one day)
147 * and safely set the pointer from the queues to NULL.
148 */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000149static void macvtap_del_queues(struct net_device *dev)
150{
151 struct macvlan_dev *vlan = netdev_priv(dev);
Arnd Bergmann564517e2010-02-11 05:55:39 +0000152 struct macvtap_queue *q;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000153
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000154 spin_lock(&macvtap_lock);
155 q = rcu_dereference(vlan->tap);
156 if (!q) {
157 spin_unlock(&macvtap_lock);
158 return;
159 }
160
161 rcu_assign_pointer(vlan->tap, NULL);
162 rcu_assign_pointer(q->vlan, NULL);
163 spin_unlock(&macvtap_lock);
164
165 synchronize_rcu();
Arnd Bergmann564517e2010-02-11 05:55:39 +0000166 sock_put(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000167}
168
169/*
170 * Forward happens for data that gets sent from one macvlan
171 * endpoint to another one in bridge mode. We just take
172 * the skb and put it into the receive queue.
173 */
174static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
175{
176 struct macvtap_queue *q = macvtap_get_queue(dev, skb);
177 if (!q)
178 return -ENOLINK;
179
180 skb_queue_tail(&q->sk.sk_receive_queue, skb);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000181 wake_up_interruptible_poll(q->sk.sk_sleep, POLLIN | POLLRDNORM | POLLRDBAND);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000182 return 0;
183}
184
185/*
186 * Receive is for data from the external interface (lowerdev),
187 * in case of macvtap, we can treat that the same way as
188 * forward, which macvlan cannot.
189 */
190static int macvtap_receive(struct sk_buff *skb)
191{
192 skb_push(skb, ETH_HLEN);
193 return macvtap_forward(skb->dev, skb);
194}
195
196static int macvtap_newlink(struct net *src_net,
197 struct net_device *dev,
198 struct nlattr *tb[],
199 struct nlattr *data[])
200{
201 struct device *classdev;
202 dev_t devt;
203 int err;
204
205 err = macvlan_common_newlink(src_net, dev, tb, data,
206 macvtap_receive, macvtap_forward);
207 if (err)
208 goto out;
209
210 devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
211
212 classdev = device_create(macvtap_class, &dev->dev, devt,
213 dev, "tap%d", dev->ifindex);
214 if (IS_ERR(classdev)) {
215 err = PTR_ERR(classdev);
216 macvtap_del_queues(dev);
217 }
218
219out:
220 return err;
221}
222
223static void macvtap_dellink(struct net_device *dev,
224 struct list_head *head)
225{
226 device_destroy(macvtap_class,
227 MKDEV(MAJOR(macvtap_major), dev->ifindex));
228
229 macvtap_del_queues(dev);
230 macvlan_dellink(dev, head);
231}
232
233static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
234 .kind = "macvtap",
235 .newlink = macvtap_newlink,
236 .dellink = macvtap_dellink,
237};
238
239
240static void macvtap_sock_write_space(struct sock *sk)
241{
242 if (!sock_writeable(sk) ||
243 !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
244 return;
245
246 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
Arnd Bergmann501c7742010-02-18 05:46:50 +0000247 wake_up_interruptible_poll(sk->sk_sleep, POLLOUT | POLLWRNORM | POLLWRBAND);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000248}
249
250static int macvtap_open(struct inode *inode, struct file *file)
251{
252 struct net *net = current->nsproxy->net_ns;
253 struct net_device *dev = dev_get_by_index(net, iminor(inode));
254 struct macvtap_queue *q;
255 int err;
256
257 err = -ENODEV;
258 if (!dev)
259 goto out;
260
261 /* check if this is a macvtap device */
262 err = -EINVAL;
263 if (dev->rtnl_link_ops != &macvtap_link_ops)
264 goto out;
265
266 err = -ENOMEM;
267 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
268 &macvtap_proto);
269 if (!q)
270 goto out;
271
272 init_waitqueue_head(&q->sock.wait);
273 q->sock.type = SOCK_RAW;
274 q->sock.state = SS_CONNECTED;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000275 q->sock.file = file;
276 q->sock.ops = &macvtap_socket_ops;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000277 sock_init_data(&q->sock, &q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000278 q->sk.sk_write_space = macvtap_sock_write_space;
279
280 err = macvtap_set_queue(dev, file, q);
281 if (err)
282 sock_put(&q->sk);
283
284out:
285 if (dev)
286 dev_put(dev);
287
288 return err;
289}
290
291static int macvtap_release(struct inode *inode, struct file *file)
292{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000293 struct macvtap_queue *q = file->private_data;
294 macvtap_put_queue(q);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000295 return 0;
296}
297
298static unsigned int macvtap_poll(struct file *file, poll_table * wait)
299{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000300 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000301 unsigned int mask = POLLERR;
302
303 if (!q)
304 goto out;
305
306 mask = 0;
307 poll_wait(file, &q->sock.wait, wait);
308
309 if (!skb_queue_empty(&q->sk.sk_receive_queue))
310 mask |= POLLIN | POLLRDNORM;
311
312 if (sock_writeable(&q->sk) ||
313 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
314 sock_writeable(&q->sk)))
315 mask |= POLLOUT | POLLWRNORM;
316
317out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000318 return mask;
319}
320
321/* Get packet from user space buffer */
322static ssize_t macvtap_get_user(struct macvtap_queue *q,
323 const struct iovec *iv, size_t count,
324 int noblock)
325{
326 struct sk_buff *skb;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000327 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000328 size_t len = count;
329 int err;
330
331 if (unlikely(len < ETH_HLEN))
332 return -EINVAL;
333
334 skb = sock_alloc_send_skb(&q->sk, NET_IP_ALIGN + len, noblock, &err);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000335 if (!skb)
336 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000337
338 skb_reserve(skb, NET_IP_ALIGN);
339 skb_put(skb, count);
340
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000341 err = skb_copy_datagram_from_iovec(skb, 0, iv, 0, len);
342 if (err)
343 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000344
345 skb_set_network_header(skb, ETH_HLEN);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000346 rcu_read_lock_bh();
347 vlan = rcu_dereference(q->vlan);
348 if (vlan)
349 macvlan_start_xmit(skb, vlan->dev);
350 else
351 kfree_skb(skb);
352 rcu_read_unlock_bh();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000353
354 return count;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000355
356err:
357 rcu_read_lock_bh();
358 vlan = rcu_dereference(q->vlan);
359 if (vlan)
360 macvlan_count_rx(q->vlan, 0, false, false);
361 rcu_read_unlock_bh();
362
363 kfree_skb(skb);
364
365 return err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000366}
367
368static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
369 unsigned long count, loff_t pos)
370{
371 struct file *file = iocb->ki_filp;
372 ssize_t result = -ENOLINK;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000373 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000374
375 result = macvtap_get_user(q, iv, iov_length(iv, count),
376 file->f_flags & O_NONBLOCK);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000377 return result;
378}
379
380/* Put packet to the user space buffer */
381static ssize_t macvtap_put_user(struct macvtap_queue *q,
382 const struct sk_buff *skb,
383 const struct iovec *iv, int len)
384{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000385 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000386 int ret;
387
388 len = min_t(int, skb->len, len);
389
390 ret = skb_copy_datagram_const_iovec(skb, 0, iv, 0, len);
391
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000392 rcu_read_lock_bh();
393 vlan = rcu_dereference(q->vlan);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000394 if (vlan)
395 macvlan_count_rx(vlan, len, ret == 0, 0);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000396 rcu_read_unlock_bh();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000397
398 return ret ? ret : len;
399}
400
Arnd Bergmann501c7742010-02-18 05:46:50 +0000401static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
402 const struct iovec *iv, unsigned long len,
403 int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000404{
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000405 DECLARE_WAITQUEUE(wait, current);
406 struct sk_buff *skb;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000407 ssize_t ret = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000408
409 add_wait_queue(q->sk.sk_sleep, &wait);
410 while (len) {
411 current->state = TASK_INTERRUPTIBLE;
412
413 /* Read frames from the queue */
414 skb = skb_dequeue(&q->sk.sk_receive_queue);
415 if (!skb) {
Arnd Bergmann501c7742010-02-18 05:46:50 +0000416 if (noblock) {
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000417 ret = -EAGAIN;
418 break;
419 }
420 if (signal_pending(current)) {
421 ret = -ERESTARTSYS;
422 break;
423 }
424 /* Nothing to read, let's sleep */
425 schedule();
426 continue;
427 }
428 ret = macvtap_put_user(q, skb, iv, len);
429 kfree_skb(skb);
430 break;
431 }
432
433 current->state = TASK_RUNNING;
434 remove_wait_queue(q->sk.sk_sleep, &wait);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000435 return ret;
436}
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000437
Arnd Bergmann501c7742010-02-18 05:46:50 +0000438static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
439 unsigned long count, loff_t pos)
440{
441 struct file *file = iocb->ki_filp;
442 struct macvtap_queue *q = file->private_data;
443 ssize_t len, ret = 0;
444
445 len = iov_length(iv, count);
446 if (len < 0) {
447 ret = -EINVAL;
448 goto out;
449 }
450
451 ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
452 ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000453out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000454 return ret;
455}
456
457/*
458 * provide compatibility with generic tun/tap interface
459 */
460static long macvtap_ioctl(struct file *file, unsigned int cmd,
461 unsigned long arg)
462{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000463 struct macvtap_queue *q = file->private_data;
464 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000465 void __user *argp = (void __user *)arg;
466 struct ifreq __user *ifr = argp;
467 unsigned int __user *up = argp;
468 unsigned int u;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000469 int ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000470
471 switch (cmd) {
472 case TUNSETIFF:
473 /* ignore the name, just look at flags */
474 if (get_user(u, &ifr->ifr_flags))
475 return -EFAULT;
476 if (u != (IFF_TAP | IFF_NO_PI))
477 return -EINVAL;
478 return 0;
479
480 case TUNGETIFF:
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000481 rcu_read_lock_bh();
482 vlan = rcu_dereference(q->vlan);
483 if (vlan)
484 dev_hold(vlan->dev);
485 rcu_read_unlock_bh();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000486
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000487 if (!vlan)
488 return -ENOLINK;
489
490 ret = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000491 if (copy_to_user(&ifr->ifr_name, q->vlan->dev->name, IFNAMSIZ) ||
492 put_user((TUN_TAP_DEV | TUN_NO_PI), &ifr->ifr_flags))
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000493 ret = -EFAULT;
494 dev_put(vlan->dev);
495 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000496
497 case TUNGETFEATURES:
498 if (put_user((IFF_TAP | IFF_NO_PI), up))
499 return -EFAULT;
500 return 0;
501
502 case TUNSETSNDBUF:
503 if (get_user(u, up))
504 return -EFAULT;
505
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000506 q->sk.sk_sndbuf = u;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000507 return 0;
508
509 case TUNSETOFFLOAD:
510 /* let the user check for future flags */
511 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
512 TUN_F_TSO_ECN | TUN_F_UFO))
513 return -EINVAL;
514
515 /* TODO: add support for these, so far we don't
516 support any offload */
517 if (arg & (TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
518 TUN_F_TSO_ECN | TUN_F_UFO))
519 return -EINVAL;
520
521 return 0;
522
523 default:
524 return -EINVAL;
525 }
526}
527
528#ifdef CONFIG_COMPAT
529static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
530 unsigned long arg)
531{
532 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
533}
534#endif
535
536static const struct file_operations macvtap_fops = {
537 .owner = THIS_MODULE,
538 .open = macvtap_open,
539 .release = macvtap_release,
540 .aio_read = macvtap_aio_read,
541 .aio_write = macvtap_aio_write,
542 .poll = macvtap_poll,
543 .llseek = no_llseek,
544 .unlocked_ioctl = macvtap_ioctl,
545#ifdef CONFIG_COMPAT
546 .compat_ioctl = macvtap_compat_ioctl,
547#endif
548};
549
Arnd Bergmann501c7742010-02-18 05:46:50 +0000550static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
551 struct msghdr *m, size_t total_len)
552{
553 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
554 return macvtap_get_user(q, m->msg_iov, total_len,
555 m->msg_flags & MSG_DONTWAIT);
556}
557
558static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
559 struct msghdr *m, size_t total_len,
560 int flags)
561{
562 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
563 int ret;
564 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
565 return -EINVAL;
566 ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
567 flags & MSG_DONTWAIT);
568 if (ret > total_len) {
569 m->msg_flags |= MSG_TRUNC;
570 ret = flags & MSG_TRUNC ? ret : total_len;
571 }
572 return ret;
573}
574
575/* Ops structure to mimic raw sockets with tun */
576static const struct proto_ops macvtap_socket_ops = {
577 .sendmsg = macvtap_sendmsg,
578 .recvmsg = macvtap_recvmsg,
579};
580
581/* Get an underlying socket object from tun file. Returns error unless file is
582 * attached to a device. The returned object works like a packet socket, it
583 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
584 * holding a reference to the file for as long as the socket is in use. */
585struct socket *macvtap_get_socket(struct file *file)
586{
587 struct macvtap_queue *q;
588 if (file->f_op != &macvtap_fops)
589 return ERR_PTR(-EINVAL);
590 q = file->private_data;
591 if (!q)
592 return ERR_PTR(-EBADFD);
593 return &q->sock;
594}
595EXPORT_SYMBOL_GPL(macvtap_get_socket);
596
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000597static int macvtap_init(void)
598{
599 int err;
600
601 err = alloc_chrdev_region(&macvtap_major, 0,
602 MACVTAP_NUM_DEVS, "macvtap");
603 if (err)
604 goto out1;
605
606 cdev_init(&macvtap_cdev, &macvtap_fops);
607 err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
608 if (err)
609 goto out2;
610
611 macvtap_class = class_create(THIS_MODULE, "macvtap");
612 if (IS_ERR(macvtap_class)) {
613 err = PTR_ERR(macvtap_class);
614 goto out3;
615 }
616
617 err = macvlan_link_register(&macvtap_link_ops);
618 if (err)
619 goto out4;
620
621 return 0;
622
623out4:
624 class_unregister(macvtap_class);
625out3:
626 cdev_del(&macvtap_cdev);
627out2:
628 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
629out1:
630 return err;
631}
632module_init(macvtap_init);
633
634static void macvtap_exit(void)
635{
636 rtnl_link_unregister(&macvtap_link_ops);
637 class_unregister(macvtap_class);
638 cdev_del(&macvtap_cdev);
639 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
640}
641module_exit(macvtap_exit);
642
643MODULE_ALIAS_RTNL_LINK("macvtap");
644MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
645MODULE_LICENSE("GPL");