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