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