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