0001
0002 #include <linux/etherdevice.h>
0003 #include <linux/if_tap.h>
0004 #include <linux/if_vlan.h>
0005 #include <linux/interrupt.h>
0006 #include <linux/nsproxy.h>
0007 #include <linux/compat.h>
0008 #include <linux/if_tun.h>
0009 #include <linux/module.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/cache.h>
0012 #include <linux/sched/signal.h>
0013 #include <linux/types.h>
0014 #include <linux/slab.h>
0015 #include <linux/wait.h>
0016 #include <linux/cdev.h>
0017 #include <linux/idr.h>
0018 #include <linux/fs.h>
0019 #include <linux/uio.h>
0020
0021 #include <net/net_namespace.h>
0022 #include <net/rtnetlink.h>
0023 #include <net/sock.h>
0024 #include <linux/virtio_net.h>
0025 #include <linux/skb_array.h>
0026
0027 #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
0028
0029 #define TAP_VNET_LE 0x80000000
0030 #define TAP_VNET_BE 0x40000000
0031
0032 #ifdef CONFIG_TUN_VNET_CROSS_LE
0033 static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
0034 {
0035 return q->flags & TAP_VNET_BE ? false :
0036 virtio_legacy_is_little_endian();
0037 }
0038
0039 static long tap_get_vnet_be(struct tap_queue *q, int __user *sp)
0040 {
0041 int s = !!(q->flags & TAP_VNET_BE);
0042
0043 if (put_user(s, sp))
0044 return -EFAULT;
0045
0046 return 0;
0047 }
0048
0049 static long tap_set_vnet_be(struct tap_queue *q, int __user *sp)
0050 {
0051 int s;
0052
0053 if (get_user(s, sp))
0054 return -EFAULT;
0055
0056 if (s)
0057 q->flags |= TAP_VNET_BE;
0058 else
0059 q->flags &= ~TAP_VNET_BE;
0060
0061 return 0;
0062 }
0063 #else
0064 static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
0065 {
0066 return virtio_legacy_is_little_endian();
0067 }
0068
0069 static long tap_get_vnet_be(struct tap_queue *q, int __user *argp)
0070 {
0071 return -EINVAL;
0072 }
0073
0074 static long tap_set_vnet_be(struct tap_queue *q, int __user *argp)
0075 {
0076 return -EINVAL;
0077 }
0078 #endif
0079
0080 static inline bool tap_is_little_endian(struct tap_queue *q)
0081 {
0082 return q->flags & TAP_VNET_LE ||
0083 tap_legacy_is_little_endian(q);
0084 }
0085
0086 static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val)
0087 {
0088 return __virtio16_to_cpu(tap_is_little_endian(q), val);
0089 }
0090
0091 static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val)
0092 {
0093 return __cpu_to_virtio16(tap_is_little_endian(q), val);
0094 }
0095
0096 static struct proto tap_proto = {
0097 .name = "tap",
0098 .owner = THIS_MODULE,
0099 .obj_size = sizeof(struct tap_queue),
0100 };
0101
0102 #define TAP_NUM_DEVS (1U << MINORBITS)
0103
0104 static LIST_HEAD(major_list);
0105
0106 struct major_info {
0107 struct rcu_head rcu;
0108 dev_t major;
0109 struct idr minor_idr;
0110 spinlock_t minor_lock;
0111 const char *device_name;
0112 struct list_head next;
0113 };
0114
0115 #define GOODCOPY_LEN 128
0116
0117 static const struct proto_ops tap_socket_ops;
0118
0119 #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
0120 #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
0121
0122 static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
0123 {
0124 return rcu_dereference(dev->rx_handler_data);
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 static int tap_enable_queue(struct tap_dev *tap, struct file *file,
0145 struct tap_queue *q)
0146 {
0147 int err = -EINVAL;
0148
0149 ASSERT_RTNL();
0150
0151 if (q->enabled)
0152 goto out;
0153
0154 err = 0;
0155 rcu_assign_pointer(tap->taps[tap->numvtaps], q);
0156 q->queue_index = tap->numvtaps;
0157 q->enabled = true;
0158
0159 tap->numvtaps++;
0160 out:
0161 return err;
0162 }
0163
0164
0165 static int tap_set_queue(struct tap_dev *tap, struct file *file,
0166 struct tap_queue *q)
0167 {
0168 if (tap->numqueues == MAX_TAP_QUEUES)
0169 return -EBUSY;
0170
0171 rcu_assign_pointer(q->tap, tap);
0172 rcu_assign_pointer(tap->taps[tap->numvtaps], q);
0173 sock_hold(&q->sk);
0174
0175 q->file = file;
0176 q->queue_index = tap->numvtaps;
0177 q->enabled = true;
0178 file->private_data = q;
0179 list_add_tail(&q->next, &tap->queue_list);
0180
0181 tap->numvtaps++;
0182 tap->numqueues++;
0183
0184 return 0;
0185 }
0186
0187 static int tap_disable_queue(struct tap_queue *q)
0188 {
0189 struct tap_dev *tap;
0190 struct tap_queue *nq;
0191
0192 ASSERT_RTNL();
0193 if (!q->enabled)
0194 return -EINVAL;
0195
0196 tap = rtnl_dereference(q->tap);
0197
0198 if (tap) {
0199 int index = q->queue_index;
0200 BUG_ON(index >= tap->numvtaps);
0201 nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
0202 nq->queue_index = index;
0203
0204 rcu_assign_pointer(tap->taps[index], nq);
0205 RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
0206 q->enabled = false;
0207
0208 tap->numvtaps--;
0209 }
0210
0211 return 0;
0212 }
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 static void tap_put_queue(struct tap_queue *q)
0223 {
0224 struct tap_dev *tap;
0225
0226 rtnl_lock();
0227 tap = rtnl_dereference(q->tap);
0228
0229 if (tap) {
0230 if (q->enabled)
0231 BUG_ON(tap_disable_queue(q));
0232
0233 tap->numqueues--;
0234 RCU_INIT_POINTER(q->tap, NULL);
0235 sock_put(&q->sk);
0236 list_del_init(&q->next);
0237 }
0238
0239 rtnl_unlock();
0240
0241 synchronize_rcu();
0242 sock_put(&q->sk);
0243 }
0244
0245
0246
0247
0248
0249
0250
0251
0252 static struct tap_queue *tap_get_queue(struct tap_dev *tap,
0253 struct sk_buff *skb)
0254 {
0255 struct tap_queue *queue = NULL;
0256
0257
0258
0259
0260
0261 int numvtaps = READ_ONCE(tap->numvtaps);
0262 __u32 rxq;
0263
0264 if (!numvtaps)
0265 goto out;
0266
0267 if (numvtaps == 1)
0268 goto single;
0269
0270
0271 rxq = skb_get_hash(skb);
0272 if (rxq) {
0273 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
0274 goto out;
0275 }
0276
0277 if (likely(skb_rx_queue_recorded(skb))) {
0278 rxq = skb_get_rx_queue(skb);
0279
0280 while (unlikely(rxq >= numvtaps))
0281 rxq -= numvtaps;
0282
0283 queue = rcu_dereference(tap->taps[rxq]);
0284 goto out;
0285 }
0286
0287 single:
0288 queue = rcu_dereference(tap->taps[0]);
0289 out:
0290 return queue;
0291 }
0292
0293
0294
0295
0296
0297
0298 void tap_del_queues(struct tap_dev *tap)
0299 {
0300 struct tap_queue *q, *tmp;
0301
0302 ASSERT_RTNL();
0303 list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
0304 list_del_init(&q->next);
0305 RCU_INIT_POINTER(q->tap, NULL);
0306 if (q->enabled)
0307 tap->numvtaps--;
0308 tap->numqueues--;
0309 sock_put(&q->sk);
0310 }
0311 BUG_ON(tap->numvtaps);
0312 BUG_ON(tap->numqueues);
0313
0314 tap->numvtaps = MAX_TAP_QUEUES;
0315 }
0316 EXPORT_SYMBOL_GPL(tap_del_queues);
0317
0318 rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
0319 {
0320 struct sk_buff *skb = *pskb;
0321 struct net_device *dev = skb->dev;
0322 struct tap_dev *tap;
0323 struct tap_queue *q;
0324 netdev_features_t features = TAP_FEATURES;
0325 enum skb_drop_reason drop_reason;
0326
0327 tap = tap_dev_get_rcu(dev);
0328 if (!tap)
0329 return RX_HANDLER_PASS;
0330
0331 q = tap_get_queue(tap, skb);
0332 if (!q)
0333 return RX_HANDLER_PASS;
0334
0335 skb_push(skb, ETH_HLEN);
0336
0337
0338
0339
0340
0341 if (q->flags & IFF_VNET_HDR)
0342 features |= tap->tap_features;
0343 if (netif_needs_gso(skb, features)) {
0344 struct sk_buff *segs = __skb_gso_segment(skb, features, false);
0345 struct sk_buff *next;
0346
0347 if (IS_ERR(segs)) {
0348 drop_reason = SKB_DROP_REASON_SKB_GSO_SEG;
0349 goto drop;
0350 }
0351
0352 if (!segs) {
0353 if (ptr_ring_produce(&q->ring, skb)) {
0354 drop_reason = SKB_DROP_REASON_FULL_RING;
0355 goto drop;
0356 }
0357 goto wake_up;
0358 }
0359
0360 consume_skb(skb);
0361 skb_list_walk_safe(segs, skb, next) {
0362 skb_mark_not_on_list(skb);
0363 if (ptr_ring_produce(&q->ring, skb)) {
0364 drop_reason = SKB_DROP_REASON_FULL_RING;
0365 kfree_skb_reason(skb, drop_reason);
0366 kfree_skb_list_reason(next, drop_reason);
0367 break;
0368 }
0369 }
0370 } else {
0371
0372
0373
0374
0375
0376 if (skb->ip_summed == CHECKSUM_PARTIAL &&
0377 !(features & NETIF_F_CSUM_MASK) &&
0378 skb_checksum_help(skb)) {
0379 drop_reason = SKB_DROP_REASON_SKB_CSUM;
0380 goto drop;
0381 }
0382 if (ptr_ring_produce(&q->ring, skb)) {
0383 drop_reason = SKB_DROP_REASON_FULL_RING;
0384 goto drop;
0385 }
0386 }
0387
0388 wake_up:
0389 wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
0390 return RX_HANDLER_CONSUMED;
0391
0392 drop:
0393
0394 if (tap->count_rx_dropped)
0395 tap->count_rx_dropped(tap);
0396 kfree_skb_reason(skb, drop_reason);
0397 return RX_HANDLER_CONSUMED;
0398 }
0399 EXPORT_SYMBOL_GPL(tap_handle_frame);
0400
0401 static struct major_info *tap_get_major(int major)
0402 {
0403 struct major_info *tap_major;
0404
0405 list_for_each_entry_rcu(tap_major, &major_list, next) {
0406 if (tap_major->major == major)
0407 return tap_major;
0408 }
0409
0410 return NULL;
0411 }
0412
0413 int tap_get_minor(dev_t major, struct tap_dev *tap)
0414 {
0415 int retval = -ENOMEM;
0416 struct major_info *tap_major;
0417
0418 rcu_read_lock();
0419 tap_major = tap_get_major(MAJOR(major));
0420 if (!tap_major) {
0421 retval = -EINVAL;
0422 goto unlock;
0423 }
0424
0425 spin_lock(&tap_major->minor_lock);
0426 retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
0427 if (retval >= 0) {
0428 tap->minor = retval;
0429 } else if (retval == -ENOSPC) {
0430 netdev_err(tap->dev, "Too many tap devices\n");
0431 retval = -EINVAL;
0432 }
0433 spin_unlock(&tap_major->minor_lock);
0434
0435 unlock:
0436 rcu_read_unlock();
0437 return retval < 0 ? retval : 0;
0438 }
0439 EXPORT_SYMBOL_GPL(tap_get_minor);
0440
0441 void tap_free_minor(dev_t major, struct tap_dev *tap)
0442 {
0443 struct major_info *tap_major;
0444
0445 rcu_read_lock();
0446 tap_major = tap_get_major(MAJOR(major));
0447 if (!tap_major) {
0448 goto unlock;
0449 }
0450
0451 spin_lock(&tap_major->minor_lock);
0452 if (tap->minor) {
0453 idr_remove(&tap_major->minor_idr, tap->minor);
0454 tap->minor = 0;
0455 }
0456 spin_unlock(&tap_major->minor_lock);
0457
0458 unlock:
0459 rcu_read_unlock();
0460 }
0461 EXPORT_SYMBOL_GPL(tap_free_minor);
0462
0463 static struct tap_dev *dev_get_by_tap_file(int major, int minor)
0464 {
0465 struct net_device *dev = NULL;
0466 struct tap_dev *tap;
0467 struct major_info *tap_major;
0468
0469 rcu_read_lock();
0470 tap_major = tap_get_major(major);
0471 if (!tap_major) {
0472 tap = NULL;
0473 goto unlock;
0474 }
0475
0476 spin_lock(&tap_major->minor_lock);
0477 tap = idr_find(&tap_major->minor_idr, minor);
0478 if (tap) {
0479 dev = tap->dev;
0480 dev_hold(dev);
0481 }
0482 spin_unlock(&tap_major->minor_lock);
0483
0484 unlock:
0485 rcu_read_unlock();
0486 return tap;
0487 }
0488
0489 static void tap_sock_write_space(struct sock *sk)
0490 {
0491 wait_queue_head_t *wqueue;
0492
0493 if (!sock_writeable(sk) ||
0494 !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
0495 return;
0496
0497 wqueue = sk_sleep(sk);
0498 if (wqueue && waitqueue_active(wqueue))
0499 wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
0500 }
0501
0502 static void tap_sock_destruct(struct sock *sk)
0503 {
0504 struct tap_queue *q = container_of(sk, struct tap_queue, sk);
0505
0506 ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
0507 }
0508
0509 static int tap_open(struct inode *inode, struct file *file)
0510 {
0511 struct net *net = current->nsproxy->net_ns;
0512 struct tap_dev *tap;
0513 struct tap_queue *q;
0514 int err = -ENODEV;
0515
0516 rtnl_lock();
0517 tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
0518 if (!tap)
0519 goto err;
0520
0521 err = -ENOMEM;
0522 q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
0523 &tap_proto, 0);
0524 if (!q)
0525 goto err;
0526 if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
0527 sk_free(&q->sk);
0528 goto err;
0529 }
0530
0531 init_waitqueue_head(&q->sock.wq.wait);
0532 q->sock.type = SOCK_RAW;
0533 q->sock.state = SS_CONNECTED;
0534 q->sock.file = file;
0535 q->sock.ops = &tap_socket_ops;
0536 sock_init_data(&q->sock, &q->sk);
0537 q->sk.sk_write_space = tap_sock_write_space;
0538 q->sk.sk_destruct = tap_sock_destruct;
0539 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
0540 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
0541
0542
0543
0544
0545
0546
0547
0548
0549 if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
0550 sock_set_flag(&q->sk, SOCK_ZEROCOPY);
0551
0552 err = tap_set_queue(tap, file, q);
0553 if (err) {
0554
0555 goto err_put;
0556 }
0557
0558 dev_put(tap->dev);
0559
0560 rtnl_unlock();
0561 return err;
0562
0563 err_put:
0564 sock_put(&q->sk);
0565 err:
0566 if (tap)
0567 dev_put(tap->dev);
0568
0569 rtnl_unlock();
0570 return err;
0571 }
0572
0573 static int tap_release(struct inode *inode, struct file *file)
0574 {
0575 struct tap_queue *q = file->private_data;
0576 tap_put_queue(q);
0577 return 0;
0578 }
0579
0580 static __poll_t tap_poll(struct file *file, poll_table *wait)
0581 {
0582 struct tap_queue *q = file->private_data;
0583 __poll_t mask = EPOLLERR;
0584
0585 if (!q)
0586 goto out;
0587
0588 mask = 0;
0589 poll_wait(file, &q->sock.wq.wait, wait);
0590
0591 if (!ptr_ring_empty(&q->ring))
0592 mask |= EPOLLIN | EPOLLRDNORM;
0593
0594 if (sock_writeable(&q->sk) ||
0595 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
0596 sock_writeable(&q->sk)))
0597 mask |= EPOLLOUT | EPOLLWRNORM;
0598
0599 out:
0600 return mask;
0601 }
0602
0603 static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
0604 size_t len, size_t linear,
0605 int noblock, int *err)
0606 {
0607 struct sk_buff *skb;
0608
0609
0610 if (prepad + len < PAGE_SIZE || !linear)
0611 linear = len;
0612
0613 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
0614 err, 0);
0615 if (!skb)
0616 return NULL;
0617
0618 skb_reserve(skb, prepad);
0619 skb_put(skb, linear);
0620 skb->data_len = len - linear;
0621 skb->len += len - linear;
0622
0623 return skb;
0624 }
0625
0626
0627 #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
0628
0629
0630 static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
0631 struct iov_iter *from, int noblock)
0632 {
0633 int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
0634 struct sk_buff *skb;
0635 struct tap_dev *tap;
0636 unsigned long total_len = iov_iter_count(from);
0637 unsigned long len = total_len;
0638 int err;
0639 struct virtio_net_hdr vnet_hdr = { 0 };
0640 int vnet_hdr_len = 0;
0641 int copylen = 0;
0642 int depth;
0643 bool zerocopy = false;
0644 size_t linear;
0645 enum skb_drop_reason drop_reason;
0646
0647 if (q->flags & IFF_VNET_HDR) {
0648 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
0649
0650 err = -EINVAL;
0651 if (len < vnet_hdr_len)
0652 goto err;
0653 len -= vnet_hdr_len;
0654
0655 err = -EFAULT;
0656 if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
0657 goto err;
0658 iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
0659 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
0660 tap16_to_cpu(q, vnet_hdr.csum_start) +
0661 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
0662 tap16_to_cpu(q, vnet_hdr.hdr_len))
0663 vnet_hdr.hdr_len = cpu_to_tap16(q,
0664 tap16_to_cpu(q, vnet_hdr.csum_start) +
0665 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
0666 err = -EINVAL;
0667 if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
0668 goto err;
0669 }
0670
0671 err = -EINVAL;
0672 if (unlikely(len < ETH_HLEN))
0673 goto err;
0674
0675 if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
0676 struct iov_iter i;
0677
0678 copylen = vnet_hdr.hdr_len ?
0679 tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
0680 if (copylen > good_linear)
0681 copylen = good_linear;
0682 else if (copylen < ETH_HLEN)
0683 copylen = ETH_HLEN;
0684 linear = copylen;
0685 i = *from;
0686 iov_iter_advance(&i, copylen);
0687 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
0688 zerocopy = true;
0689 }
0690
0691 if (!zerocopy) {
0692 copylen = len;
0693 linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
0694 if (linear > good_linear)
0695 linear = good_linear;
0696 else if (linear < ETH_HLEN)
0697 linear = ETH_HLEN;
0698 }
0699
0700 skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
0701 linear, noblock, &err);
0702 if (!skb)
0703 goto err;
0704
0705 if (zerocopy)
0706 err = zerocopy_sg_from_iter(skb, from);
0707 else
0708 err = skb_copy_datagram_from_iter(skb, 0, from, len);
0709
0710 if (err) {
0711 drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
0712 goto err_kfree;
0713 }
0714
0715 skb_set_network_header(skb, ETH_HLEN);
0716 skb_reset_mac_header(skb);
0717 skb->protocol = eth_hdr(skb)->h_proto;
0718
0719 rcu_read_lock();
0720 tap = rcu_dereference(q->tap);
0721 if (!tap) {
0722 kfree_skb(skb);
0723 rcu_read_unlock();
0724 return total_len;
0725 }
0726 skb->dev = tap->dev;
0727
0728 if (vnet_hdr_len) {
0729 err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
0730 tap_is_little_endian(q));
0731 if (err) {
0732 rcu_read_unlock();
0733 drop_reason = SKB_DROP_REASON_DEV_HDR;
0734 goto err_kfree;
0735 }
0736 }
0737
0738 skb_probe_transport_header(skb);
0739
0740
0741 if (eth_type_vlan(skb->protocol) &&
0742 __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
0743 skb_set_network_header(skb, depth);
0744
0745
0746 if (zerocopy) {
0747 skb_zcopy_init(skb, msg_control);
0748 } else if (msg_control) {
0749 struct ubuf_info *uarg = msg_control;
0750 uarg->callback(NULL, uarg, false);
0751 }
0752
0753 dev_queue_xmit(skb);
0754 rcu_read_unlock();
0755 return total_len;
0756
0757 err_kfree:
0758 kfree_skb_reason(skb, drop_reason);
0759
0760 err:
0761 rcu_read_lock();
0762 tap = rcu_dereference(q->tap);
0763 if (tap && tap->count_tx_dropped)
0764 tap->count_tx_dropped(tap);
0765 rcu_read_unlock();
0766
0767 return err;
0768 }
0769
0770 static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
0771 {
0772 struct file *file = iocb->ki_filp;
0773 struct tap_queue *q = file->private_data;
0774
0775 return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
0776 }
0777
0778
0779 static ssize_t tap_put_user(struct tap_queue *q,
0780 const struct sk_buff *skb,
0781 struct iov_iter *iter)
0782 {
0783 int ret;
0784 int vnet_hdr_len = 0;
0785 int vlan_offset = 0;
0786 int total;
0787
0788 if (q->flags & IFF_VNET_HDR) {
0789 int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
0790 struct virtio_net_hdr vnet_hdr;
0791
0792 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
0793 if (iov_iter_count(iter) < vnet_hdr_len)
0794 return -EINVAL;
0795
0796 if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
0797 tap_is_little_endian(q), true,
0798 vlan_hlen))
0799 BUG();
0800
0801 if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
0802 sizeof(vnet_hdr))
0803 return -EFAULT;
0804
0805 iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
0806 }
0807 total = vnet_hdr_len;
0808 total += skb->len;
0809
0810 if (skb_vlan_tag_present(skb)) {
0811 struct {
0812 __be16 h_vlan_proto;
0813 __be16 h_vlan_TCI;
0814 } veth;
0815 veth.h_vlan_proto = skb->vlan_proto;
0816 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
0817
0818 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
0819 total += VLAN_HLEN;
0820
0821 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
0822 if (ret || !iov_iter_count(iter))
0823 goto done;
0824
0825 ret = copy_to_iter(&veth, sizeof(veth), iter);
0826 if (ret != sizeof(veth) || !iov_iter_count(iter))
0827 goto done;
0828 }
0829
0830 ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
0831 skb->len - vlan_offset);
0832
0833 done:
0834 return ret ? ret : total;
0835 }
0836
0837 static ssize_t tap_do_read(struct tap_queue *q,
0838 struct iov_iter *to,
0839 int noblock, struct sk_buff *skb)
0840 {
0841 DEFINE_WAIT(wait);
0842 ssize_t ret = 0;
0843
0844 if (!iov_iter_count(to)) {
0845 kfree_skb(skb);
0846 return 0;
0847 }
0848
0849 if (skb)
0850 goto put;
0851
0852 while (1) {
0853 if (!noblock)
0854 prepare_to_wait(sk_sleep(&q->sk), &wait,
0855 TASK_INTERRUPTIBLE);
0856
0857
0858 skb = ptr_ring_consume(&q->ring);
0859 if (skb)
0860 break;
0861 if (noblock) {
0862 ret = -EAGAIN;
0863 break;
0864 }
0865 if (signal_pending(current)) {
0866 ret = -ERESTARTSYS;
0867 break;
0868 }
0869
0870 schedule();
0871 }
0872 if (!noblock)
0873 finish_wait(sk_sleep(&q->sk), &wait);
0874
0875 put:
0876 if (skb) {
0877 ret = tap_put_user(q, skb, to);
0878 if (unlikely(ret < 0))
0879 kfree_skb(skb);
0880 else
0881 consume_skb(skb);
0882 }
0883 return ret;
0884 }
0885
0886 static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
0887 {
0888 struct file *file = iocb->ki_filp;
0889 struct tap_queue *q = file->private_data;
0890 ssize_t len = iov_iter_count(to), ret;
0891
0892 ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
0893 ret = min_t(ssize_t, ret, len);
0894 if (ret > 0)
0895 iocb->ki_pos = ret;
0896 return ret;
0897 }
0898
0899 static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
0900 {
0901 struct tap_dev *tap;
0902
0903 ASSERT_RTNL();
0904 tap = rtnl_dereference(q->tap);
0905 if (tap)
0906 dev_hold(tap->dev);
0907
0908 return tap;
0909 }
0910
0911 static void tap_put_tap_dev(struct tap_dev *tap)
0912 {
0913 dev_put(tap->dev);
0914 }
0915
0916 static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
0917 {
0918 struct tap_queue *q = file->private_data;
0919 struct tap_dev *tap;
0920 int ret;
0921
0922 tap = tap_get_tap_dev(q);
0923 if (!tap)
0924 return -EINVAL;
0925
0926 if (flags & IFF_ATTACH_QUEUE)
0927 ret = tap_enable_queue(tap, file, q);
0928 else if (flags & IFF_DETACH_QUEUE)
0929 ret = tap_disable_queue(q);
0930 else
0931 ret = -EINVAL;
0932
0933 tap_put_tap_dev(tap);
0934 return ret;
0935 }
0936
0937 static int set_offload(struct tap_queue *q, unsigned long arg)
0938 {
0939 struct tap_dev *tap;
0940 netdev_features_t features;
0941 netdev_features_t feature_mask = 0;
0942
0943 tap = rtnl_dereference(q->tap);
0944 if (!tap)
0945 return -ENOLINK;
0946
0947 features = tap->dev->features;
0948
0949 if (arg & TUN_F_CSUM) {
0950 feature_mask = NETIF_F_HW_CSUM;
0951
0952 if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
0953 if (arg & TUN_F_TSO_ECN)
0954 feature_mask |= NETIF_F_TSO_ECN;
0955 if (arg & TUN_F_TSO4)
0956 feature_mask |= NETIF_F_TSO;
0957 if (arg & TUN_F_TSO6)
0958 feature_mask |= NETIF_F_TSO6;
0959 }
0960 }
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970 if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
0971 features |= RX_OFFLOADS;
0972 else
0973 features &= ~RX_OFFLOADS;
0974
0975
0976
0977
0978 tap->tap_features = feature_mask;
0979 if (tap->update_features)
0980 tap->update_features(tap, features);
0981
0982 return 0;
0983 }
0984
0985
0986
0987
0988 static long tap_ioctl(struct file *file, unsigned int cmd,
0989 unsigned long arg)
0990 {
0991 struct tap_queue *q = file->private_data;
0992 struct tap_dev *tap;
0993 void __user *argp = (void __user *)arg;
0994 struct ifreq __user *ifr = argp;
0995 unsigned int __user *up = argp;
0996 unsigned short u;
0997 int __user *sp = argp;
0998 struct sockaddr sa;
0999 int s;
1000 int ret;
1001
1002 switch (cmd) {
1003 case TUNSETIFF:
1004
1005 if (get_user(u, &ifr->ifr_flags))
1006 return -EFAULT;
1007
1008 ret = 0;
1009 if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
1010 ret = -EINVAL;
1011 else
1012 q->flags = (q->flags & ~TAP_IFFEATURES) | u;
1013
1014 return ret;
1015
1016 case TUNGETIFF:
1017 rtnl_lock();
1018 tap = tap_get_tap_dev(q);
1019 if (!tap) {
1020 rtnl_unlock();
1021 return -ENOLINK;
1022 }
1023
1024 ret = 0;
1025 u = q->flags;
1026 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
1027 put_user(u, &ifr->ifr_flags))
1028 ret = -EFAULT;
1029 tap_put_tap_dev(tap);
1030 rtnl_unlock();
1031 return ret;
1032
1033 case TUNSETQUEUE:
1034 if (get_user(u, &ifr->ifr_flags))
1035 return -EFAULT;
1036 rtnl_lock();
1037 ret = tap_ioctl_set_queue(file, u);
1038 rtnl_unlock();
1039 return ret;
1040
1041 case TUNGETFEATURES:
1042 if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
1043 return -EFAULT;
1044 return 0;
1045
1046 case TUNSETSNDBUF:
1047 if (get_user(s, sp))
1048 return -EFAULT;
1049 if (s <= 0)
1050 return -EINVAL;
1051
1052 q->sk.sk_sndbuf = s;
1053 return 0;
1054
1055 case TUNGETVNETHDRSZ:
1056 s = q->vnet_hdr_sz;
1057 if (put_user(s, sp))
1058 return -EFAULT;
1059 return 0;
1060
1061 case TUNSETVNETHDRSZ:
1062 if (get_user(s, sp))
1063 return -EFAULT;
1064 if (s < (int)sizeof(struct virtio_net_hdr))
1065 return -EINVAL;
1066
1067 q->vnet_hdr_sz = s;
1068 return 0;
1069
1070 case TUNGETVNETLE:
1071 s = !!(q->flags & TAP_VNET_LE);
1072 if (put_user(s, sp))
1073 return -EFAULT;
1074 return 0;
1075
1076 case TUNSETVNETLE:
1077 if (get_user(s, sp))
1078 return -EFAULT;
1079 if (s)
1080 q->flags |= TAP_VNET_LE;
1081 else
1082 q->flags &= ~TAP_VNET_LE;
1083 return 0;
1084
1085 case TUNGETVNETBE:
1086 return tap_get_vnet_be(q, sp);
1087
1088 case TUNSETVNETBE:
1089 return tap_set_vnet_be(q, sp);
1090
1091 case TUNSETOFFLOAD:
1092
1093 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
1094 TUN_F_TSO_ECN | TUN_F_UFO))
1095 return -EINVAL;
1096
1097 rtnl_lock();
1098 ret = set_offload(q, arg);
1099 rtnl_unlock();
1100 return ret;
1101
1102 case SIOCGIFHWADDR:
1103 rtnl_lock();
1104 tap = tap_get_tap_dev(q);
1105 if (!tap) {
1106 rtnl_unlock();
1107 return -ENOLINK;
1108 }
1109 ret = 0;
1110 dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
1111 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
1112 copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
1113 ret = -EFAULT;
1114 tap_put_tap_dev(tap);
1115 rtnl_unlock();
1116 return ret;
1117
1118 case SIOCSIFHWADDR:
1119 if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
1120 return -EFAULT;
1121 rtnl_lock();
1122 tap = tap_get_tap_dev(q);
1123 if (!tap) {
1124 rtnl_unlock();
1125 return -ENOLINK;
1126 }
1127 ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
1128 tap_put_tap_dev(tap);
1129 rtnl_unlock();
1130 return ret;
1131
1132 default:
1133 return -EINVAL;
1134 }
1135 }
1136
1137 static const struct file_operations tap_fops = {
1138 .owner = THIS_MODULE,
1139 .open = tap_open,
1140 .release = tap_release,
1141 .read_iter = tap_read_iter,
1142 .write_iter = tap_write_iter,
1143 .poll = tap_poll,
1144 .llseek = no_llseek,
1145 .unlocked_ioctl = tap_ioctl,
1146 .compat_ioctl = compat_ptr_ioctl,
1147 };
1148
1149 static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
1150 {
1151 struct tun_xdp_hdr *hdr = xdp->data_hard_start;
1152 struct virtio_net_hdr *gso = &hdr->gso;
1153 int buflen = hdr->buflen;
1154 int vnet_hdr_len = 0;
1155 struct tap_dev *tap;
1156 struct sk_buff *skb;
1157 int err, depth;
1158
1159 if (q->flags & IFF_VNET_HDR)
1160 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
1161
1162 skb = build_skb(xdp->data_hard_start, buflen);
1163 if (!skb) {
1164 err = -ENOMEM;
1165 goto err;
1166 }
1167
1168 skb_reserve(skb, xdp->data - xdp->data_hard_start);
1169 skb_put(skb, xdp->data_end - xdp->data);
1170
1171 skb_set_network_header(skb, ETH_HLEN);
1172 skb_reset_mac_header(skb);
1173 skb->protocol = eth_hdr(skb)->h_proto;
1174
1175 if (vnet_hdr_len) {
1176 err = virtio_net_hdr_to_skb(skb, gso, tap_is_little_endian(q));
1177 if (err)
1178 goto err_kfree;
1179 }
1180
1181
1182 if (eth_type_vlan(skb->protocol) &&
1183 __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
1184 skb_set_network_header(skb, depth);
1185
1186 rcu_read_lock();
1187 tap = rcu_dereference(q->tap);
1188 if (tap) {
1189 skb->dev = tap->dev;
1190 skb_probe_transport_header(skb);
1191 dev_queue_xmit(skb);
1192 } else {
1193 kfree_skb(skb);
1194 }
1195 rcu_read_unlock();
1196
1197 return 0;
1198
1199 err_kfree:
1200 kfree_skb(skb);
1201 err:
1202 rcu_read_lock();
1203 tap = rcu_dereference(q->tap);
1204 if (tap && tap->count_tx_dropped)
1205 tap->count_tx_dropped(tap);
1206 rcu_read_unlock();
1207 return err;
1208 }
1209
1210 static int tap_sendmsg(struct socket *sock, struct msghdr *m,
1211 size_t total_len)
1212 {
1213 struct tap_queue *q = container_of(sock, struct tap_queue, sock);
1214 struct tun_msg_ctl *ctl = m->msg_control;
1215 struct xdp_buff *xdp;
1216 int i;
1217
1218 if (m->msg_controllen == sizeof(struct tun_msg_ctl) &&
1219 ctl && ctl->type == TUN_MSG_PTR) {
1220 for (i = 0; i < ctl->num; i++) {
1221 xdp = &((struct xdp_buff *)ctl->ptr)[i];
1222 tap_get_user_xdp(q, xdp);
1223 }
1224 return 0;
1225 }
1226
1227 return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
1228 m->msg_flags & MSG_DONTWAIT);
1229 }
1230
1231 static int tap_recvmsg(struct socket *sock, struct msghdr *m,
1232 size_t total_len, int flags)
1233 {
1234 struct tap_queue *q = container_of(sock, struct tap_queue, sock);
1235 struct sk_buff *skb = m->msg_control;
1236 int ret;
1237 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
1238 kfree_skb(skb);
1239 return -EINVAL;
1240 }
1241 ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
1242 if (ret > total_len) {
1243 m->msg_flags |= MSG_TRUNC;
1244 ret = flags & MSG_TRUNC ? ret : total_len;
1245 }
1246 return ret;
1247 }
1248
1249 static int tap_peek_len(struct socket *sock)
1250 {
1251 struct tap_queue *q = container_of(sock, struct tap_queue,
1252 sock);
1253 return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
1254 }
1255
1256
1257 static const struct proto_ops tap_socket_ops = {
1258 .sendmsg = tap_sendmsg,
1259 .recvmsg = tap_recvmsg,
1260 .peek_len = tap_peek_len,
1261 };
1262
1263
1264
1265
1266
1267 struct socket *tap_get_socket(struct file *file)
1268 {
1269 struct tap_queue *q;
1270 if (file->f_op != &tap_fops)
1271 return ERR_PTR(-EINVAL);
1272 q = file->private_data;
1273 if (!q)
1274 return ERR_PTR(-EBADFD);
1275 return &q->sock;
1276 }
1277 EXPORT_SYMBOL_GPL(tap_get_socket);
1278
1279 struct ptr_ring *tap_get_ptr_ring(struct file *file)
1280 {
1281 struct tap_queue *q;
1282
1283 if (file->f_op != &tap_fops)
1284 return ERR_PTR(-EINVAL);
1285 q = file->private_data;
1286 if (!q)
1287 return ERR_PTR(-EBADFD);
1288 return &q->ring;
1289 }
1290 EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
1291
1292 int tap_queue_resize(struct tap_dev *tap)
1293 {
1294 struct net_device *dev = tap->dev;
1295 struct tap_queue *q;
1296 struct ptr_ring **rings;
1297 int n = tap->numqueues;
1298 int ret, i = 0;
1299
1300 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
1301 if (!rings)
1302 return -ENOMEM;
1303
1304 list_for_each_entry(q, &tap->queue_list, next)
1305 rings[i++] = &q->ring;
1306
1307 ret = ptr_ring_resize_multiple(rings, n,
1308 dev->tx_queue_len, GFP_KERNEL,
1309 __skb_array_destroy_skb);
1310
1311 kfree(rings);
1312 return ret;
1313 }
1314 EXPORT_SYMBOL_GPL(tap_queue_resize);
1315
1316 static int tap_list_add(dev_t major, const char *device_name)
1317 {
1318 struct major_info *tap_major;
1319
1320 tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
1321 if (!tap_major)
1322 return -ENOMEM;
1323
1324 tap_major->major = MAJOR(major);
1325
1326 idr_init(&tap_major->minor_idr);
1327 spin_lock_init(&tap_major->minor_lock);
1328
1329 tap_major->device_name = device_name;
1330
1331 list_add_tail_rcu(&tap_major->next, &major_list);
1332 return 0;
1333 }
1334
1335 int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
1336 const char *device_name, struct module *module)
1337 {
1338 int err;
1339
1340 err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
1341 if (err)
1342 goto out1;
1343
1344 cdev_init(tap_cdev, &tap_fops);
1345 tap_cdev->owner = module;
1346 err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
1347 if (err)
1348 goto out2;
1349
1350 err = tap_list_add(*tap_major, device_name);
1351 if (err)
1352 goto out3;
1353
1354 return 0;
1355
1356 out3:
1357 cdev_del(tap_cdev);
1358 out2:
1359 unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
1360 out1:
1361 return err;
1362 }
1363 EXPORT_SYMBOL_GPL(tap_create_cdev);
1364
1365 void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
1366 {
1367 struct major_info *tap_major, *tmp;
1368
1369 cdev_del(tap_cdev);
1370 unregister_chrdev_region(major, TAP_NUM_DEVS);
1371 list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
1372 if (tap_major->major == MAJOR(major)) {
1373 idr_destroy(&tap_major->minor_idr);
1374 list_del_rcu(&tap_major->next);
1375 kfree_rcu(tap_major, rcu);
1376 }
1377 }
1378 }
1379 EXPORT_SYMBOL_GPL(tap_destroy_cdev);
1380
1381 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
1382 MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
1383 MODULE_LICENSE("GPL");