1 #include <linux/etherdevice.h> 2 #include <linux/if_macvlan.h> 3 #include <linux/if_vlan.h> 4 #include <linux/interrupt.h> 5 #include <linux/nsproxy.h> 6 #include <linux/compat.h> 7 #include <linux/if_tun.h> 8 #include <linux/module.h> 9 #include <linux/skbuff.h> 10 #include <linux/cache.h> 11 #include <linux/sched.h> 12 #include <linux/types.h> 13 #include <linux/slab.h> 14 #include <linux/wait.h> 15 #include <linux/cdev.h> 16 #include <linux/idr.h> 17 #include <linux/fs.h> 18 #include <linux/uio.h> 19 20 #include <net/net_namespace.h> 21 #include <net/rtnetlink.h> 22 #include <net/sock.h> 23 #include <linux/virtio_net.h> 24 #include <linux/skb_array.h> 25 26 /* 27 * A tap queue is the central object of this driver, it connects 28 * an open character device to a macvlan interface. There can be 29 * multiple queues on one interface, which map back to queues 30 * implemented in hardware on the underlying device. 31 * 32 * tap_proto is used to allocate queues through the sock allocation 33 * mechanism. 34 * 35 */ 36 struct tap_queue { 37 struct sock sk; 38 struct socket sock; 39 struct socket_wq wq; 40 int vnet_hdr_sz; 41 struct macvlan_dev __rcu *vlan; 42 struct file *file; 43 unsigned int flags; 44 u16 queue_index; 45 bool enabled; 46 struct list_head next; 47 struct skb_array skb_array; 48 }; 49 50 #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE) 51 52 #define TAP_VNET_LE 0x80000000 53 #define TAP_VNET_BE 0x40000000 54 55 #ifdef CONFIG_TUN_VNET_CROSS_LE 56 static inline bool tap_legacy_is_little_endian(struct tap_queue *q) 57 { 58 return q->flags & TAP_VNET_BE ? false : 59 virtio_legacy_is_little_endian(); 60 } 61 62 static long tap_get_vnet_be(struct tap_queue *q, int __user *sp) 63 { 64 int s = !!(q->flags & TAP_VNET_BE); 65 66 if (put_user(s, sp)) 67 return -EFAULT; 68 69 return 0; 70 } 71 72 static long tap_set_vnet_be(struct tap_queue *q, int __user *sp) 73 { 74 int s; 75 76 if (get_user(s, sp)) 77 return -EFAULT; 78 79 if (s) 80 q->flags |= TAP_VNET_BE; 81 else 82 q->flags &= ~TAP_VNET_BE; 83 84 return 0; 85 } 86 #else 87 static inline bool tap_legacy_is_little_endian(struct tap_queue *q) 88 { 89 return virtio_legacy_is_little_endian(); 90 } 91 92 static long tap_get_vnet_be(struct tap_queue *q, int __user *argp) 93 { 94 return -EINVAL; 95 } 96 97 static long tap_set_vnet_be(struct tap_queue *q, int __user *argp) 98 { 99 return -EINVAL; 100 } 101 #endif /* CONFIG_TUN_VNET_CROSS_LE */ 102 103 static inline bool tap_is_little_endian(struct tap_queue *q) 104 { 105 return q->flags & TAP_VNET_LE || 106 tap_legacy_is_little_endian(q); 107 } 108 109 static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val) 110 { 111 return __virtio16_to_cpu(tap_is_little_endian(q), val); 112 } 113 114 static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val) 115 { 116 return __cpu_to_virtio16(tap_is_little_endian(q), val); 117 } 118 119 static struct proto tap_proto = { 120 .name = "tap", 121 .owner = THIS_MODULE, 122 .obj_size = sizeof(struct tap_queue), 123 }; 124 125 #define TAP_NUM_DEVS (1U << MINORBITS) 126 struct major_info { 127 dev_t major; 128 struct idr minor_idr; 129 struct mutex minor_lock; 130 const char *device_name; 131 } macvtap_major; 132 133 #define GOODCOPY_LEN 128 134 135 static const struct proto_ops tap_socket_ops; 136 137 #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO) 138 #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST) 139 140 static struct macvlan_dev *tap_get_vlan_rcu(const struct net_device *dev) 141 { 142 return rcu_dereference(dev->rx_handler_data); 143 } 144 145 /* 146 * RCU usage: 147 * The tap_queue and the macvlan_dev are loosely coupled, the 148 * pointers from one to the other can only be read while rcu_read_lock 149 * or rtnl is held. 150 * 151 * Both the file and the macvlan_dev hold a reference on the tap_queue 152 * through sock_hold(&q->sk). When the macvlan_dev goes away first, 153 * q->vlan becomes inaccessible. When the files gets closed, 154 * tap_get_queue() fails. 155 * 156 * There may still be references to the struct sock inside of the 157 * queue from outbound SKBs, but these never reference back to the 158 * file or the dev. The data structure is freed through __sk_free 159 * when both our references and any pending SKBs are gone. 160 */ 161 162 static int tap_enable_queue(struct net_device *dev, struct file *file, 163 struct tap_queue *q) 164 { 165 struct macvlan_dev *vlan = netdev_priv(dev); 166 int err = -EINVAL; 167 168 ASSERT_RTNL(); 169 170 if (q->enabled) 171 goto out; 172 173 err = 0; 174 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q); 175 q->queue_index = vlan->numvtaps; 176 q->enabled = true; 177 178 vlan->numvtaps++; 179 out: 180 return err; 181 } 182 183 /* Requires RTNL */ 184 static int tap_set_queue(struct net_device *dev, struct file *file, 185 struct tap_queue *q) 186 { 187 struct macvlan_dev *vlan = netdev_priv(dev); 188 189 if (vlan->numqueues == MAX_TAP_QUEUES) 190 return -EBUSY; 191 192 rcu_assign_pointer(q->vlan, vlan); 193 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q); 194 sock_hold(&q->sk); 195 196 q->file = file; 197 q->queue_index = vlan->numvtaps; 198 q->enabled = true; 199 file->private_data = q; 200 list_add_tail(&q->next, &vlan->queue_list); 201 202 vlan->numvtaps++; 203 vlan->numqueues++; 204 205 return 0; 206 } 207 208 static int tap_disable_queue(struct tap_queue *q) 209 { 210 struct macvlan_dev *vlan; 211 struct tap_queue *nq; 212 213 ASSERT_RTNL(); 214 if (!q->enabled) 215 return -EINVAL; 216 217 vlan = rtnl_dereference(q->vlan); 218 219 if (vlan) { 220 int index = q->queue_index; 221 BUG_ON(index >= vlan->numvtaps); 222 nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]); 223 nq->queue_index = index; 224 225 rcu_assign_pointer(vlan->taps[index], nq); 226 RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL); 227 q->enabled = false; 228 229 vlan->numvtaps--; 230 } 231 232 return 0; 233 } 234 235 /* 236 * The file owning the queue got closed, give up both 237 * the reference that the files holds as well as the 238 * one from the macvlan_dev if that still exists. 239 * 240 * Using the spinlock makes sure that we don't get 241 * to the queue again after destroying it. 242 */ 243 static void tap_put_queue(struct tap_queue *q) 244 { 245 struct macvlan_dev *vlan; 246 247 rtnl_lock(); 248 vlan = rtnl_dereference(q->vlan); 249 250 if (vlan) { 251 if (q->enabled) 252 BUG_ON(tap_disable_queue(q)); 253 254 vlan->numqueues--; 255 RCU_INIT_POINTER(q->vlan, NULL); 256 sock_put(&q->sk); 257 list_del_init(&q->next); 258 } 259 260 rtnl_unlock(); 261 262 synchronize_rcu(); 263 sock_put(&q->sk); 264 } 265 266 /* 267 * Select a queue based on the rxq of the device on which this packet 268 * arrived. If the incoming device is not mq, calculate a flow hash 269 * to select a queue. If all fails, find the first available queue. 270 * Cache vlan->numvtaps since it can become zero during the execution 271 * of this function. 272 */ 273 static struct tap_queue *tap_get_queue(struct net_device *dev, 274 struct sk_buff *skb) 275 { 276 struct macvlan_dev *vlan = netdev_priv(dev); 277 struct tap_queue *tap = NULL; 278 /* Access to taps array is protected by rcu, but access to numvtaps 279 * isn't. Below we use it to lookup a queue, but treat it as a hint 280 * and validate that the result isn't NULL - in case we are 281 * racing against queue removal. 282 */ 283 int numvtaps = ACCESS_ONCE(vlan->numvtaps); 284 __u32 rxq; 285 286 if (!numvtaps) 287 goto out; 288 289 if (numvtaps == 1) 290 goto single; 291 292 /* Check if we can use flow to select a queue */ 293 rxq = skb_get_hash(skb); 294 if (rxq) { 295 tap = rcu_dereference(vlan->taps[rxq % numvtaps]); 296 goto out; 297 } 298 299 if (likely(skb_rx_queue_recorded(skb))) { 300 rxq = skb_get_rx_queue(skb); 301 302 while (unlikely(rxq >= numvtaps)) 303 rxq -= numvtaps; 304 305 tap = rcu_dereference(vlan->taps[rxq]); 306 goto out; 307 } 308 309 single: 310 tap = rcu_dereference(vlan->taps[0]); 311 out: 312 return tap; 313 } 314 315 /* 316 * The net_device is going away, give up the reference 317 * that it holds on all queues and safely set the pointer 318 * from the queues to NULL. 319 */ 320 void tap_del_queues(struct net_device *dev) 321 { 322 struct macvlan_dev *vlan = netdev_priv(dev); 323 struct tap_queue *q, *tmp; 324 325 ASSERT_RTNL(); 326 list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) { 327 list_del_init(&q->next); 328 RCU_INIT_POINTER(q->vlan, NULL); 329 if (q->enabled) 330 vlan->numvtaps--; 331 vlan->numqueues--; 332 sock_put(&q->sk); 333 } 334 BUG_ON(vlan->numvtaps); 335 BUG_ON(vlan->numqueues); 336 /* guarantee that any future tap_set_queue will fail */ 337 vlan->numvtaps = MAX_TAP_QUEUES; 338 } 339 340 rx_handler_result_t tap_handle_frame(struct sk_buff **pskb) 341 { 342 struct sk_buff *skb = *pskb; 343 struct net_device *dev = skb->dev; 344 struct macvlan_dev *vlan; 345 struct tap_queue *q; 346 netdev_features_t features = TAP_FEATURES; 347 348 vlan = tap_get_vlan_rcu(dev); 349 if (!vlan) 350 return RX_HANDLER_PASS; 351 352 q = tap_get_queue(dev, skb); 353 if (!q) 354 return RX_HANDLER_PASS; 355 356 if (__skb_array_full(&q->skb_array)) 357 goto drop; 358 359 skb_push(skb, ETH_HLEN); 360 361 /* Apply the forward feature mask so that we perform segmentation 362 * according to users wishes. This only works if VNET_HDR is 363 * enabled. 364 */ 365 if (q->flags & IFF_VNET_HDR) 366 features |= vlan->tap_features; 367 if (netif_needs_gso(skb, features)) { 368 struct sk_buff *segs = __skb_gso_segment(skb, features, false); 369 370 if (IS_ERR(segs)) 371 goto drop; 372 373 if (!segs) { 374 if (skb_array_produce(&q->skb_array, skb)) 375 goto drop; 376 goto wake_up; 377 } 378 379 consume_skb(skb); 380 while (segs) { 381 struct sk_buff *nskb = segs->next; 382 383 segs->next = NULL; 384 if (skb_array_produce(&q->skb_array, segs)) { 385 kfree_skb(segs); 386 kfree_skb_list(nskb); 387 break; 388 } 389 segs = nskb; 390 } 391 } else { 392 /* If we receive a partial checksum and the tap side 393 * doesn't support checksum offload, compute the checksum. 394 * Note: it doesn't matter which checksum feature to 395 * check, we either support them all or none. 396 */ 397 if (skb->ip_summed == CHECKSUM_PARTIAL && 398 !(features & NETIF_F_CSUM_MASK) && 399 skb_checksum_help(skb)) 400 goto drop; 401 if (skb_array_produce(&q->skb_array, skb)) 402 goto drop; 403 } 404 405 wake_up: 406 wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND); 407 return RX_HANDLER_CONSUMED; 408 409 drop: 410 /* Count errors/drops only here, thus don't care about args. */ 411 macvlan_count_rx(vlan, 0, 0, 0); 412 kfree_skb(skb); 413 return RX_HANDLER_CONSUMED; 414 } 415 416 int tap_get_minor(struct macvlan_dev *vlan) 417 { 418 int retval = -ENOMEM; 419 420 mutex_lock(&macvtap_major.minor_lock); 421 retval = idr_alloc(&macvtap_major.minor_idr, vlan, 1, TAP_NUM_DEVS, GFP_KERNEL); 422 if (retval >= 0) { 423 vlan->minor = retval; 424 } else if (retval == -ENOSPC) { 425 netdev_err(vlan->dev, "Too many tap devices\n"); 426 retval = -EINVAL; 427 } 428 mutex_unlock(&macvtap_major.minor_lock); 429 return retval < 0 ? retval : 0; 430 } 431 432 void tap_free_minor(struct macvlan_dev *vlan) 433 { 434 mutex_lock(&macvtap_major.minor_lock); 435 if (vlan->minor) { 436 idr_remove(&macvtap_major.minor_idr, vlan->minor); 437 vlan->minor = 0; 438 } 439 mutex_unlock(&macvtap_major.minor_lock); 440 } 441 442 static struct net_device *dev_get_by_tap_minor(int minor) 443 { 444 struct net_device *dev = NULL; 445 struct macvlan_dev *vlan; 446 447 mutex_lock(&macvtap_major.minor_lock); 448 vlan = idr_find(&macvtap_major.minor_idr, minor); 449 if (vlan) { 450 dev = vlan->dev; 451 dev_hold(dev); 452 } 453 mutex_unlock(&macvtap_major.minor_lock); 454 return dev; 455 } 456 457 static void tap_sock_write_space(struct sock *sk) 458 { 459 wait_queue_head_t *wqueue; 460 461 if (!sock_writeable(sk) || 462 !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 463 return; 464 465 wqueue = sk_sleep(sk); 466 if (wqueue && waitqueue_active(wqueue)) 467 wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND); 468 } 469 470 static void tap_sock_destruct(struct sock *sk) 471 { 472 struct tap_queue *q = container_of(sk, struct tap_queue, sk); 473 474 skb_array_cleanup(&q->skb_array); 475 } 476 477 static int tap_open(struct inode *inode, struct file *file) 478 { 479 struct net *net = current->nsproxy->net_ns; 480 struct net_device *dev; 481 struct tap_queue *q; 482 int err = -ENODEV; 483 484 rtnl_lock(); 485 dev = dev_get_by_tap_minor(iminor(inode)); 486 if (!dev) 487 goto err; 488 489 err = -ENOMEM; 490 q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 491 &tap_proto, 0); 492 if (!q) 493 goto err; 494 495 RCU_INIT_POINTER(q->sock.wq, &q->wq); 496 init_waitqueue_head(&q->wq.wait); 497 q->sock.type = SOCK_RAW; 498 q->sock.state = SS_CONNECTED; 499 q->sock.file = file; 500 q->sock.ops = &tap_socket_ops; 501 sock_init_data(&q->sock, &q->sk); 502 q->sk.sk_write_space = tap_sock_write_space; 503 q->sk.sk_destruct = tap_sock_destruct; 504 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP; 505 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 506 507 /* 508 * so far only KVM virtio_net uses tap, enable zero copy between 509 * guest kernel and host kernel when lower device supports zerocopy 510 * 511 * The macvlan supports zerocopy iff the lower device supports zero 512 * copy so we don't have to look at the lower device directly. 513 */ 514 if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG)) 515 sock_set_flag(&q->sk, SOCK_ZEROCOPY); 516 517 err = -ENOMEM; 518 if (skb_array_init(&q->skb_array, dev->tx_queue_len, GFP_KERNEL)) 519 goto err_array; 520 521 err = tap_set_queue(dev, file, q); 522 if (err) 523 goto err_queue; 524 525 dev_put(dev); 526 527 rtnl_unlock(); 528 return err; 529 530 err_queue: 531 skb_array_cleanup(&q->skb_array); 532 err_array: 533 sock_put(&q->sk); 534 err: 535 if (dev) 536 dev_put(dev); 537 538 rtnl_unlock(); 539 return err; 540 } 541 542 static int tap_release(struct inode *inode, struct file *file) 543 { 544 struct tap_queue *q = file->private_data; 545 tap_put_queue(q); 546 return 0; 547 } 548 549 static unsigned int tap_poll(struct file *file, poll_table *wait) 550 { 551 struct tap_queue *q = file->private_data; 552 unsigned int mask = POLLERR; 553 554 if (!q) 555 goto out; 556 557 mask = 0; 558 poll_wait(file, &q->wq.wait, wait); 559 560 if (!skb_array_empty(&q->skb_array)) 561 mask |= POLLIN | POLLRDNORM; 562 563 if (sock_writeable(&q->sk) || 564 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) && 565 sock_writeable(&q->sk))) 566 mask |= POLLOUT | POLLWRNORM; 567 568 out: 569 return mask; 570 } 571 572 static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad, 573 size_t len, size_t linear, 574 int noblock, int *err) 575 { 576 struct sk_buff *skb; 577 578 /* Under a page? Don't bother with paged skb. */ 579 if (prepad + len < PAGE_SIZE || !linear) 580 linear = len; 581 582 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 583 err, 0); 584 if (!skb) 585 return NULL; 586 587 skb_reserve(skb, prepad); 588 skb_put(skb, linear); 589 skb->data_len = len - linear; 590 skb->len += len - linear; 591 592 return skb; 593 } 594 595 /* Neighbour code has some assumptions on HH_DATA_MOD alignment */ 596 #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN) 597 598 /* Get packet from user space buffer */ 599 static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m, 600 struct iov_iter *from, int noblock) 601 { 602 int good_linear = SKB_MAX_HEAD(TAP_RESERVE); 603 struct sk_buff *skb; 604 struct macvlan_dev *vlan; 605 unsigned long total_len = iov_iter_count(from); 606 unsigned long len = total_len; 607 int err; 608 struct virtio_net_hdr vnet_hdr = { 0 }; 609 int vnet_hdr_len = 0; 610 int copylen = 0; 611 int depth; 612 bool zerocopy = false; 613 size_t linear; 614 615 if (q->flags & IFF_VNET_HDR) { 616 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz); 617 618 err = -EINVAL; 619 if (len < vnet_hdr_len) 620 goto err; 621 len -= vnet_hdr_len; 622 623 err = -EFAULT; 624 if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from)) 625 goto err; 626 iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr)); 627 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 628 tap16_to_cpu(q, vnet_hdr.csum_start) + 629 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 > 630 tap16_to_cpu(q, vnet_hdr.hdr_len)) 631 vnet_hdr.hdr_len = cpu_to_tap16(q, 632 tap16_to_cpu(q, vnet_hdr.csum_start) + 633 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2); 634 err = -EINVAL; 635 if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len) 636 goto err; 637 } 638 639 err = -EINVAL; 640 if (unlikely(len < ETH_HLEN)) 641 goto err; 642 643 if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) { 644 struct iov_iter i; 645 646 copylen = vnet_hdr.hdr_len ? 647 tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN; 648 if (copylen > good_linear) 649 copylen = good_linear; 650 else if (copylen < ETH_HLEN) 651 copylen = ETH_HLEN; 652 linear = copylen; 653 i = *from; 654 iov_iter_advance(&i, copylen); 655 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 656 zerocopy = true; 657 } 658 659 if (!zerocopy) { 660 copylen = len; 661 linear = tap16_to_cpu(q, vnet_hdr.hdr_len); 662 if (linear > good_linear) 663 linear = good_linear; 664 else if (linear < ETH_HLEN) 665 linear = ETH_HLEN; 666 } 667 668 skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen, 669 linear, noblock, &err); 670 if (!skb) 671 goto err; 672 673 if (zerocopy) 674 err = zerocopy_sg_from_iter(skb, from); 675 else 676 err = skb_copy_datagram_from_iter(skb, 0, from, len); 677 678 if (err) 679 goto err_kfree; 680 681 skb_set_network_header(skb, ETH_HLEN); 682 skb_reset_mac_header(skb); 683 skb->protocol = eth_hdr(skb)->h_proto; 684 685 if (vnet_hdr_len) { 686 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, 687 tap_is_little_endian(q)); 688 if (err) 689 goto err_kfree; 690 } 691 692 skb_probe_transport_header(skb, ETH_HLEN); 693 694 /* Move network header to the right position for VLAN tagged packets */ 695 if ((skb->protocol == htons(ETH_P_8021Q) || 696 skb->protocol == htons(ETH_P_8021AD)) && 697 __vlan_get_protocol(skb, skb->protocol, &depth) != 0) 698 skb_set_network_header(skb, depth); 699 700 rcu_read_lock(); 701 vlan = rcu_dereference(q->vlan); 702 /* copy skb_ubuf_info for callback when skb has no error */ 703 if (zerocopy) { 704 skb_shinfo(skb)->destructor_arg = m->msg_control; 705 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 706 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 707 } else if (m && m->msg_control) { 708 struct ubuf_info *uarg = m->msg_control; 709 uarg->callback(uarg, false); 710 } 711 712 if (vlan) { 713 skb->dev = vlan->dev; 714 dev_queue_xmit(skb); 715 } else { 716 kfree_skb(skb); 717 } 718 rcu_read_unlock(); 719 720 return total_len; 721 722 err_kfree: 723 kfree_skb(skb); 724 725 err: 726 rcu_read_lock(); 727 vlan = rcu_dereference(q->vlan); 728 if (vlan) 729 this_cpu_inc(vlan->pcpu_stats->tx_dropped); 730 rcu_read_unlock(); 731 732 return err; 733 } 734 735 static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from) 736 { 737 struct file *file = iocb->ki_filp; 738 struct tap_queue *q = file->private_data; 739 740 return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK); 741 } 742 743 /* Put packet to the user space buffer */ 744 static ssize_t tap_put_user(struct tap_queue *q, 745 const struct sk_buff *skb, 746 struct iov_iter *iter) 747 { 748 int ret; 749 int vnet_hdr_len = 0; 750 int vlan_offset = 0; 751 int total; 752 753 if (q->flags & IFF_VNET_HDR) { 754 struct virtio_net_hdr vnet_hdr; 755 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz); 756 if (iov_iter_count(iter) < vnet_hdr_len) 757 return -EINVAL; 758 759 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, 760 tap_is_little_endian(q), true)) 761 BUG(); 762 763 if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) != 764 sizeof(vnet_hdr)) 765 return -EFAULT; 766 767 iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr)); 768 } 769 total = vnet_hdr_len; 770 total += skb->len; 771 772 if (skb_vlan_tag_present(skb)) { 773 struct { 774 __be16 h_vlan_proto; 775 __be16 h_vlan_TCI; 776 } veth; 777 veth.h_vlan_proto = skb->vlan_proto; 778 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 779 780 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 781 total += VLAN_HLEN; 782 783 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 784 if (ret || !iov_iter_count(iter)) 785 goto done; 786 787 ret = copy_to_iter(&veth, sizeof(veth), iter); 788 if (ret != sizeof(veth) || !iov_iter_count(iter)) 789 goto done; 790 } 791 792 ret = skb_copy_datagram_iter(skb, vlan_offset, iter, 793 skb->len - vlan_offset); 794 795 done: 796 return ret ? ret : total; 797 } 798 799 static ssize_t tap_do_read(struct tap_queue *q, 800 struct iov_iter *to, 801 int noblock) 802 { 803 DEFINE_WAIT(wait); 804 struct sk_buff *skb; 805 ssize_t ret = 0; 806 807 if (!iov_iter_count(to)) 808 return 0; 809 810 while (1) { 811 if (!noblock) 812 prepare_to_wait(sk_sleep(&q->sk), &wait, 813 TASK_INTERRUPTIBLE); 814 815 /* Read frames from the queue */ 816 skb = skb_array_consume(&q->skb_array); 817 if (skb) 818 break; 819 if (noblock) { 820 ret = -EAGAIN; 821 break; 822 } 823 if (signal_pending(current)) { 824 ret = -ERESTARTSYS; 825 break; 826 } 827 /* Nothing to read, let's sleep */ 828 schedule(); 829 } 830 if (!noblock) 831 finish_wait(sk_sleep(&q->sk), &wait); 832 833 if (skb) { 834 ret = tap_put_user(q, skb, to); 835 if (unlikely(ret < 0)) 836 kfree_skb(skb); 837 else 838 consume_skb(skb); 839 } 840 return ret; 841 } 842 843 static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to) 844 { 845 struct file *file = iocb->ki_filp; 846 struct tap_queue *q = file->private_data; 847 ssize_t len = iov_iter_count(to), ret; 848 849 ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK); 850 ret = min_t(ssize_t, ret, len); 851 if (ret > 0) 852 iocb->ki_pos = ret; 853 return ret; 854 } 855 856 static struct macvlan_dev *tap_get_vlan(struct tap_queue *q) 857 { 858 struct macvlan_dev *vlan; 859 860 ASSERT_RTNL(); 861 vlan = rtnl_dereference(q->vlan); 862 if (vlan) 863 dev_hold(vlan->dev); 864 865 return vlan; 866 } 867 868 static void tap_put_vlan(struct macvlan_dev *vlan) 869 { 870 dev_put(vlan->dev); 871 } 872 873 static int tap_ioctl_set_queue(struct file *file, unsigned int flags) 874 { 875 struct tap_queue *q = file->private_data; 876 struct macvlan_dev *vlan; 877 int ret; 878 879 vlan = tap_get_vlan(q); 880 if (!vlan) 881 return -EINVAL; 882 883 if (flags & IFF_ATTACH_QUEUE) 884 ret = tap_enable_queue(vlan->dev, file, q); 885 else if (flags & IFF_DETACH_QUEUE) 886 ret = tap_disable_queue(q); 887 else 888 ret = -EINVAL; 889 890 tap_put_vlan(vlan); 891 return ret; 892 } 893 894 static int set_offload(struct tap_queue *q, unsigned long arg) 895 { 896 struct macvlan_dev *vlan; 897 netdev_features_t features; 898 netdev_features_t feature_mask = 0; 899 900 vlan = rtnl_dereference(q->vlan); 901 if (!vlan) 902 return -ENOLINK; 903 904 features = vlan->dev->features; 905 906 if (arg & TUN_F_CSUM) { 907 feature_mask = NETIF_F_HW_CSUM; 908 909 if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) { 910 if (arg & TUN_F_TSO_ECN) 911 feature_mask |= NETIF_F_TSO_ECN; 912 if (arg & TUN_F_TSO4) 913 feature_mask |= NETIF_F_TSO; 914 if (arg & TUN_F_TSO6) 915 feature_mask |= NETIF_F_TSO6; 916 } 917 918 if (arg & TUN_F_UFO) 919 feature_mask |= NETIF_F_UFO; 920 } 921 922 /* tun/tap driver inverts the usage for TSO offloads, where 923 * setting the TSO bit means that the userspace wants to 924 * accept TSO frames and turning it off means that user space 925 * does not support TSO. 926 * For tap, we have to invert it to mean the same thing. 927 * When user space turns off TSO, we turn off GSO/LRO so that 928 * user-space will not receive TSO frames. 929 */ 930 if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO)) 931 features |= RX_OFFLOADS; 932 else 933 features &= ~RX_OFFLOADS; 934 935 /* tap_features are the same as features on tun/tap and 936 * reflect user expectations. 937 */ 938 vlan->tap_features = feature_mask; 939 vlan->set_features = features; 940 netdev_update_features(vlan->dev); 941 942 return 0; 943 } 944 945 /* 946 * provide compatibility with generic tun/tap interface 947 */ 948 static long tap_ioctl(struct file *file, unsigned int cmd, 949 unsigned long arg) 950 { 951 struct tap_queue *q = file->private_data; 952 struct macvlan_dev *vlan; 953 void __user *argp = (void __user *)arg; 954 struct ifreq __user *ifr = argp; 955 unsigned int __user *up = argp; 956 unsigned short u; 957 int __user *sp = argp; 958 struct sockaddr sa; 959 int s; 960 int ret; 961 962 switch (cmd) { 963 case TUNSETIFF: 964 /* ignore the name, just look at flags */ 965 if (get_user(u, &ifr->ifr_flags)) 966 return -EFAULT; 967 968 ret = 0; 969 if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP)) 970 ret = -EINVAL; 971 else 972 q->flags = (q->flags & ~TAP_IFFEATURES) | u; 973 974 return ret; 975 976 case TUNGETIFF: 977 rtnl_lock(); 978 vlan = tap_get_vlan(q); 979 if (!vlan) { 980 rtnl_unlock(); 981 return -ENOLINK; 982 } 983 984 ret = 0; 985 u = q->flags; 986 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) || 987 put_user(u, &ifr->ifr_flags)) 988 ret = -EFAULT; 989 tap_put_vlan(vlan); 990 rtnl_unlock(); 991 return ret; 992 993 case TUNSETQUEUE: 994 if (get_user(u, &ifr->ifr_flags)) 995 return -EFAULT; 996 rtnl_lock(); 997 ret = tap_ioctl_set_queue(file, u); 998 rtnl_unlock(); 999 return ret; 1000 1001 case TUNGETFEATURES: 1002 if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up)) 1003 return -EFAULT; 1004 return 0; 1005 1006 case TUNSETSNDBUF: 1007 if (get_user(s, sp)) 1008 return -EFAULT; 1009 1010 q->sk.sk_sndbuf = s; 1011 return 0; 1012 1013 case TUNGETVNETHDRSZ: 1014 s = q->vnet_hdr_sz; 1015 if (put_user(s, sp)) 1016 return -EFAULT; 1017 return 0; 1018 1019 case TUNSETVNETHDRSZ: 1020 if (get_user(s, sp)) 1021 return -EFAULT; 1022 if (s < (int)sizeof(struct virtio_net_hdr)) 1023 return -EINVAL; 1024 1025 q->vnet_hdr_sz = s; 1026 return 0; 1027 1028 case TUNGETVNETLE: 1029 s = !!(q->flags & TAP_VNET_LE); 1030 if (put_user(s, sp)) 1031 return -EFAULT; 1032 return 0; 1033 1034 case TUNSETVNETLE: 1035 if (get_user(s, sp)) 1036 return -EFAULT; 1037 if (s) 1038 q->flags |= TAP_VNET_LE; 1039 else 1040 q->flags &= ~TAP_VNET_LE; 1041 return 0; 1042 1043 case TUNGETVNETBE: 1044 return tap_get_vnet_be(q, sp); 1045 1046 case TUNSETVNETBE: 1047 return tap_set_vnet_be(q, sp); 1048 1049 case TUNSETOFFLOAD: 1050 /* let the user check for future flags */ 1051 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | 1052 TUN_F_TSO_ECN | TUN_F_UFO)) 1053 return -EINVAL; 1054 1055 rtnl_lock(); 1056 ret = set_offload(q, arg); 1057 rtnl_unlock(); 1058 return ret; 1059 1060 case SIOCGIFHWADDR: 1061 rtnl_lock(); 1062 vlan = tap_get_vlan(q); 1063 if (!vlan) { 1064 rtnl_unlock(); 1065 return -ENOLINK; 1066 } 1067 ret = 0; 1068 u = vlan->dev->type; 1069 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) || 1070 copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) || 1071 put_user(u, &ifr->ifr_hwaddr.sa_family)) 1072 ret = -EFAULT; 1073 tap_put_vlan(vlan); 1074 rtnl_unlock(); 1075 return ret; 1076 1077 case SIOCSIFHWADDR: 1078 if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa))) 1079 return -EFAULT; 1080 rtnl_lock(); 1081 vlan = tap_get_vlan(q); 1082 if (!vlan) { 1083 rtnl_unlock(); 1084 return -ENOLINK; 1085 } 1086 ret = dev_set_mac_address(vlan->dev, &sa); 1087 tap_put_vlan(vlan); 1088 rtnl_unlock(); 1089 return ret; 1090 1091 default: 1092 return -EINVAL; 1093 } 1094 } 1095 1096 #ifdef CONFIG_COMPAT 1097 static long tap_compat_ioctl(struct file *file, unsigned int cmd, 1098 unsigned long arg) 1099 { 1100 return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1101 } 1102 #endif 1103 1104 const struct file_operations tap_fops = { 1105 .owner = THIS_MODULE, 1106 .open = tap_open, 1107 .release = tap_release, 1108 .read_iter = tap_read_iter, 1109 .write_iter = tap_write_iter, 1110 .poll = tap_poll, 1111 .llseek = no_llseek, 1112 .unlocked_ioctl = tap_ioctl, 1113 #ifdef CONFIG_COMPAT 1114 .compat_ioctl = tap_compat_ioctl, 1115 #endif 1116 }; 1117 1118 static int tap_sendmsg(struct socket *sock, struct msghdr *m, 1119 size_t total_len) 1120 { 1121 struct tap_queue *q = container_of(sock, struct tap_queue, sock); 1122 return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT); 1123 } 1124 1125 static int tap_recvmsg(struct socket *sock, struct msghdr *m, 1126 size_t total_len, int flags) 1127 { 1128 struct tap_queue *q = container_of(sock, struct tap_queue, sock); 1129 int ret; 1130 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) 1131 return -EINVAL; 1132 ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT); 1133 if (ret > total_len) { 1134 m->msg_flags |= MSG_TRUNC; 1135 ret = flags & MSG_TRUNC ? ret : total_len; 1136 } 1137 return ret; 1138 } 1139 1140 static int tap_peek_len(struct socket *sock) 1141 { 1142 struct tap_queue *q = container_of(sock, struct tap_queue, 1143 sock); 1144 return skb_array_peek_len(&q->skb_array); 1145 } 1146 1147 /* Ops structure to mimic raw sockets with tun */ 1148 static const struct proto_ops tap_socket_ops = { 1149 .sendmsg = tap_sendmsg, 1150 .recvmsg = tap_recvmsg, 1151 .peek_len = tap_peek_len, 1152 }; 1153 1154 /* Get an underlying socket object from tun file. Returns error unless file is 1155 * attached to a device. The returned object works like a packet socket, it 1156 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 1157 * holding a reference to the file for as long as the socket is in use. */ 1158 struct socket *tap_get_socket(struct file *file) 1159 { 1160 struct tap_queue *q; 1161 if (file->f_op != &tap_fops) 1162 return ERR_PTR(-EINVAL); 1163 q = file->private_data; 1164 if (!q) 1165 return ERR_PTR(-EBADFD); 1166 return &q->sock; 1167 } 1168 EXPORT_SYMBOL_GPL(tap_get_socket); 1169 1170 int tap_queue_resize(struct macvlan_dev *vlan) 1171 { 1172 struct net_device *dev = vlan->dev; 1173 struct tap_queue *q; 1174 struct skb_array **arrays; 1175 int n = vlan->numqueues; 1176 int ret, i = 0; 1177 1178 arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); 1179 if (!arrays) 1180 return -ENOMEM; 1181 1182 list_for_each_entry(q, &vlan->queue_list, next) 1183 arrays[i++] = &q->skb_array; 1184 1185 ret = skb_array_resize_multiple(arrays, n, 1186 dev->tx_queue_len, GFP_KERNEL); 1187 1188 kfree(arrays); 1189 return ret; 1190 } 1191 1192 int tap_create_cdev(struct cdev *tap_cdev, 1193 dev_t *tap_major, const char *device_name) 1194 { 1195 int err; 1196 1197 err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name); 1198 if (err) 1199 goto out1; 1200 1201 cdev_init(tap_cdev, &tap_fops); 1202 err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS); 1203 if (err) 1204 goto out2; 1205 1206 macvtap_major.major = MAJOR(*tap_major); 1207 1208 idr_init(&macvtap_major.minor_idr); 1209 mutex_init(&macvtap_major.minor_lock); 1210 1211 macvtap_major.device_name = device_name; 1212 1213 return 0; 1214 1215 out2: 1216 unregister_chrdev_region(*tap_major, TAP_NUM_DEVS); 1217 out1: 1218 return err; 1219 } 1220 1221 void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev) 1222 { 1223 cdev_del(tap_cdev); 1224 unregister_chrdev_region(major, TAP_NUM_DEVS); 1225 idr_destroy(&macvtap_major.minor_idr); 1226 } 1227