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