tuntap: RCUify dereferencing between tun_struct and tun_file
[deliverable/linux.git] / drivers / net / tun.c
1 /*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18 /*
19 * Changes:
20 *
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
24 * Mark Smith <markzzzsmith@yahoo.com.au>
25 * Use eth_random_addr() for tap MAC address.
26 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39 #define DRV_NAME "tun"
40 #define DRV_VERSION "1.6"
41 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
44 #include <linux/module.h>
45 #include <linux/errno.h>
46 #include <linux/kernel.h>
47 #include <linux/major.h>
48 #include <linux/slab.h>
49 #include <linux/poll.h>
50 #include <linux/fcntl.h>
51 #include <linux/init.h>
52 #include <linux/skbuff.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/miscdevice.h>
56 #include <linux/ethtool.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/compat.h>
59 #include <linux/if.h>
60 #include <linux/if_arp.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_tun.h>
63 #include <linux/crc32.h>
64 #include <linux/nsproxy.h>
65 #include <linux/virtio_net.h>
66 #include <linux/rcupdate.h>
67 #include <net/net_namespace.h>
68 #include <net/netns/generic.h>
69 #include <net/rtnetlink.h>
70 #include <net/sock.h>
71
72 #include <asm/uaccess.h>
73
74 /* Uncomment to enable debugging */
75 /* #define TUN_DEBUG 1 */
76
77 #ifdef TUN_DEBUG
78 static int debug;
79
80 #define tun_debug(level, tun, fmt, args...) \
81 do { \
82 if (tun->debug) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
84 } while (0)
85 #define DBG1(level, fmt, args...) \
86 do { \
87 if (debug == 2) \
88 printk(level fmt, ##args); \
89 } while (0)
90 #else
91 #define tun_debug(level, tun, fmt, args...) \
92 do { \
93 if (0) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
95 } while (0)
96 #define DBG1(level, fmt, args...) \
97 do { \
98 if (0) \
99 printk(level fmt, ##args); \
100 } while (0)
101 #endif
102
103 #define GOODCOPY_LEN 128
104
105 #define FLT_EXACT_COUNT 8
106 struct tap_filter {
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
110 };
111
112 /* A tun_file connects an open character device to a tuntap netdevice. It
113 * also contains all socket related strctures (except sock_fprog and tap_filter)
114 * to serve as one transmit queue for tuntap device. The sock_fprog and
115 * tap_filter were kept in tun_struct since they were used for filtering for the
116 * netdevice not for a specific queue (at least I didn't see the reqirement for
117 * this).
118 *
119 * RCU usage:
120 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
121 * other can only be read while rcu_read_lock or rtnl_lock is held.
122 */
123 struct tun_file {
124 struct sock sk;
125 struct socket socket;
126 struct socket_wq wq;
127 struct tun_struct __rcu *tun;
128 struct net *net;
129 struct fasync_struct *fasync;
130 /* only used for fasnyc */
131 unsigned int flags;
132 };
133
134 /* Since the socket were moved to tun_file, to preserve the behavior of persist
135 * device, socket fileter, sndbuf and vnet header size were restore when the
136 * file were attached to a persist device.
137 */
138 struct tun_struct {
139 struct tun_file __rcu *tfile;
140 unsigned int flags;
141 kuid_t owner;
142 kgid_t group;
143
144 struct net_device *dev;
145 netdev_features_t set_features;
146 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
147 NETIF_F_TSO6|NETIF_F_UFO)
148
149 int vnet_hdr_sz;
150 int sndbuf;
151 struct tap_filter txflt;
152 struct sock_fprog fprog;
153 /* protected by rtnl lock */
154 bool filter_attached;
155 #ifdef TUN_DEBUG
156 int debug;
157 #endif
158 };
159
160 static int tun_attach(struct tun_struct *tun, struct file *file)
161 {
162 struct tun_file *tfile = file->private_data;
163 int err;
164
165 ASSERT_RTNL();
166
167 netif_tx_lock_bh(tun->dev);
168
169 err = -EINVAL;
170 if (tfile->tun)
171 goto out;
172
173 err = -EBUSY;
174 if (tun->tfile)
175 goto out;
176
177 err = 0;
178
179 /* Re-attach filter when attaching to a persist device */
180 if (tun->filter_attached == true) {
181 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
182 if (!err)
183 goto out;
184 }
185 rcu_assign_pointer(tfile->tun, tun);
186 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
187 rcu_assign_pointer(tun->tfile, tfile);
188 netif_carrier_on(tun->dev);
189 sock_hold(&tfile->sk);
190
191 out:
192 netif_tx_unlock_bh(tun->dev);
193 return err;
194 }
195
196 static void __tun_detach(struct tun_struct *tun)
197 {
198 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
199 lockdep_rtnl_is_held());
200 /* Detach from net device */
201 netif_carrier_off(tun->dev);
202 rcu_assign_pointer(tun->tfile, NULL);
203 if (tfile) {
204 rcu_assign_pointer(tfile->tun, NULL);
205
206 synchronize_net();
207 /* Drop read queue */
208 skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
209 }
210 }
211
212 static struct tun_struct *__tun_get(struct tun_file *tfile)
213 {
214 struct tun_struct *tun;
215
216 rcu_read_lock();
217 tun = rcu_dereference(tfile->tun);
218 if (tun)
219 dev_hold(tun->dev);
220 rcu_read_unlock();
221
222 return tun;
223 }
224
225 static struct tun_struct *tun_get(struct file *file)
226 {
227 return __tun_get(file->private_data);
228 }
229
230 static void tun_put(struct tun_struct *tun)
231 {
232 dev_put(tun->dev);
233 }
234
235 /* TAP filtering */
236 static void addr_hash_set(u32 *mask, const u8 *addr)
237 {
238 int n = ether_crc(ETH_ALEN, addr) >> 26;
239 mask[n >> 5] |= (1 << (n & 31));
240 }
241
242 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
243 {
244 int n = ether_crc(ETH_ALEN, addr) >> 26;
245 return mask[n >> 5] & (1 << (n & 31));
246 }
247
248 static int update_filter(struct tap_filter *filter, void __user *arg)
249 {
250 struct { u8 u[ETH_ALEN]; } *addr;
251 struct tun_filter uf;
252 int err, alen, n, nexact;
253
254 if (copy_from_user(&uf, arg, sizeof(uf)))
255 return -EFAULT;
256
257 if (!uf.count) {
258 /* Disabled */
259 filter->count = 0;
260 return 0;
261 }
262
263 alen = ETH_ALEN * uf.count;
264 addr = kmalloc(alen, GFP_KERNEL);
265 if (!addr)
266 return -ENOMEM;
267
268 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
269 err = -EFAULT;
270 goto done;
271 }
272
273 /* The filter is updated without holding any locks. Which is
274 * perfectly safe. We disable it first and in the worst
275 * case we'll accept a few undesired packets. */
276 filter->count = 0;
277 wmb();
278
279 /* Use first set of addresses as an exact filter */
280 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
281 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
282
283 nexact = n;
284
285 /* Remaining multicast addresses are hashed,
286 * unicast will leave the filter disabled. */
287 memset(filter->mask, 0, sizeof(filter->mask));
288 for (; n < uf.count; n++) {
289 if (!is_multicast_ether_addr(addr[n].u)) {
290 err = 0; /* no filter */
291 goto done;
292 }
293 addr_hash_set(filter->mask, addr[n].u);
294 }
295
296 /* For ALLMULTI just set the mask to all ones.
297 * This overrides the mask populated above. */
298 if ((uf.flags & TUN_FLT_ALLMULTI))
299 memset(filter->mask, ~0, sizeof(filter->mask));
300
301 /* Now enable the filter */
302 wmb();
303 filter->count = nexact;
304
305 /* Return the number of exact filters */
306 err = nexact;
307
308 done:
309 kfree(addr);
310 return err;
311 }
312
313 /* Returns: 0 - drop, !=0 - accept */
314 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
315 {
316 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
317 * at this point. */
318 struct ethhdr *eh = (struct ethhdr *) skb->data;
319 int i;
320
321 /* Exact match */
322 for (i = 0; i < filter->count; i++)
323 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
324 return 1;
325
326 /* Inexact match (multicast only) */
327 if (is_multicast_ether_addr(eh->h_dest))
328 return addr_hash_test(filter->mask, eh->h_dest);
329
330 return 0;
331 }
332
333 /*
334 * Checks whether the packet is accepted or not.
335 * Returns: 0 - drop, !=0 - accept
336 */
337 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
338 {
339 if (!filter->count)
340 return 1;
341
342 return run_filter(filter, skb);
343 }
344
345 /* Network device part of the driver */
346
347 static const struct ethtool_ops tun_ethtool_ops;
348
349 /* Net device detach from fd. */
350 static void tun_net_uninit(struct net_device *dev)
351 {
352 struct tun_struct *tun = netdev_priv(dev);
353 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
354 lockdep_rtnl_is_held());
355
356 /* Inform the methods they need to stop using the dev.
357 */
358 if (tfile) {
359 wake_up_all(&tfile->wq.wait);
360 __tun_detach(tun);
361 synchronize_net();
362 }
363 }
364
365 /* Net device open. */
366 static int tun_net_open(struct net_device *dev)
367 {
368 netif_start_queue(dev);
369 return 0;
370 }
371
372 /* Net device close. */
373 static int tun_net_close(struct net_device *dev)
374 {
375 netif_stop_queue(dev);
376 return 0;
377 }
378
379 /* Net device start xmit */
380 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
381 {
382 struct tun_struct *tun = netdev_priv(dev);
383 struct tun_file *tfile;
384
385 rcu_read_lock();
386 tfile = rcu_dereference(tun->tfile);
387 /* Drop packet if interface is not attached */
388 if (!tfile)
389 goto drop;
390
391 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
392
393 /* Drop if the filter does not like it.
394 * This is a noop if the filter is disabled.
395 * Filter can be enabled only for the TAP devices. */
396 if (!check_filter(&tun->txflt, skb))
397 goto drop;
398
399 if (tfile->socket.sk->sk_filter &&
400 sk_filter(tfile->socket.sk, skb))
401 goto drop;
402
403 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
404 >= dev->tx_queue_len) {
405 if (!(tun->flags & TUN_ONE_QUEUE)) {
406 /* Normal queueing mode. */
407 /* Packet scheduler handles dropping of further packets. */
408 netif_stop_queue(dev);
409
410 /* We won't see all dropped packets individually, so overrun
411 * error is more appropriate. */
412 dev->stats.tx_fifo_errors++;
413 } else {
414 /* Single queue mode.
415 * Driver handles dropping of all packets itself. */
416 goto drop;
417 }
418 }
419
420 /* Orphan the skb - required as we might hang on to it
421 * for indefinite time. */
422 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
423 goto drop;
424 skb_orphan(skb);
425
426 /* Enqueue packet */
427 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
428
429 /* Notify and wake up reader process */
430 if (tfile->flags & TUN_FASYNC)
431 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
432 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
433 POLLRDNORM | POLLRDBAND);
434
435 rcu_read_unlock();
436 return NETDEV_TX_OK;
437
438 drop:
439 dev->stats.tx_dropped++;
440 kfree_skb(skb);
441 rcu_read_unlock();
442 return NETDEV_TX_OK;
443 }
444
445 static void tun_net_mclist(struct net_device *dev)
446 {
447 /*
448 * This callback is supposed to deal with mc filter in
449 * _rx_ path and has nothing to do with the _tx_ path.
450 * In rx path we always accept everything userspace gives us.
451 */
452 }
453
454 #define MIN_MTU 68
455 #define MAX_MTU 65535
456
457 static int
458 tun_net_change_mtu(struct net_device *dev, int new_mtu)
459 {
460 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
461 return -EINVAL;
462 dev->mtu = new_mtu;
463 return 0;
464 }
465
466 static netdev_features_t tun_net_fix_features(struct net_device *dev,
467 netdev_features_t features)
468 {
469 struct tun_struct *tun = netdev_priv(dev);
470
471 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
472 }
473 #ifdef CONFIG_NET_POLL_CONTROLLER
474 static void tun_poll_controller(struct net_device *dev)
475 {
476 /*
477 * Tun only receives frames when:
478 * 1) the char device endpoint gets data from user space
479 * 2) the tun socket gets a sendmsg call from user space
480 * Since both of those are syncronous operations, we are guaranteed
481 * never to have pending data when we poll for it
482 * so theres nothing to do here but return.
483 * We need this though so netpoll recognizes us as an interface that
484 * supports polling, which enables bridge devices in virt setups to
485 * still use netconsole
486 */
487 return;
488 }
489 #endif
490 static const struct net_device_ops tun_netdev_ops = {
491 .ndo_uninit = tun_net_uninit,
492 .ndo_open = tun_net_open,
493 .ndo_stop = tun_net_close,
494 .ndo_start_xmit = tun_net_xmit,
495 .ndo_change_mtu = tun_net_change_mtu,
496 .ndo_fix_features = tun_net_fix_features,
497 #ifdef CONFIG_NET_POLL_CONTROLLER
498 .ndo_poll_controller = tun_poll_controller,
499 #endif
500 };
501
502 static const struct net_device_ops tap_netdev_ops = {
503 .ndo_uninit = tun_net_uninit,
504 .ndo_open = tun_net_open,
505 .ndo_stop = tun_net_close,
506 .ndo_start_xmit = tun_net_xmit,
507 .ndo_change_mtu = tun_net_change_mtu,
508 .ndo_fix_features = tun_net_fix_features,
509 .ndo_set_rx_mode = tun_net_mclist,
510 .ndo_set_mac_address = eth_mac_addr,
511 .ndo_validate_addr = eth_validate_addr,
512 #ifdef CONFIG_NET_POLL_CONTROLLER
513 .ndo_poll_controller = tun_poll_controller,
514 #endif
515 };
516
517 /* Initialize net device. */
518 static void tun_net_init(struct net_device *dev)
519 {
520 struct tun_struct *tun = netdev_priv(dev);
521
522 switch (tun->flags & TUN_TYPE_MASK) {
523 case TUN_TUN_DEV:
524 dev->netdev_ops = &tun_netdev_ops;
525
526 /* Point-to-Point TUN Device */
527 dev->hard_header_len = 0;
528 dev->addr_len = 0;
529 dev->mtu = 1500;
530
531 /* Zero header length */
532 dev->type = ARPHRD_NONE;
533 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
534 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
535 break;
536
537 case TUN_TAP_DEV:
538 dev->netdev_ops = &tap_netdev_ops;
539 /* Ethernet TAP Device */
540 ether_setup(dev);
541 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
542
543 eth_hw_addr_random(dev);
544
545 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
546 break;
547 }
548 }
549
550 /* Character device part */
551
552 /* Poll */
553 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
554 {
555 struct tun_file *tfile = file->private_data;
556 struct tun_struct *tun = __tun_get(tfile);
557 struct sock *sk;
558 unsigned int mask = 0;
559
560 if (!tun)
561 return POLLERR;
562
563 sk = tfile->socket.sk;
564
565 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
566
567 poll_wait(file, &tfile->wq.wait, wait);
568
569 if (!skb_queue_empty(&sk->sk_receive_queue))
570 mask |= POLLIN | POLLRDNORM;
571
572 if (sock_writeable(sk) ||
573 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
574 sock_writeable(sk)))
575 mask |= POLLOUT | POLLWRNORM;
576
577 if (tun->dev->reg_state != NETREG_REGISTERED)
578 mask = POLLERR;
579
580 tun_put(tun);
581 return mask;
582 }
583
584 /* prepad is the amount to reserve at front. len is length after that.
585 * linear is a hint as to how much to copy (usually headers). */
586 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
587 size_t prepad, size_t len,
588 size_t linear, int noblock)
589 {
590 struct sock *sk = tfile->socket.sk;
591 struct sk_buff *skb;
592 int err;
593
594 /* Under a page? Don't bother with paged skb. */
595 if (prepad + len < PAGE_SIZE || !linear)
596 linear = len;
597
598 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
599 &err);
600 if (!skb)
601 return ERR_PTR(err);
602
603 skb_reserve(skb, prepad);
604 skb_put(skb, linear);
605 skb->data_len = len - linear;
606 skb->len += len - linear;
607
608 return skb;
609 }
610
611 /* set skb frags from iovec, this can move to core network code for reuse */
612 static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
613 int offset, size_t count)
614 {
615 int len = iov_length(from, count) - offset;
616 int copy = skb_headlen(skb);
617 int size, offset1 = 0;
618 int i = 0;
619
620 /* Skip over from offset */
621 while (count && (offset >= from->iov_len)) {
622 offset -= from->iov_len;
623 ++from;
624 --count;
625 }
626
627 /* copy up to skb headlen */
628 while (count && (copy > 0)) {
629 size = min_t(unsigned int, copy, from->iov_len - offset);
630 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
631 size))
632 return -EFAULT;
633 if (copy > size) {
634 ++from;
635 --count;
636 offset = 0;
637 } else
638 offset += size;
639 copy -= size;
640 offset1 += size;
641 }
642
643 if (len == offset1)
644 return 0;
645
646 while (count--) {
647 struct page *page[MAX_SKB_FRAGS];
648 int num_pages;
649 unsigned long base;
650 unsigned long truesize;
651
652 len = from->iov_len - offset;
653 if (!len) {
654 offset = 0;
655 ++from;
656 continue;
657 }
658 base = (unsigned long)from->iov_base + offset;
659 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
660 if (i + size > MAX_SKB_FRAGS)
661 return -EMSGSIZE;
662 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
663 if (num_pages != size) {
664 for (i = 0; i < num_pages; i++)
665 put_page(page[i]);
666 return -EFAULT;
667 }
668 truesize = size * PAGE_SIZE;
669 skb->data_len += len;
670 skb->len += len;
671 skb->truesize += truesize;
672 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
673 while (len) {
674 int off = base & ~PAGE_MASK;
675 int size = min_t(int, len, PAGE_SIZE - off);
676 __skb_fill_page_desc(skb, i, page[i], off, size);
677 skb_shinfo(skb)->nr_frags++;
678 /* increase sk_wmem_alloc */
679 base += size;
680 len -= size;
681 i++;
682 }
683 offset = 0;
684 ++from;
685 }
686 return 0;
687 }
688
689 /* Get packet from user space buffer */
690 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
691 void *msg_control, const struct iovec *iv,
692 size_t total_len, size_t count, int noblock)
693 {
694 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
695 struct sk_buff *skb;
696 size_t len = total_len, align = NET_SKB_PAD;
697 struct virtio_net_hdr gso = { 0 };
698 int offset = 0;
699 int copylen;
700 bool zerocopy = false;
701 int err;
702
703 if (!(tun->flags & TUN_NO_PI)) {
704 if ((len -= sizeof(pi)) > total_len)
705 return -EINVAL;
706
707 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
708 return -EFAULT;
709 offset += sizeof(pi);
710 }
711
712 if (tun->flags & TUN_VNET_HDR) {
713 if ((len -= tun->vnet_hdr_sz) > total_len)
714 return -EINVAL;
715
716 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
717 return -EFAULT;
718
719 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
720 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
721 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
722
723 if (gso.hdr_len > len)
724 return -EINVAL;
725 offset += tun->vnet_hdr_sz;
726 }
727
728 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
729 align += NET_IP_ALIGN;
730 if (unlikely(len < ETH_HLEN ||
731 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
732 return -EINVAL;
733 }
734
735 if (msg_control)
736 zerocopy = true;
737
738 if (zerocopy) {
739 /* Userspace may produce vectors with count greater than
740 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
741 * to let the rest of data to be fit in the frags.
742 */
743 if (count > MAX_SKB_FRAGS) {
744 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
745 if (copylen < offset)
746 copylen = 0;
747 else
748 copylen -= offset;
749 } else
750 copylen = 0;
751 /* There are 256 bytes to be copied in skb, so there is enough
752 * room for skb expand head in case it is used.
753 * The rest of the buffer is mapped from userspace.
754 */
755 if (copylen < gso.hdr_len)
756 copylen = gso.hdr_len;
757 if (!copylen)
758 copylen = GOODCOPY_LEN;
759 } else
760 copylen = len;
761
762 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
763 if (IS_ERR(skb)) {
764 if (PTR_ERR(skb) != -EAGAIN)
765 tun->dev->stats.rx_dropped++;
766 return PTR_ERR(skb);
767 }
768
769 if (zerocopy)
770 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
771 else
772 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
773
774 if (err) {
775 tun->dev->stats.rx_dropped++;
776 kfree_skb(skb);
777 return -EFAULT;
778 }
779
780 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
781 if (!skb_partial_csum_set(skb, gso.csum_start,
782 gso.csum_offset)) {
783 tun->dev->stats.rx_frame_errors++;
784 kfree_skb(skb);
785 return -EINVAL;
786 }
787 }
788
789 switch (tun->flags & TUN_TYPE_MASK) {
790 case TUN_TUN_DEV:
791 if (tun->flags & TUN_NO_PI) {
792 switch (skb->data[0] & 0xf0) {
793 case 0x40:
794 pi.proto = htons(ETH_P_IP);
795 break;
796 case 0x60:
797 pi.proto = htons(ETH_P_IPV6);
798 break;
799 default:
800 tun->dev->stats.rx_dropped++;
801 kfree_skb(skb);
802 return -EINVAL;
803 }
804 }
805
806 skb_reset_mac_header(skb);
807 skb->protocol = pi.proto;
808 skb->dev = tun->dev;
809 break;
810 case TUN_TAP_DEV:
811 skb->protocol = eth_type_trans(skb, tun->dev);
812 break;
813 }
814
815 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
816 pr_debug("GSO!\n");
817 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
818 case VIRTIO_NET_HDR_GSO_TCPV4:
819 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
820 break;
821 case VIRTIO_NET_HDR_GSO_TCPV6:
822 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
823 break;
824 case VIRTIO_NET_HDR_GSO_UDP:
825 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
826 break;
827 default:
828 tun->dev->stats.rx_frame_errors++;
829 kfree_skb(skb);
830 return -EINVAL;
831 }
832
833 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
834 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
835
836 skb_shinfo(skb)->gso_size = gso.gso_size;
837 if (skb_shinfo(skb)->gso_size == 0) {
838 tun->dev->stats.rx_frame_errors++;
839 kfree_skb(skb);
840 return -EINVAL;
841 }
842
843 /* Header must be checked, and gso_segs computed. */
844 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
845 skb_shinfo(skb)->gso_segs = 0;
846 }
847
848 /* copy skb_ubuf_info for callback when skb has no error */
849 if (zerocopy) {
850 skb_shinfo(skb)->destructor_arg = msg_control;
851 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
852 }
853
854 netif_rx_ni(skb);
855
856 tun->dev->stats.rx_packets++;
857 tun->dev->stats.rx_bytes += len;
858
859 return total_len;
860 }
861
862 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
863 unsigned long count, loff_t pos)
864 {
865 struct file *file = iocb->ki_filp;
866 struct tun_struct *tun = tun_get(file);
867 struct tun_file *tfile = file->private_data;
868 ssize_t result;
869
870 if (!tun)
871 return -EBADFD;
872
873 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
874
875 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
876 count, file->f_flags & O_NONBLOCK);
877
878 tun_put(tun);
879 return result;
880 }
881
882 /* Put packet to the user space buffer */
883 static ssize_t tun_put_user(struct tun_struct *tun,
884 struct tun_file *tfile,
885 struct sk_buff *skb,
886 const struct iovec *iv, int len)
887 {
888 struct tun_pi pi = { 0, skb->protocol };
889 ssize_t total = 0;
890
891 if (!(tun->flags & TUN_NO_PI)) {
892 if ((len -= sizeof(pi)) < 0)
893 return -EINVAL;
894
895 if (len < skb->len) {
896 /* Packet will be striped */
897 pi.flags |= TUN_PKT_STRIP;
898 }
899
900 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
901 return -EFAULT;
902 total += sizeof(pi);
903 }
904
905 if (tun->flags & TUN_VNET_HDR) {
906 struct virtio_net_hdr gso = { 0 }; /* no info leak */
907 if ((len -= tun->vnet_hdr_sz) < 0)
908 return -EINVAL;
909
910 if (skb_is_gso(skb)) {
911 struct skb_shared_info *sinfo = skb_shinfo(skb);
912
913 /* This is a hint as to how much should be linear. */
914 gso.hdr_len = skb_headlen(skb);
915 gso.gso_size = sinfo->gso_size;
916 if (sinfo->gso_type & SKB_GSO_TCPV4)
917 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
918 else if (sinfo->gso_type & SKB_GSO_TCPV6)
919 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
920 else if (sinfo->gso_type & SKB_GSO_UDP)
921 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
922 else {
923 pr_err("unexpected GSO type: "
924 "0x%x, gso_size %d, hdr_len %d\n",
925 sinfo->gso_type, gso.gso_size,
926 gso.hdr_len);
927 print_hex_dump(KERN_ERR, "tun: ",
928 DUMP_PREFIX_NONE,
929 16, 1, skb->head,
930 min((int)gso.hdr_len, 64), true);
931 WARN_ON_ONCE(1);
932 return -EINVAL;
933 }
934 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
935 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
936 } else
937 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
938
939 if (skb->ip_summed == CHECKSUM_PARTIAL) {
940 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
941 gso.csum_start = skb_checksum_start_offset(skb);
942 gso.csum_offset = skb->csum_offset;
943 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
944 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
945 } /* else everything is zero */
946
947 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
948 sizeof(gso))))
949 return -EFAULT;
950 total += tun->vnet_hdr_sz;
951 }
952
953 len = min_t(int, skb->len, len);
954
955 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
956 total += skb->len;
957
958 tun->dev->stats.tx_packets++;
959 tun->dev->stats.tx_bytes += len;
960
961 return total;
962 }
963
964 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
965 struct kiocb *iocb, const struct iovec *iv,
966 ssize_t len, int noblock)
967 {
968 DECLARE_WAITQUEUE(wait, current);
969 struct sk_buff *skb;
970 ssize_t ret = 0;
971
972 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
973
974 if (unlikely(!noblock))
975 add_wait_queue(&tfile->wq.wait, &wait);
976 while (len) {
977 current->state = TASK_INTERRUPTIBLE;
978
979 /* Read frames from the queue */
980 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
981 if (noblock) {
982 ret = -EAGAIN;
983 break;
984 }
985 if (signal_pending(current)) {
986 ret = -ERESTARTSYS;
987 break;
988 }
989 if (tun->dev->reg_state != NETREG_REGISTERED) {
990 ret = -EIO;
991 break;
992 }
993
994 /* Nothing to read, let's sleep */
995 schedule();
996 continue;
997 }
998 netif_wake_queue(tun->dev);
999
1000 ret = tun_put_user(tun, tfile, skb, iv, len);
1001 kfree_skb(skb);
1002 break;
1003 }
1004
1005 current->state = TASK_RUNNING;
1006 if (unlikely(!noblock))
1007 remove_wait_queue(&tfile->wq.wait, &wait);
1008
1009 return ret;
1010 }
1011
1012 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1013 unsigned long count, loff_t pos)
1014 {
1015 struct file *file = iocb->ki_filp;
1016 struct tun_file *tfile = file->private_data;
1017 struct tun_struct *tun = __tun_get(tfile);
1018 ssize_t len, ret;
1019
1020 if (!tun)
1021 return -EBADFD;
1022 len = iov_length(iv, count);
1023 if (len < 0) {
1024 ret = -EINVAL;
1025 goto out;
1026 }
1027
1028 ret = tun_do_read(tun, tfile, iocb, iv, len,
1029 file->f_flags & O_NONBLOCK);
1030 ret = min_t(ssize_t, ret, len);
1031 out:
1032 tun_put(tun);
1033 return ret;
1034 }
1035
1036 static void tun_setup(struct net_device *dev)
1037 {
1038 struct tun_struct *tun = netdev_priv(dev);
1039
1040 tun->owner = INVALID_UID;
1041 tun->group = INVALID_GID;
1042
1043 dev->ethtool_ops = &tun_ethtool_ops;
1044 dev->destructor = free_netdev;
1045 }
1046
1047 /* Trivial set of netlink ops to allow deleting tun or tap
1048 * device with netlink.
1049 */
1050 static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1051 {
1052 return -EINVAL;
1053 }
1054
1055 static struct rtnl_link_ops tun_link_ops __read_mostly = {
1056 .kind = DRV_NAME,
1057 .priv_size = sizeof(struct tun_struct),
1058 .setup = tun_setup,
1059 .validate = tun_validate,
1060 };
1061
1062 static void tun_sock_write_space(struct sock *sk)
1063 {
1064 struct tun_file *tfile;
1065 wait_queue_head_t *wqueue;
1066
1067 if (!sock_writeable(sk))
1068 return;
1069
1070 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1071 return;
1072
1073 wqueue = sk_sleep(sk);
1074 if (wqueue && waitqueue_active(wqueue))
1075 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
1076 POLLWRNORM | POLLWRBAND);
1077
1078 tfile = container_of(sk, struct tun_file, sk);
1079 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
1080 }
1081
1082 static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1083 struct msghdr *m, size_t total_len)
1084 {
1085 int ret;
1086 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1087 struct tun_struct *tun = __tun_get(tfile);
1088
1089 if (!tun)
1090 return -EBADFD;
1091 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1092 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1093 tun_put(tun);
1094 return ret;
1095 }
1096
1097
1098 static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1099 struct msghdr *m, size_t total_len,
1100 int flags)
1101 {
1102 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1103 struct tun_struct *tun = __tun_get(tfile);
1104 int ret;
1105
1106 if (!tun)
1107 return -EBADFD;
1108
1109 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1110 return -EINVAL;
1111 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
1112 flags & MSG_DONTWAIT);
1113 if (ret > total_len) {
1114 m->msg_flags |= MSG_TRUNC;
1115 ret = flags & MSG_TRUNC ? ret : total_len;
1116 }
1117 tun_put(tun);
1118 return ret;
1119 }
1120
1121 static int tun_release(struct socket *sock)
1122 {
1123 if (sock->sk)
1124 sock_put(sock->sk);
1125 return 0;
1126 }
1127
1128 /* Ops structure to mimic raw sockets with tun */
1129 static const struct proto_ops tun_socket_ops = {
1130 .sendmsg = tun_sendmsg,
1131 .recvmsg = tun_recvmsg,
1132 .release = tun_release,
1133 };
1134
1135 static struct proto tun_proto = {
1136 .name = "tun",
1137 .owner = THIS_MODULE,
1138 .obj_size = sizeof(struct tun_file),
1139 };
1140
1141 static int tun_flags(struct tun_struct *tun)
1142 {
1143 int flags = 0;
1144
1145 if (tun->flags & TUN_TUN_DEV)
1146 flags |= IFF_TUN;
1147 else
1148 flags |= IFF_TAP;
1149
1150 if (tun->flags & TUN_NO_PI)
1151 flags |= IFF_NO_PI;
1152
1153 if (tun->flags & TUN_ONE_QUEUE)
1154 flags |= IFF_ONE_QUEUE;
1155
1156 if (tun->flags & TUN_VNET_HDR)
1157 flags |= IFF_VNET_HDR;
1158
1159 return flags;
1160 }
1161
1162 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1163 char *buf)
1164 {
1165 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1166 return sprintf(buf, "0x%x\n", tun_flags(tun));
1167 }
1168
1169 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1170 char *buf)
1171 {
1172 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1173 return uid_valid(tun->owner)?
1174 sprintf(buf, "%u\n",
1175 from_kuid_munged(current_user_ns(), tun->owner)):
1176 sprintf(buf, "-1\n");
1177 }
1178
1179 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1180 char *buf)
1181 {
1182 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1183 return gid_valid(tun->group) ?
1184 sprintf(buf, "%u\n",
1185 from_kgid_munged(current_user_ns(), tun->group)):
1186 sprintf(buf, "-1\n");
1187 }
1188
1189 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1190 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1191 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1192
1193 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1194 {
1195 struct tun_struct *tun;
1196 struct tun_file *tfile = file->private_data;
1197 struct net_device *dev;
1198 int err;
1199
1200 dev = __dev_get_by_name(net, ifr->ifr_name);
1201 if (dev) {
1202 const struct cred *cred = current_cred();
1203
1204 if (ifr->ifr_flags & IFF_TUN_EXCL)
1205 return -EBUSY;
1206 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1207 tun = netdev_priv(dev);
1208 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1209 tun = netdev_priv(dev);
1210 else
1211 return -EINVAL;
1212
1213 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1214 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
1215 !capable(CAP_NET_ADMIN))
1216 return -EPERM;
1217 err = security_tun_dev_attach(tfile->socket.sk);
1218 if (err < 0)
1219 return err;
1220
1221 err = tun_attach(tun, file);
1222 if (err < 0)
1223 return err;
1224 }
1225 else {
1226 char *name;
1227 unsigned long flags = 0;
1228
1229 if (!capable(CAP_NET_ADMIN))
1230 return -EPERM;
1231 err = security_tun_dev_create();
1232 if (err < 0)
1233 return err;
1234
1235 /* Set dev type */
1236 if (ifr->ifr_flags & IFF_TUN) {
1237 /* TUN device */
1238 flags |= TUN_TUN_DEV;
1239 name = "tun%d";
1240 } else if (ifr->ifr_flags & IFF_TAP) {
1241 /* TAP device */
1242 flags |= TUN_TAP_DEV;
1243 name = "tap%d";
1244 } else
1245 return -EINVAL;
1246
1247 if (*ifr->ifr_name)
1248 name = ifr->ifr_name;
1249
1250 dev = alloc_netdev(sizeof(struct tun_struct), name,
1251 tun_setup);
1252 if (!dev)
1253 return -ENOMEM;
1254
1255 dev_net_set(dev, net);
1256 dev->rtnl_link_ops = &tun_link_ops;
1257
1258 tun = netdev_priv(dev);
1259 tun->dev = dev;
1260 tun->flags = flags;
1261 tun->txflt.count = 0;
1262 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1263
1264 tun->filter_attached = false;
1265 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
1266
1267 security_tun_dev_post_create(&tfile->sk);
1268
1269 tun_net_init(dev);
1270
1271 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1272 TUN_USER_FEATURES;
1273 dev->features = dev->hw_features;
1274
1275 err = register_netdevice(tun->dev);
1276 if (err < 0)
1277 goto err_free_dev;
1278
1279 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1280 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1281 device_create_file(&tun->dev->dev, &dev_attr_group))
1282 pr_err("Failed to create tun sysfs files\n");
1283
1284 err = tun_attach(tun, file);
1285 if (err < 0)
1286 goto failed;
1287 }
1288
1289 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1290
1291 if (ifr->ifr_flags & IFF_NO_PI)
1292 tun->flags |= TUN_NO_PI;
1293 else
1294 tun->flags &= ~TUN_NO_PI;
1295
1296 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1297 tun->flags |= TUN_ONE_QUEUE;
1298 else
1299 tun->flags &= ~TUN_ONE_QUEUE;
1300
1301 if (ifr->ifr_flags & IFF_VNET_HDR)
1302 tun->flags |= TUN_VNET_HDR;
1303 else
1304 tun->flags &= ~TUN_VNET_HDR;
1305
1306 /* Make sure persistent devices do not get stuck in
1307 * xoff state.
1308 */
1309 if (netif_running(tun->dev))
1310 netif_wake_queue(tun->dev);
1311
1312 strcpy(ifr->ifr_name, tun->dev->name);
1313 return 0;
1314
1315 err_free_dev:
1316 free_netdev(dev);
1317 failed:
1318 return err;
1319 }
1320
1321 static int tun_get_iff(struct net *net, struct tun_struct *tun,
1322 struct ifreq *ifr)
1323 {
1324 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1325
1326 strcpy(ifr->ifr_name, tun->dev->name);
1327
1328 ifr->ifr_flags = tun_flags(tun);
1329
1330 return 0;
1331 }
1332
1333 /* This is like a cut-down ethtool ops, except done via tun fd so no
1334 * privs required. */
1335 static int set_offload(struct tun_struct *tun, unsigned long arg)
1336 {
1337 netdev_features_t features = 0;
1338
1339 if (arg & TUN_F_CSUM) {
1340 features |= NETIF_F_HW_CSUM;
1341 arg &= ~TUN_F_CSUM;
1342
1343 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1344 if (arg & TUN_F_TSO_ECN) {
1345 features |= NETIF_F_TSO_ECN;
1346 arg &= ~TUN_F_TSO_ECN;
1347 }
1348 if (arg & TUN_F_TSO4)
1349 features |= NETIF_F_TSO;
1350 if (arg & TUN_F_TSO6)
1351 features |= NETIF_F_TSO6;
1352 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1353 }
1354
1355 if (arg & TUN_F_UFO) {
1356 features |= NETIF_F_UFO;
1357 arg &= ~TUN_F_UFO;
1358 }
1359 }
1360
1361 /* This gives the user a way to test for new features in future by
1362 * trying to set them. */
1363 if (arg)
1364 return -EINVAL;
1365
1366 tun->set_features = features;
1367 netdev_update_features(tun->dev);
1368
1369 return 0;
1370 }
1371
1372 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1373 unsigned long arg, int ifreq_len)
1374 {
1375 struct tun_file *tfile = file->private_data;
1376 struct tun_struct *tun;
1377 void __user* argp = (void __user*)arg;
1378 struct ifreq ifr;
1379 kuid_t owner;
1380 kgid_t group;
1381 int sndbuf;
1382 int vnet_hdr_sz;
1383 int ret;
1384
1385 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
1386 if (copy_from_user(&ifr, argp, ifreq_len))
1387 return -EFAULT;
1388 } else {
1389 memset(&ifr, 0, sizeof(ifr));
1390 }
1391 if (cmd == TUNGETFEATURES) {
1392 /* Currently this just means: "what IFF flags are valid?".
1393 * This is needed because we never checked for invalid flags on
1394 * TUNSETIFF. */
1395 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1396 IFF_VNET_HDR,
1397 (unsigned int __user*)argp);
1398 }
1399
1400 rtnl_lock();
1401
1402 tun = __tun_get(tfile);
1403 if (cmd == TUNSETIFF && !tun) {
1404 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1405
1406 ret = tun_set_iff(tfile->net, file, &ifr);
1407
1408 if (ret)
1409 goto unlock;
1410
1411 if (copy_to_user(argp, &ifr, ifreq_len))
1412 ret = -EFAULT;
1413 goto unlock;
1414 }
1415
1416 ret = -EBADFD;
1417 if (!tun)
1418 goto unlock;
1419
1420 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1421
1422 ret = 0;
1423 switch (cmd) {
1424 case TUNGETIFF:
1425 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1426 if (ret)
1427 break;
1428
1429 if (copy_to_user(argp, &ifr, ifreq_len))
1430 ret = -EFAULT;
1431 break;
1432
1433 case TUNSETNOCSUM:
1434 /* Disable/Enable checksum */
1435
1436 /* [unimplemented] */
1437 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
1438 arg ? "disabled" : "enabled");
1439 break;
1440
1441 case TUNSETPERSIST:
1442 /* Disable/Enable persist mode. Keep an extra reference to the
1443 * module to prevent the module being unprobed.
1444 */
1445 if (arg) {
1446 tun->flags |= TUN_PERSIST;
1447 __module_get(THIS_MODULE);
1448 } else {
1449 tun->flags &= ~TUN_PERSIST;
1450 module_put(THIS_MODULE);
1451 }
1452
1453 tun_debug(KERN_INFO, tun, "persist %s\n",
1454 arg ? "enabled" : "disabled");
1455 break;
1456
1457 case TUNSETOWNER:
1458 /* Set owner of the device */
1459 owner = make_kuid(current_user_ns(), arg);
1460 if (!uid_valid(owner)) {
1461 ret = -EINVAL;
1462 break;
1463 }
1464 tun->owner = owner;
1465 tun_debug(KERN_INFO, tun, "owner set to %u\n",
1466 from_kuid(&init_user_ns, tun->owner));
1467 break;
1468
1469 case TUNSETGROUP:
1470 /* Set group of the device */
1471 group = make_kgid(current_user_ns(), arg);
1472 if (!gid_valid(group)) {
1473 ret = -EINVAL;
1474 break;
1475 }
1476 tun->group = group;
1477 tun_debug(KERN_INFO, tun, "group set to %u\n",
1478 from_kgid(&init_user_ns, tun->group));
1479 break;
1480
1481 case TUNSETLINK:
1482 /* Only allow setting the type when the interface is down */
1483 if (tun->dev->flags & IFF_UP) {
1484 tun_debug(KERN_INFO, tun,
1485 "Linktype set failed because interface is up\n");
1486 ret = -EBUSY;
1487 } else {
1488 tun->dev->type = (int) arg;
1489 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1490 tun->dev->type);
1491 ret = 0;
1492 }
1493 break;
1494
1495 #ifdef TUN_DEBUG
1496 case TUNSETDEBUG:
1497 tun->debug = arg;
1498 break;
1499 #endif
1500 case TUNSETOFFLOAD:
1501 ret = set_offload(tun, arg);
1502 break;
1503
1504 case TUNSETTXFILTER:
1505 /* Can be set only for TAPs */
1506 ret = -EINVAL;
1507 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1508 break;
1509 ret = update_filter(&tun->txflt, (void __user *)arg);
1510 break;
1511
1512 case SIOCGIFHWADDR:
1513 /* Get hw address */
1514 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1515 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1516 if (copy_to_user(argp, &ifr, ifreq_len))
1517 ret = -EFAULT;
1518 break;
1519
1520 case SIOCSIFHWADDR:
1521 /* Set hw address */
1522 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1523 ifr.ifr_hwaddr.sa_data);
1524
1525 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1526 break;
1527
1528 case TUNGETSNDBUF:
1529 sndbuf = tfile->socket.sk->sk_sndbuf;
1530 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1531 ret = -EFAULT;
1532 break;
1533
1534 case TUNSETSNDBUF:
1535 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1536 ret = -EFAULT;
1537 break;
1538 }
1539
1540 tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
1541 break;
1542
1543 case TUNGETVNETHDRSZ:
1544 vnet_hdr_sz = tun->vnet_hdr_sz;
1545 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1546 ret = -EFAULT;
1547 break;
1548
1549 case TUNSETVNETHDRSZ:
1550 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1551 ret = -EFAULT;
1552 break;
1553 }
1554 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1555 ret = -EINVAL;
1556 break;
1557 }
1558
1559 tun->vnet_hdr_sz = vnet_hdr_sz;
1560 break;
1561
1562 case TUNATTACHFILTER:
1563 /* Can be set only for TAPs */
1564 ret = -EINVAL;
1565 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1566 break;
1567 ret = -EFAULT;
1568 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
1569 break;
1570
1571 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1572 if (!ret)
1573 tun->filter_attached = true;
1574 break;
1575
1576 case TUNDETACHFILTER:
1577 /* Can be set only for TAPs */
1578 ret = -EINVAL;
1579 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1580 break;
1581 ret = sk_detach_filter(tfile->socket.sk);
1582 if (!ret)
1583 tun->filter_attached = false;
1584 break;
1585
1586 default:
1587 ret = -EINVAL;
1588 break;
1589 }
1590
1591 unlock:
1592 rtnl_unlock();
1593 if (tun)
1594 tun_put(tun);
1595 return ret;
1596 }
1597
1598 static long tun_chr_ioctl(struct file *file,
1599 unsigned int cmd, unsigned long arg)
1600 {
1601 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1602 }
1603
1604 #ifdef CONFIG_COMPAT
1605 static long tun_chr_compat_ioctl(struct file *file,
1606 unsigned int cmd, unsigned long arg)
1607 {
1608 switch (cmd) {
1609 case TUNSETIFF:
1610 case TUNGETIFF:
1611 case TUNSETTXFILTER:
1612 case TUNGETSNDBUF:
1613 case TUNSETSNDBUF:
1614 case SIOCGIFHWADDR:
1615 case SIOCSIFHWADDR:
1616 arg = (unsigned long)compat_ptr(arg);
1617 break;
1618 default:
1619 arg = (compat_ulong_t)arg;
1620 break;
1621 }
1622
1623 /*
1624 * compat_ifreq is shorter than ifreq, so we must not access beyond
1625 * the end of that structure. All fields that are used in this
1626 * driver are compatible though, we don't need to convert the
1627 * contents.
1628 */
1629 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1630 }
1631 #endif /* CONFIG_COMPAT */
1632
1633 static int tun_chr_fasync(int fd, struct file *file, int on)
1634 {
1635 struct tun_file *tfile = file->private_data;
1636 int ret;
1637
1638 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
1639 goto out;
1640
1641 if (on) {
1642 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1643 if (ret)
1644 goto out;
1645 tfile->flags |= TUN_FASYNC;
1646 } else
1647 tfile->flags &= ~TUN_FASYNC;
1648 ret = 0;
1649 out:
1650 return ret;
1651 }
1652
1653 static int tun_chr_open(struct inode *inode, struct file * file)
1654 {
1655 struct tun_file *tfile;
1656
1657 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1658
1659 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1660 &tun_proto);
1661 if (!tfile)
1662 return -ENOMEM;
1663 rcu_assign_pointer(tfile->tun, NULL);
1664 tfile->net = get_net(current->nsproxy->net_ns);
1665 tfile->flags = 0;
1666
1667 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1668 init_waitqueue_head(&tfile->wq.wait);
1669
1670 tfile->socket.file = file;
1671 tfile->socket.ops = &tun_socket_ops;
1672
1673 sock_init_data(&tfile->socket, &tfile->sk);
1674 sk_change_net(&tfile->sk, tfile->net);
1675
1676 tfile->sk.sk_write_space = tun_sock_write_space;
1677 tfile->sk.sk_sndbuf = INT_MAX;
1678
1679 file->private_data = tfile;
1680 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1681
1682 return 0;
1683 }
1684
1685 static int tun_chr_close(struct inode *inode, struct file *file)
1686 {
1687 struct tun_file *tfile = file->private_data;
1688 struct tun_struct *tun;
1689 struct net *net = tfile->net;
1690
1691 rtnl_lock();
1692
1693 tun = rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held());
1694 if (tun) {
1695 struct net_device *dev = tun->dev;
1696
1697 tun_debug(KERN_INFO, tun, "tun_chr_close\n");
1698
1699 __tun_detach(tun);
1700
1701 synchronize_net();
1702
1703 /* If desirable, unregister the netdevice. */
1704 if (!(tun->flags & TUN_PERSIST)) {
1705 if (dev->reg_state == NETREG_REGISTERED)
1706 unregister_netdevice(dev);
1707 }
1708
1709 /* drop the reference that netdevice holds */
1710 sock_put(&tfile->sk);
1711 }
1712
1713 rtnl_unlock();
1714
1715 /* drop the reference that file holds */
1716 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
1717 &tfile->socket.flags));
1718 sk_release_kernel(&tfile->sk);
1719 put_net(net);
1720
1721 return 0;
1722 }
1723
1724 static const struct file_operations tun_fops = {
1725 .owner = THIS_MODULE,
1726 .llseek = no_llseek,
1727 .read = do_sync_read,
1728 .aio_read = tun_chr_aio_read,
1729 .write = do_sync_write,
1730 .aio_write = tun_chr_aio_write,
1731 .poll = tun_chr_poll,
1732 .unlocked_ioctl = tun_chr_ioctl,
1733 #ifdef CONFIG_COMPAT
1734 .compat_ioctl = tun_chr_compat_ioctl,
1735 #endif
1736 .open = tun_chr_open,
1737 .release = tun_chr_close,
1738 .fasync = tun_chr_fasync
1739 };
1740
1741 static struct miscdevice tun_miscdev = {
1742 .minor = TUN_MINOR,
1743 .name = "tun",
1744 .nodename = "net/tun",
1745 .fops = &tun_fops,
1746 };
1747
1748 /* ethtool interface */
1749
1750 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1751 {
1752 cmd->supported = 0;
1753 cmd->advertising = 0;
1754 ethtool_cmd_speed_set(cmd, SPEED_10);
1755 cmd->duplex = DUPLEX_FULL;
1756 cmd->port = PORT_TP;
1757 cmd->phy_address = 0;
1758 cmd->transceiver = XCVR_INTERNAL;
1759 cmd->autoneg = AUTONEG_DISABLE;
1760 cmd->maxtxpkt = 0;
1761 cmd->maxrxpkt = 0;
1762 return 0;
1763 }
1764
1765 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1766 {
1767 struct tun_struct *tun = netdev_priv(dev);
1768
1769 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1770 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1771
1772 switch (tun->flags & TUN_TYPE_MASK) {
1773 case TUN_TUN_DEV:
1774 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
1775 break;
1776 case TUN_TAP_DEV:
1777 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
1778 break;
1779 }
1780 }
1781
1782 static u32 tun_get_msglevel(struct net_device *dev)
1783 {
1784 #ifdef TUN_DEBUG
1785 struct tun_struct *tun = netdev_priv(dev);
1786 return tun->debug;
1787 #else
1788 return -EOPNOTSUPP;
1789 #endif
1790 }
1791
1792 static void tun_set_msglevel(struct net_device *dev, u32 value)
1793 {
1794 #ifdef TUN_DEBUG
1795 struct tun_struct *tun = netdev_priv(dev);
1796 tun->debug = value;
1797 #endif
1798 }
1799
1800 static const struct ethtool_ops tun_ethtool_ops = {
1801 .get_settings = tun_get_settings,
1802 .get_drvinfo = tun_get_drvinfo,
1803 .get_msglevel = tun_get_msglevel,
1804 .set_msglevel = tun_set_msglevel,
1805 .get_link = ethtool_op_get_link,
1806 };
1807
1808
1809 static int __init tun_init(void)
1810 {
1811 int ret = 0;
1812
1813 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1814 pr_info("%s\n", DRV_COPYRIGHT);
1815
1816 ret = rtnl_link_register(&tun_link_ops);
1817 if (ret) {
1818 pr_err("Can't register link_ops\n");
1819 goto err_linkops;
1820 }
1821
1822 ret = misc_register(&tun_miscdev);
1823 if (ret) {
1824 pr_err("Can't register misc device %d\n", TUN_MINOR);
1825 goto err_misc;
1826 }
1827 return 0;
1828 err_misc:
1829 rtnl_link_unregister(&tun_link_ops);
1830 err_linkops:
1831 return ret;
1832 }
1833
1834 static void tun_cleanup(void)
1835 {
1836 misc_deregister(&tun_miscdev);
1837 rtnl_link_unregister(&tun_link_ops);
1838 }
1839
1840 /* Get an underlying socket object from tun file. Returns error unless file is
1841 * attached to a device. The returned object works like a packet socket, it
1842 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1843 * holding a reference to the file for as long as the socket is in use. */
1844 struct socket *tun_get_socket(struct file *file)
1845 {
1846 struct tun_file *tfile;
1847 if (file->f_op != &tun_fops)
1848 return ERR_PTR(-EINVAL);
1849 tfile = file->private_data;
1850 if (!tfile)
1851 return ERR_PTR(-EBADFD);
1852 return &tfile->socket;
1853 }
1854 EXPORT_SYMBOL_GPL(tun_get_socket);
1855
1856 module_init(tun_init);
1857 module_exit(tun_cleanup);
1858 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1859 MODULE_AUTHOR(DRV_COPYRIGHT);
1860 MODULE_LICENSE("GPL");
1861 MODULE_ALIAS_MISCDEV(TUN_MINOR);
1862 MODULE_ALIAS("devname:net/tun");
This page took 0.085493 seconds and 6 git commands to generate.