]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/macvtap.c
macvtap: Rewrite macvtap_newlink so the error handling works.
[mirror_ubuntu-bionic-kernel.git] / drivers / net / macvtap.c
CommitLineData
20d29d7a
AB
1#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>
5a0e3ad6 12#include <linux/slab.h>
20d29d7a
AB
13#include <linux/init.h>
14#include <linux/wait.h>
15#include <linux/cdev.h>
16#include <linux/fs.h>
17
18#include <net/net_namespace.h>
19#include <net/rtnetlink.h>
20#include <net/sock.h>
b9fb9ee0 21#include <linux/virtio_net.h>
20d29d7a
AB
22
23/*
24 * A macvtap queue is the central object of this driver, it connects
25 * an open character device to a macvlan interface. There can be
26 * multiple queues on one interface, which map back to queues
27 * implemented in hardware on the underlying device.
28 *
29 * macvtap_proto is used to allocate queues through the sock allocation
30 * mechanism.
31 *
32 * TODO: multiqueue support is currently not implemented, even though
33 * macvtap is basically prepared for that. We will need to add this
34 * here as well as in virtio-net and qemu to get line rate on 10gbit
35 * adapters from a guest.
36 */
37struct macvtap_queue {
38 struct sock sk;
39 struct socket sock;
43815482 40 struct socket_wq wq;
55afbd08 41 int vnet_hdr_sz;
13707f9e 42 struct macvlan_dev __rcu *vlan;
20d29d7a 43 struct file *file;
b9fb9ee0 44 unsigned int flags;
20d29d7a
AB
45};
46
47static struct proto macvtap_proto = {
48 .name = "macvtap",
49 .owner = THIS_MODULE,
50 .obj_size = sizeof (struct macvtap_queue),
51};
52
53/*
54 * Minor number matches netdev->ifindex, so need a potentially
55 * large value. This also makes it possible to split the
56 * tap functionality out again in the future by offering it
57 * from other drivers besides macvtap. As long as every device
58 * only has one tap, the interface numbers assure that the
59 * device nodes are unique.
60 */
1ebed71a 61static dev_t macvtap_major;
20d29d7a 62#define MACVTAP_NUM_DEVS 65536
97bc3633 63#define GOODCOPY_LEN 128
20d29d7a
AB
64static struct class *macvtap_class;
65static struct cdev macvtap_cdev;
66
501c774c
AB
67static const struct proto_ops macvtap_socket_ops;
68
20d29d7a
AB
69/*
70 * RCU usage:
02df55d2
AB
71 * The macvtap_queue and the macvlan_dev are loosely coupled, the
72 * pointers from one to the other can only be read while rcu_read_lock
73 * or macvtap_lock is held.
20d29d7a 74 *
02df55d2
AB
75 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
76 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
77 * q->vlan becomes inaccessible. When the files gets closed,
78 * macvtap_get_queue() fails.
20d29d7a 79 *
02df55d2
AB
80 * There may still be references to the struct sock inside of the
81 * queue from outbound SKBs, but these never reference back to the
82 * file or the dev. The data structure is freed through __sk_free
83 * when both our references and any pending SKBs are gone.
20d29d7a
AB
84 */
85static DEFINE_SPINLOCK(macvtap_lock);
86
87/*
1565c7c1
KK
88 * get_slot: return a [unused/occupied] slot in vlan->taps[]:
89 * - if 'q' is NULL, return the first empty slot;
90 * - otherwise, return the slot this pointer occupies.
20d29d7a 91 */
1565c7c1
KK
92static int get_slot(struct macvlan_dev *vlan, struct macvtap_queue *q)
93{
94 int i;
95
96 for (i = 0; i < MAX_MACVTAP_QUEUES; i++) {
97 if (rcu_dereference(vlan->taps[i]) == q)
98 return i;
99 }
100
101 /* Should never happen */
102 BUG_ON(1);
103}
104
20d29d7a
AB
105static int macvtap_set_queue(struct net_device *dev, struct file *file,
106 struct macvtap_queue *q)
107{
108 struct macvlan_dev *vlan = netdev_priv(dev);
1565c7c1 109 int index;
20d29d7a
AB
110 int err = -EBUSY;
111
112 spin_lock(&macvtap_lock);
1565c7c1 113 if (vlan->numvtaps == MAX_MACVTAP_QUEUES)
20d29d7a
AB
114 goto out;
115
116 err = 0;
1565c7c1 117 index = get_slot(vlan, NULL);
02df55d2 118 rcu_assign_pointer(q->vlan, vlan);
1565c7c1 119 rcu_assign_pointer(vlan->taps[index], q);
02df55d2 120 sock_hold(&q->sk);
20d29d7a
AB
121
122 q->file = file;
02df55d2 123 file->private_data = q;
20d29d7a 124
1565c7c1
KK
125 vlan->numvtaps++;
126
20d29d7a
AB
127out:
128 spin_unlock(&macvtap_lock);
129 return err;
130}
131
132/*
02df55d2
AB
133 * The file owning the queue got closed, give up both
134 * the reference that the files holds as well as the
135 * one from the macvlan_dev if that still exists.
20d29d7a
AB
136 *
137 * Using the spinlock makes sure that we don't get
138 * to the queue again after destroying it.
20d29d7a 139 */
02df55d2 140static void macvtap_put_queue(struct macvtap_queue *q)
20d29d7a 141{
02df55d2 142 struct macvlan_dev *vlan;
20d29d7a
AB
143
144 spin_lock(&macvtap_lock);
13707f9e
ED
145 vlan = rcu_dereference_protected(q->vlan,
146 lockdep_is_held(&macvtap_lock));
02df55d2 147 if (vlan) {
1565c7c1
KK
148 int index = get_slot(vlan, q);
149
150 rcu_assign_pointer(vlan->taps[index], NULL);
02df55d2
AB
151 rcu_assign_pointer(q->vlan, NULL);
152 sock_put(&q->sk);
1565c7c1 153 --vlan->numvtaps;
20d29d7a
AB
154 }
155
20d29d7a
AB
156 spin_unlock(&macvtap_lock);
157
158 synchronize_rcu();
159 sock_put(&q->sk);
160}
161
162/*
1565c7c1
KK
163 * Select a queue based on the rxq of the device on which this packet
164 * arrived. If the incoming device is not mq, calculate a flow hash
165 * to select a queue. If all fails, find the first available queue.
166 * Cache vlan->numvtaps since it can become zero during the execution
167 * of this function.
20d29d7a
AB
168 */
169static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
170 struct sk_buff *skb)
171{
172 struct macvlan_dev *vlan = netdev_priv(dev);
1565c7c1
KK
173 struct macvtap_queue *tap = NULL;
174 int numvtaps = vlan->numvtaps;
175 __u32 rxq;
176
177 if (!numvtaps)
178 goto out;
179
180 if (likely(skb_rx_queue_recorded(skb))) {
181 rxq = skb_get_rx_queue(skb);
20d29d7a 182
1565c7c1
KK
183 while (unlikely(rxq >= numvtaps))
184 rxq -= numvtaps;
185
186 tap = rcu_dereference(vlan->taps[rxq]);
187 if (tap)
188 goto out;
189 }
190
191 /* Check if we can use flow to select a queue */
192 rxq = skb_get_rxhash(skb);
193 if (rxq) {
194 tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
195 if (tap)
196 goto out;
197 }
198
199 /* Everything failed - find first available queue */
200 for (rxq = 0; rxq < MAX_MACVTAP_QUEUES; rxq++) {
201 tap = rcu_dereference(vlan->taps[rxq]);
202 if (tap)
203 break;
204 }
205
206out:
207 return tap;
20d29d7a
AB
208}
209
02df55d2
AB
210/*
211 * The net_device is going away, give up the reference
1565c7c1
KK
212 * that it holds on all queues and safely set the pointer
213 * from the queues to NULL.
02df55d2 214 */
20d29d7a
AB
215static void macvtap_del_queues(struct net_device *dev)
216{
217 struct macvlan_dev *vlan = netdev_priv(dev);
1565c7c1
KK
218 struct macvtap_queue *q, *qlist[MAX_MACVTAP_QUEUES];
219 int i, j = 0;
02df55d2 220
1565c7c1 221 /* macvtap_put_queue can free some slots, so go through all slots */
02df55d2 222 spin_lock(&macvtap_lock);
1565c7c1 223 for (i = 0; i < MAX_MACVTAP_QUEUES && vlan->numvtaps; i++) {
13707f9e
ED
224 q = rcu_dereference_protected(vlan->taps[i],
225 lockdep_is_held(&macvtap_lock));
1565c7c1
KK
226 if (q) {
227 qlist[j++] = q;
228 rcu_assign_pointer(vlan->taps[i], NULL);
229 rcu_assign_pointer(q->vlan, NULL);
230 vlan->numvtaps--;
231 }
564517e8 232 }
1565c7c1 233 BUG_ON(vlan->numvtaps != 0);
99f34b38
EB
234 /* guarantee that any future macvtap_set_queue will fail */
235 vlan->numvtaps = MAX_MACVTAP_QUEUES;
02df55d2
AB
236 spin_unlock(&macvtap_lock);
237
238 synchronize_rcu();
1565c7c1
KK
239
240 for (--j; j >= 0; j--)
241 sock_put(&qlist[j]->sk);
20d29d7a
AB
242}
243
244/*
245 * Forward happens for data that gets sent from one macvlan
246 * endpoint to another one in bridge mode. We just take
247 * the skb and put it into the receive queue.
248 */
249static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
250{
251 struct macvtap_queue *q = macvtap_get_queue(dev, skb);
252 if (!q)
8a35747a
HX
253 goto drop;
254
255 if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
256 goto drop;
20d29d7a
AB
257
258 skb_queue_tail(&q->sk.sk_receive_queue, skb);
4a4771a5 259 wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
8a35747a
HX
260 return NET_RX_SUCCESS;
261
262drop:
263 kfree_skb(skb);
264 return NET_RX_DROP;
20d29d7a
AB
265}
266
267/*
268 * Receive is for data from the external interface (lowerdev),
269 * in case of macvtap, we can treat that the same way as
270 * forward, which macvlan cannot.
271 */
272static int macvtap_receive(struct sk_buff *skb)
273{
274 skb_push(skb, ETH_HLEN);
275 return macvtap_forward(skb->dev, skb);
276}
277
278static int macvtap_newlink(struct net *src_net,
279 struct net_device *dev,
280 struct nlattr *tb[],
281 struct nlattr *data[])
282{
9bf1907f
EB
283 /* Don't put anything that may fail after macvlan_common_newlink
284 * because we can't undo what it does.
285 */
286 return macvlan_common_newlink(src_net, dev, tb, data,
287 macvtap_receive, macvtap_forward);
20d29d7a
AB
288}
289
290static void macvtap_dellink(struct net_device *dev,
291 struct list_head *head)
292{
20d29d7a
AB
293 macvtap_del_queues(dev);
294 macvlan_dellink(dev, head);
295}
296
8a35747a
HX
297static void macvtap_setup(struct net_device *dev)
298{
299 macvlan_common_setup(dev);
300 dev->tx_queue_len = TUN_READQ_SIZE;
301}
302
20d29d7a
AB
303static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
304 .kind = "macvtap",
8a35747a 305 .setup = macvtap_setup,
20d29d7a
AB
306 .newlink = macvtap_newlink,
307 .dellink = macvtap_dellink,
308};
309
310
311static void macvtap_sock_write_space(struct sock *sk)
312{
43815482
ED
313 wait_queue_head_t *wqueue;
314
20d29d7a
AB
315 if (!sock_writeable(sk) ||
316 !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
317 return;
318
43815482
ED
319 wqueue = sk_sleep(sk);
320 if (wqueue && waitqueue_active(wqueue))
321 wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
20d29d7a
AB
322}
323
2259fef0
EB
324static void macvtap_sock_destruct(struct sock *sk)
325{
326 skb_queue_purge(&sk->sk_receive_queue);
327}
328
20d29d7a
AB
329static int macvtap_open(struct inode *inode, struct file *file)
330{
331 struct net *net = current->nsproxy->net_ns;
332 struct net_device *dev = dev_get_by_index(net, iminor(inode));
333 struct macvtap_queue *q;
334 int err;
335
336 err = -ENODEV;
337 if (!dev)
338 goto out;
339
340 /* check if this is a macvtap device */
341 err = -EINVAL;
342 if (dev->rtnl_link_ops != &macvtap_link_ops)
343 goto out;
344
345 err = -ENOMEM;
346 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
347 &macvtap_proto);
348 if (!q)
349 goto out;
350
43815482
ED
351 q->sock.wq = &q->wq;
352 init_waitqueue_head(&q->wq.wait);
20d29d7a
AB
353 q->sock.type = SOCK_RAW;
354 q->sock.state = SS_CONNECTED;
501c774c
AB
355 q->sock.file = file;
356 q->sock.ops = &macvtap_socket_ops;
20d29d7a 357 sock_init_data(&q->sock, &q->sk);
20d29d7a 358 q->sk.sk_write_space = macvtap_sock_write_space;
2259fef0 359 q->sk.sk_destruct = macvtap_sock_destruct;
b9fb9ee0 360 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
55afbd08 361 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
20d29d7a 362
97bc3633
SM
363 /*
364 * so far only KVM virtio_net uses macvtap, enable zero copy between
365 * guest kernel and host kernel when lower device supports zerocopy
047af9cf
EB
366 *
367 * The macvlan supports zerocopy iff the lower device supports zero
368 * copy so we don't have to look at the lower device directly.
97bc3633 369 */
047af9cf
EB
370 if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
371 sock_set_flag(&q->sk, SOCK_ZEROCOPY);
97bc3633 372
20d29d7a
AB
373 err = macvtap_set_queue(dev, file, q);
374 if (err)
375 sock_put(&q->sk);
376
377out:
378 if (dev)
379 dev_put(dev);
380
381 return err;
382}
383
384static int macvtap_release(struct inode *inode, struct file *file)
385{
02df55d2
AB
386 struct macvtap_queue *q = file->private_data;
387 macvtap_put_queue(q);
20d29d7a
AB
388 return 0;
389}
390
391static unsigned int macvtap_poll(struct file *file, poll_table * wait)
392{
02df55d2 393 struct macvtap_queue *q = file->private_data;
20d29d7a
AB
394 unsigned int mask = POLLERR;
395
396 if (!q)
397 goto out;
398
399 mask = 0;
43815482 400 poll_wait(file, &q->wq.wait, wait);
20d29d7a
AB
401
402 if (!skb_queue_empty(&q->sk.sk_receive_queue))
403 mask |= POLLIN | POLLRDNORM;
404
405 if (sock_writeable(&q->sk) ||
406 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
407 sock_writeable(&q->sk)))
408 mask |= POLLOUT | POLLWRNORM;
409
410out:
20d29d7a
AB
411 return mask;
412}
413
b9fb9ee0
AB
414static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
415 size_t len, size_t linear,
416 int noblock, int *err)
417{
418 struct sk_buff *skb;
419
420 /* Under a page? Don't bother with paged skb. */
421 if (prepad + len < PAGE_SIZE || !linear)
422 linear = len;
423
424 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
425 err);
426 if (!skb)
427 return NULL;
428
429 skb_reserve(skb, prepad);
430 skb_put(skb, linear);
431 skb->data_len = len - linear;
432 skb->len += len - linear;
433
434 return skb;
435}
436
97bc3633
SM
437/* set skb frags from iovec, this can move to core network code for reuse */
438static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
439 int offset, size_t count)
440{
441 int len = iov_length(from, count) - offset;
442 int copy = skb_headlen(skb);
443 int size, offset1 = 0;
444 int i = 0;
97bc3633
SM
445
446 /* Skip over from offset */
447 while (count && (offset >= from->iov_len)) {
448 offset -= from->iov_len;
449 ++from;
450 --count;
451 }
452
453 /* copy up to skb headlen */
454 while (count && (copy > 0)) {
455 size = min_t(unsigned int, copy, from->iov_len - offset);
456 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
457 size))
458 return -EFAULT;
459 if (copy > size) {
460 ++from;
461 --count;
462 }
463 copy -= size;
464 offset1 += size;
465 offset = 0;
466 }
467
468 if (len == offset1)
469 return 0;
470
471 while (count--) {
472 struct page *page[MAX_SKB_FRAGS];
473 int num_pages;
474 unsigned long base;
475
476 len = from->iov_len - offset1;
477 if (!len) {
478 offset1 = 0;
479 ++from;
480 continue;
481 }
482 base = (unsigned long)from->iov_base + offset1;
483 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
484 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
485 if ((num_pages != size) ||
486 (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
487 /* put_page is in skb free */
488 return -EFAULT;
489 skb->data_len += len;
490 skb->len += len;
491 skb->truesize += len;
492 atomic_add(len, &skb->sk->sk_wmem_alloc);
493 while (len) {
653fc915
JW
494 int off = base & ~PAGE_MASK;
495 int size = min_t(int, len, PAGE_SIZE - off);
496 __skb_fill_page_desc(skb, i, page[i], off, size);
97bc3633
SM
497 skb_shinfo(skb)->nr_frags++;
498 /* increase sk_wmem_alloc */
653fc915
JW
499 base += size;
500 len -= size;
97bc3633
SM
501 i++;
502 }
503 offset1 = 0;
504 ++from;
505 }
506 return 0;
507}
508
b9fb9ee0
AB
509/*
510 * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
511 * be shared with the tun/tap driver.
512 */
513static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
514 struct virtio_net_hdr *vnet_hdr)
515{
516 unsigned short gso_type = 0;
517 if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
518 switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
519 case VIRTIO_NET_HDR_GSO_TCPV4:
520 gso_type = SKB_GSO_TCPV4;
521 break;
522 case VIRTIO_NET_HDR_GSO_TCPV6:
523 gso_type = SKB_GSO_TCPV6;
524 break;
525 case VIRTIO_NET_HDR_GSO_UDP:
526 gso_type = SKB_GSO_UDP;
527 break;
528 default:
529 return -EINVAL;
530 }
531
532 if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
533 gso_type |= SKB_GSO_TCP_ECN;
534
535 if (vnet_hdr->gso_size == 0)
536 return -EINVAL;
537 }
538
539 if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
540 if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
541 vnet_hdr->csum_offset))
542 return -EINVAL;
543 }
544
545 if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
546 skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
547 skb_shinfo(skb)->gso_type = gso_type;
548
549 /* Header must be checked, and gso_segs computed. */
550 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
551 skb_shinfo(skb)->gso_segs = 0;
552 }
553 return 0;
554}
555
556static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
557 struct virtio_net_hdr *vnet_hdr)
558{
559 memset(vnet_hdr, 0, sizeof(*vnet_hdr));
560
561 if (skb_is_gso(skb)) {
562 struct skb_shared_info *sinfo = skb_shinfo(skb);
563
564 /* This is a hint as to how much should be linear. */
565 vnet_hdr->hdr_len = skb_headlen(skb);
566 vnet_hdr->gso_size = sinfo->gso_size;
567 if (sinfo->gso_type & SKB_GSO_TCPV4)
568 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
569 else if (sinfo->gso_type & SKB_GSO_TCPV6)
570 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
571 else if (sinfo->gso_type & SKB_GSO_UDP)
572 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
573 else
574 BUG();
575 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
576 vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
577 } else
578 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
579
580 if (skb->ip_summed == CHECKSUM_PARTIAL) {
581 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55508d60 582 vnet_hdr->csum_start = skb_checksum_start_offset(skb);
b9fb9ee0 583 vnet_hdr->csum_offset = skb->csum_offset;
10a8d94a
JW
584 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
585 vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
b9fb9ee0
AB
586 } /* else everything is zero */
587
588 return 0;
589}
590
591
20d29d7a 592/* Get packet from user space buffer */
97bc3633
SM
593static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
594 const struct iovec *iv, unsigned long total_len,
595 size_t count, int noblock)
20d29d7a
AB
596{
597 struct sk_buff *skb;
02df55d2 598 struct macvlan_dev *vlan;
97bc3633 599 unsigned long len = total_len;
20d29d7a 600 int err;
b9fb9ee0
AB
601 struct virtio_net_hdr vnet_hdr = { 0 };
602 int vnet_hdr_len = 0;
97bc3633
SM
603 int copylen;
604 bool zerocopy = false;
b9fb9ee0
AB
605
606 if (q->flags & IFF_VNET_HDR) {
55afbd08 607 vnet_hdr_len = q->vnet_hdr_sz;
b9fb9ee0
AB
608
609 err = -EINVAL;
ce3c8692 610 if (len < vnet_hdr_len)
b9fb9ee0 611 goto err;
ce3c8692 612 len -= vnet_hdr_len;
b9fb9ee0
AB
613
614 err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
55afbd08 615 sizeof(vnet_hdr));
b9fb9ee0
AB
616 if (err < 0)
617 goto err;
618 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
619 vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
620 vnet_hdr.hdr_len)
621 vnet_hdr.hdr_len = vnet_hdr.csum_start +
622 vnet_hdr.csum_offset + 2;
623 err = -EINVAL;
624 if (vnet_hdr.hdr_len > len)
625 goto err;
626 }
20d29d7a 627
b9fb9ee0 628 err = -EINVAL;
20d29d7a 629 if (unlikely(len < ETH_HLEN))
b9fb9ee0 630 goto err;
20d29d7a 631
97bc3633
SM
632 if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
633 zerocopy = true;
634
635 if (zerocopy) {
636 /* There are 256 bytes to be copied in skb, so there is enough
637 * room for skb expand head in case it is used.
638 * The rest buffer is mapped from userspace.
639 */
640 copylen = vnet_hdr.hdr_len;
641 if (!copylen)
642 copylen = GOODCOPY_LEN;
643 } else
644 copylen = len;
645
646 skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
647 vnet_hdr.hdr_len, noblock, &err);
02df55d2
AB
648 if (!skb)
649 goto err;
20d29d7a 650
97bc3633
SM
651 if (zerocopy) {
652 err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
653 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
654 } else
655 err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
656 len);
02df55d2 657 if (err)
b9fb9ee0 658 goto err_kfree;
20d29d7a
AB
659
660 skb_set_network_header(skb, ETH_HLEN);
b9fb9ee0
AB
661 skb_reset_mac_header(skb);
662 skb->protocol = eth_hdr(skb)->h_proto;
663
664 if (vnet_hdr_len) {
665 err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
666 if (err)
667 goto err_kfree;
668 }
669
02df55d2 670 rcu_read_lock_bh();
13707f9e 671 vlan = rcu_dereference_bh(q->vlan);
97bc3633
SM
672 /* copy skb_ubuf_info for callback when skb has no error */
673 if (zerocopy)
674 skb_shinfo(skb)->destructor_arg = m->msg_control;
02df55d2
AB
675 if (vlan)
676 macvlan_start_xmit(skb, vlan->dev);
677 else
678 kfree_skb(skb);
679 rcu_read_unlock_bh();
20d29d7a 680
97bc3633 681 return total_len;
02df55d2 682
b9fb9ee0
AB
683err_kfree:
684 kfree_skb(skb);
685
02df55d2
AB
686err:
687 rcu_read_lock_bh();
13707f9e 688 vlan = rcu_dereference_bh(q->vlan);
02df55d2 689 if (vlan)
1ac9ad13 690 vlan->dev->stats.tx_dropped++;
02df55d2
AB
691 rcu_read_unlock_bh();
692
02df55d2 693 return err;
20d29d7a
AB
694}
695
696static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
697 unsigned long count, loff_t pos)
698{
699 struct file *file = iocb->ki_filp;
700 ssize_t result = -ENOLINK;
02df55d2 701 struct macvtap_queue *q = file->private_data;
20d29d7a 702
97bc3633
SM
703 result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
704 file->f_flags & O_NONBLOCK);
20d29d7a
AB
705 return result;
706}
707
708/* Put packet to the user space buffer */
709static ssize_t macvtap_put_user(struct macvtap_queue *q,
710 const struct sk_buff *skb,
711 const struct iovec *iv, int len)
712{
02df55d2 713 struct macvlan_dev *vlan;
20d29d7a 714 int ret;
b9fb9ee0
AB
715 int vnet_hdr_len = 0;
716
717 if (q->flags & IFF_VNET_HDR) {
718 struct virtio_net_hdr vnet_hdr;
55afbd08 719 vnet_hdr_len = q->vnet_hdr_sz;
b9fb9ee0
AB
720 if ((len -= vnet_hdr_len) < 0)
721 return -EINVAL;
722
723 ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
724 if (ret)
725 return ret;
726
55afbd08 727 if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
b9fb9ee0
AB
728 return -EFAULT;
729 }
20d29d7a
AB
730
731 len = min_t(int, skb->len, len);
732
b9fb9ee0 733 ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len);
20d29d7a 734
02df55d2 735 rcu_read_lock_bh();
13707f9e 736 vlan = rcu_dereference_bh(q->vlan);
501c774c
AB
737 if (vlan)
738 macvlan_count_rx(vlan, len, ret == 0, 0);
02df55d2 739 rcu_read_unlock_bh();
20d29d7a 740
b9fb9ee0 741 return ret ? ret : (len + vnet_hdr_len);
20d29d7a
AB
742}
743
501c774c
AB
744static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
745 const struct iovec *iv, unsigned long len,
746 int noblock)
20d29d7a 747{
20d29d7a
AB
748 DECLARE_WAITQUEUE(wait, current);
749 struct sk_buff *skb;
501c774c 750 ssize_t ret = 0;
20d29d7a 751
4a4771a5 752 add_wait_queue(sk_sleep(&q->sk), &wait);
20d29d7a
AB
753 while (len) {
754 current->state = TASK_INTERRUPTIBLE;
755
756 /* Read frames from the queue */
757 skb = skb_dequeue(&q->sk.sk_receive_queue);
758 if (!skb) {
501c774c 759 if (noblock) {
20d29d7a
AB
760 ret = -EAGAIN;
761 break;
762 }
763 if (signal_pending(current)) {
764 ret = -ERESTARTSYS;
765 break;
766 }
767 /* Nothing to read, let's sleep */
768 schedule();
769 continue;
770 }
771 ret = macvtap_put_user(q, skb, iv, len);
772 kfree_skb(skb);
773 break;
774 }
775
776 current->state = TASK_RUNNING;
4a4771a5 777 remove_wait_queue(sk_sleep(&q->sk), &wait);
501c774c
AB
778 return ret;
779}
780
781static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
782 unsigned long count, loff_t pos)
783{
784 struct file *file = iocb->ki_filp;
785 struct macvtap_queue *q = file->private_data;
786 ssize_t len, ret = 0;
20d29d7a 787
501c774c
AB
788 len = iov_length(iv, count);
789 if (len < 0) {
790 ret = -EINVAL;
791 goto out;
792 }
793
794 ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
795 ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
20d29d7a 796out:
20d29d7a
AB
797 return ret;
798}
799
800/*
801 * provide compatibility with generic tun/tap interface
802 */
803static long macvtap_ioctl(struct file *file, unsigned int cmd,
804 unsigned long arg)
805{
02df55d2
AB
806 struct macvtap_queue *q = file->private_data;
807 struct macvlan_dev *vlan;
20d29d7a
AB
808 void __user *argp = (void __user *)arg;
809 struct ifreq __user *ifr = argp;
810 unsigned int __user *up = argp;
811 unsigned int u;
55afbd08
MT
812 int __user *sp = argp;
813 int s;
02df55d2 814 int ret;
20d29d7a
AB
815
816 switch (cmd) {
817 case TUNSETIFF:
818 /* ignore the name, just look at flags */
819 if (get_user(u, &ifr->ifr_flags))
820 return -EFAULT;
b9fb9ee0
AB
821
822 ret = 0;
823 if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
824 ret = -EINVAL;
825 else
826 q->flags = u;
827
828 return ret;
20d29d7a
AB
829
830 case TUNGETIFF:
02df55d2 831 rcu_read_lock_bh();
13707f9e 832 vlan = rcu_dereference_bh(q->vlan);
02df55d2
AB
833 if (vlan)
834 dev_hold(vlan->dev);
835 rcu_read_unlock_bh();
836
837 if (!vlan)
20d29d7a 838 return -ENOLINK;
20d29d7a 839
02df55d2 840 ret = 0;
13707f9e 841 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
b9fb9ee0 842 put_user(q->flags, &ifr->ifr_flags))
02df55d2
AB
843 ret = -EFAULT;
844 dev_put(vlan->dev);
845 return ret;
20d29d7a
AB
846
847 case TUNGETFEATURES:
b9fb9ee0 848 if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up))
20d29d7a
AB
849 return -EFAULT;
850 return 0;
851
852 case TUNSETSNDBUF:
853 if (get_user(u, up))
854 return -EFAULT;
855
20d29d7a 856 q->sk.sk_sndbuf = u;
20d29d7a
AB
857 return 0;
858
55afbd08
MT
859 case TUNGETVNETHDRSZ:
860 s = q->vnet_hdr_sz;
861 if (put_user(s, sp))
862 return -EFAULT;
863 return 0;
864
865 case TUNSETVNETHDRSZ:
866 if (get_user(s, sp))
867 return -EFAULT;
868 if (s < (int)sizeof(struct virtio_net_hdr))
869 return -EINVAL;
870
871 q->vnet_hdr_sz = s;
872 return 0;
873
20d29d7a
AB
874 case TUNSETOFFLOAD:
875 /* let the user check for future flags */
876 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
b9fb9ee0 877 TUN_F_TSO_ECN | TUN_F_UFO))
20d29d7a
AB
878 return -EINVAL;
879
b9fb9ee0
AB
880 /* TODO: only accept frames with the features that
881 got enabled for forwarded frames */
882 if (!(q->flags & IFF_VNET_HDR))
883 return -EINVAL;
20d29d7a
AB
884 return 0;
885
886 default:
887 return -EINVAL;
888 }
889}
890
891#ifdef CONFIG_COMPAT
892static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
893 unsigned long arg)
894{
895 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
896}
897#endif
898
899static const struct file_operations macvtap_fops = {
900 .owner = THIS_MODULE,
901 .open = macvtap_open,
902 .release = macvtap_release,
903 .aio_read = macvtap_aio_read,
904 .aio_write = macvtap_aio_write,
905 .poll = macvtap_poll,
906 .llseek = no_llseek,
907 .unlocked_ioctl = macvtap_ioctl,
908#ifdef CONFIG_COMPAT
909 .compat_ioctl = macvtap_compat_ioctl,
910#endif
911};
912
501c774c
AB
913static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
914 struct msghdr *m, size_t total_len)
915{
916 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
97bc3633 917 return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
501c774c
AB
918 m->msg_flags & MSG_DONTWAIT);
919}
920
921static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
922 struct msghdr *m, size_t total_len,
923 int flags)
924{
925 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
926 int ret;
927 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
928 return -EINVAL;
929 ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
930 flags & MSG_DONTWAIT);
931 if (ret > total_len) {
932 m->msg_flags |= MSG_TRUNC;
933 ret = flags & MSG_TRUNC ? ret : total_len;
934 }
935 return ret;
936}
937
938/* Ops structure to mimic raw sockets with tun */
939static const struct proto_ops macvtap_socket_ops = {
940 .sendmsg = macvtap_sendmsg,
941 .recvmsg = macvtap_recvmsg,
942};
943
944/* Get an underlying socket object from tun file. Returns error unless file is
945 * attached to a device. The returned object works like a packet socket, it
946 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
947 * holding a reference to the file for as long as the socket is in use. */
948struct socket *macvtap_get_socket(struct file *file)
949{
950 struct macvtap_queue *q;
951 if (file->f_op != &macvtap_fops)
952 return ERR_PTR(-EINVAL);
953 q = file->private_data;
954 if (!q)
955 return ERR_PTR(-EBADFD);
956 return &q->sock;
957}
958EXPORT_SYMBOL_GPL(macvtap_get_socket);
959
9bf1907f
EB
960static int macvtap_device_event(struct notifier_block *unused,
961 unsigned long event, void *ptr)
962{
963 struct net_device *dev = ptr;
964 struct device *classdev;
965 dev_t devt;
966
967 if (dev->rtnl_link_ops != &macvtap_link_ops)
968 return NOTIFY_DONE;
969
970
971 switch (event) {
972 case NETDEV_REGISTER:
973 /* Create the device node here after the network device has
974 * been registered but before register_netdevice has
975 * finished running.
976 */
977 devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
978 classdev = device_create(macvtap_class, &dev->dev, devt,
979 dev, "tap%d", dev->ifindex);
980 if (IS_ERR(classdev))
981 return notifier_from_errno(PTR_ERR(classdev));
982 break;
983 case NETDEV_UNREGISTER:
984 devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
985 device_destroy(macvtap_class, devt);
986 break;
987 }
988
989 return NOTIFY_DONE;
990}
991
992static struct notifier_block macvtap_notifier_block __read_mostly = {
993 .notifier_call = macvtap_device_event,
994};
995
20d29d7a
AB
996static int macvtap_init(void)
997{
998 int err;
999
1000 err = alloc_chrdev_region(&macvtap_major, 0,
1001 MACVTAP_NUM_DEVS, "macvtap");
1002 if (err)
1003 goto out1;
1004
1005 cdev_init(&macvtap_cdev, &macvtap_fops);
1006 err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
1007 if (err)
1008 goto out2;
1009
1010 macvtap_class = class_create(THIS_MODULE, "macvtap");
1011 if (IS_ERR(macvtap_class)) {
1012 err = PTR_ERR(macvtap_class);
1013 goto out3;
1014 }
1015
9bf1907f 1016 err = register_netdevice_notifier(&macvtap_notifier_block);
20d29d7a
AB
1017 if (err)
1018 goto out4;
1019
9bf1907f
EB
1020 err = macvlan_link_register(&macvtap_link_ops);
1021 if (err)
1022 goto out5;
1023
20d29d7a
AB
1024 return 0;
1025
9bf1907f
EB
1026out5:
1027 unregister_netdevice_notifier(&macvtap_notifier_block);
20d29d7a
AB
1028out4:
1029 class_unregister(macvtap_class);
1030out3:
1031 cdev_del(&macvtap_cdev);
1032out2:
1033 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
1034out1:
1035 return err;
1036}
1037module_init(macvtap_init);
1038
1039static void macvtap_exit(void)
1040{
1041 rtnl_link_unregister(&macvtap_link_ops);
9bf1907f 1042 unregister_netdevice_notifier(&macvtap_notifier_block);
20d29d7a
AB
1043 class_unregister(macvtap_class);
1044 cdev_del(&macvtap_cdev);
1045 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
1046}
1047module_exit(macvtap_exit);
1048
1049MODULE_ALIAS_RTNL_LINK("macvtap");
1050MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
1051MODULE_LICENSE("GPL");