37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #define DRV_NAME "tun"
40 #define DRV_VERSION "1.6"
41 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
44 #include <linux/module.h>
45 #include <linux/errno.h>
46 #include <linux/kernel.h>
48 #include <linux/slab.h>
49 #include <linux/poll.h>
50 #include <linux/fcntl.h>
53 #include <linux/netdevice.h>
56 #include <linux/ethtool.h>
57 #include <linux/rtnetlink.h>
60 #include <linux/if_arp.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_tun.h>
73 #include <asm/uaccess.h>
81 #define tun_debug(level, tun, fmt, args...) \
84 netdev_printk(level, tun->dev, fmt, ##args); \
86 #define DBG1(level, fmt, args...) \
89 printk(level fmt, ##args); \
92 #define tun_debug(level, tun, fmt, args...) \
95 netdev_printk(level, tun->dev, fmt, ##args); \
97 #define DBG1(level, fmt, args...) \
100 printk(level fmt, ##args); \
104 #define GOODCOPY_LEN 128
106 #define FLT_EXACT_COUNT 8
129 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
130 NETIF_F_TSO6|NETIF_F_UFO)
161 netif_tx_lock_bh(tun->
dev);
177 sock_hold(tun->
socket.sk);
181 netif_tx_unlock_bh(tun->
dev);
185 static void __tun_detach(
struct tun_struct *tun)
188 netif_tx_lock_bh(tun->
dev);
191 netif_tx_unlock_bh(tun->
dev);
200 static void tun_detach(
struct tun_struct *tun)
217 static struct tun_struct *tun_get(
struct file *file)
227 tun_detach(tfile->
tun);
234 mask[n >> 5] |= (1 << (n & 31));
237 static unsigned int addr_hash_test(
const u32 *
mask,
const u8 *
addr)
240 return mask[n >> 5] & (1 << (n & 31));
247 int err, alen,
n, nexact;
283 for (; n < uf.count; n++) {
284 if (!is_multicast_ether_addr(
addr[n].
u)) {
288 addr_hash_set(filter->
mask,
addr[n].u);
298 filter->
count = nexact;
322 if (is_multicast_ether_addr(eh->
h_dest))
323 return addr_hash_test(filter->
mask, eh->
h_dest);
337 return run_filter(filter, skb);
359 static void tun_free_netdev(
struct net_device *dev)
369 static int tun_net_open(
struct net_device *dev)
371 netif_start_queue(dev);
376 static int tun_net_close(
struct net_device *dev)
378 netif_stop_queue(dev);
396 if (!check_filter(&tun->
txflt, skb))
399 if (tun->
socket.sk->sk_filter &&
407 netif_stop_queue(dev);
411 dev->
stats.tx_fifo_errors++;
436 dev->
stats.tx_dropped++;
441 static void tun_net_mclist(
struct net_device *dev)
451 #define MAX_MTU 65535
454 tun_net_change_mtu(
struct net_device *dev,
int new_mtu)
456 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len >
MAX_MTU)
469 #ifdef CONFIG_NET_POLL_CONTROLLER
470 static void tun_poll_controller(
struct net_device *dev)
487 .ndo_uninit = tun_net_uninit,
488 .ndo_open = tun_net_open,
489 .ndo_stop = tun_net_close,
490 .ndo_start_xmit = tun_net_xmit,
491 .ndo_change_mtu = tun_net_change_mtu,
492 .ndo_fix_features = tun_net_fix_features,
493 #ifdef CONFIG_NET_POLL_CONTROLLER
494 .ndo_poll_controller = tun_poll_controller,
499 .ndo_uninit = tun_net_uninit,
500 .ndo_open = tun_net_open,
501 .ndo_stop = tun_net_close,
502 .ndo_start_xmit = tun_net_xmit,
503 .ndo_change_mtu = tun_net_change_mtu,
504 .ndo_fix_features = tun_net_fix_features,
505 .ndo_set_rx_mode = tun_net_mclist,
508 #ifdef CONFIG_NET_POLL_CONTROLLER
509 .ndo_poll_controller = tun_poll_controller,
514 static void tun_net_init(
struct net_device *dev)
539 eth_hw_addr_random(dev);
549 static unsigned int tun_chr_poll(
struct file *file,
poll_table *
wait)
554 unsigned int mask = 0;
563 poll_wait(file, &tun->
wq.wait, wait);
568 if (sock_writeable(sk) ||
573 if (tun->
dev->reg_state != NETREG_REGISTERED)
583 size_t prepad,
size_t len,
584 size_t linear,
int noblock)
590 sock_update_classid(sk);
601 skb_reserve(skb, prepad);
610 static int zerocopy_sg_from_iovec(
struct sk_buff *skb,
const struct iovec *
from,
613 int len = iov_length(from, count) -
offset;
614 int copy = skb_headlen(skb);
619 while (count && (offset >= from->
iov_len)) {
626 while (count && (copy > 0)) {
627 size =
min_t(
unsigned int, copy, from->
iov_len - offset);
648 unsigned long truesize;
661 if (num_pages != size) {
673 int size =
min_t(
int, len, PAGE_SIZE - off);
674 __skb_fill_page_desc(skb, i, page[i], off, size);
675 skb_shinfo(skb)->nr_frags++;
690 size_t count,
int noblock)
698 bool zerocopy =
false;
702 if ((len -=
sizeof(pi)) > total_len)
707 offset +=
sizeof(pi);
743 if (copylen < offset)
760 skb = tun_alloc_skb(tun, align, copylen, gso.
hdr_len, noblock);
762 if (PTR_ERR(skb) != -
EAGAIN)
763 tun->
dev->stats.rx_dropped++;
768 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
773 tun->
dev->stats.rx_dropped++;
781 tun->
dev->stats.rx_frame_errors++;
790 switch (skb->
data[0] & 0xf0) {
798 tun->
dev->stats.rx_dropped++;
804 skb_reset_mac_header(skb);
826 tun->
dev->stats.rx_frame_errors++;
834 skb_shinfo(skb)->gso_size = gso.
gso_size;
835 if (skb_shinfo(skb)->
gso_size == 0) {
836 tun->
dev->stats.rx_frame_errors++;
843 skb_shinfo(skb)->gso_segs = 0;
848 skb_shinfo(skb)->destructor_arg = msg_control;
854 tun->
dev->stats.rx_packets++;
855 tun->
dev->stats.rx_bytes += len;
861 unsigned long count, loff_t
pos)
863 struct file *file = iocb->
ki_filp;
872 result = tun_get_user(tun,
NULL, iv, iov_length(iv, count), count,
882 const struct iovec *iv,
int len)
888 if ((len -=
sizeof(pi)) < 0)
891 if (len < skb->len) {
906 if (skb_is_gso(skb)) {
910 gso.
hdr_len = skb_headlen(skb);
919 pr_err(
"unexpected GSO type: "
920 "0x%x, gso_size %d, hdr_len %d\n",
937 gso.
csum_start = skb_checksum_start_offset(skb);
954 tun->
dev->stats.tx_packets++;
955 tun->
dev->stats.tx_bytes += len;
985 if (tun->
dev->reg_state != NETREG_REGISTERED) {
994 netif_wake_queue(tun->
dev);
996 ret = tun_put_user(tun, skb, iv, len);
1009 unsigned long count, loff_t pos)
1011 struct file *file = iocb->
ki_filp;
1018 len = iov_length(iv, count);
1031 static void tun_setup(
struct net_device *dev)
1054 .validate = tun_validate,
1057 static void tun_sock_write_space(
struct sock *sk)
1062 if (!sock_writeable(sk))
1068 wqueue = sk_sleep(sk);
1069 if (wqueue && waitqueue_active(wqueue))
1073 tun = tun_sk(sk)->tun;
1077 static void tun_sock_destruct(
struct sock *sk)
1083 struct msghdr *
m,
size_t total_len)
1091 struct msghdr *
m,
size_t total_len,
1098 ret = tun_do_read(tun, iocb, m->
msg_iov, total_len,
1100 if (ret > total_len) {
1107 static int tun_release(
struct socket *sock)
1115 static const struct proto_ops tun_socket_ops = {
1116 .sendmsg = tun_sendmsg,
1117 .recvmsg = tun_recvmsg,
1118 .release = tun_release,
1121 static struct proto tun_proto = {
1124 .obj_size =
sizeof(
struct tun_sock),
1152 return sprintf(buf,
"0x%x\n", tun_flags(tun));
1159 return uid_valid(tun->
owner)?
1169 return gid_valid(tun->
group) ?
1179 static int tun_set_iff(
struct net *
net,
struct file *file,
struct ifreq *ifr)
1193 tun = netdev_priv(dev);
1195 tun = netdev_priv(dev);
1199 if (((uid_valid(tun->
owner) && !uid_eq(cred->
euid, tun->
owner)) ||
1203 err = security_tun_dev_attach(tun->
socket.sk);
1207 err = tun_attach(tun, file);
1213 unsigned long flags = 0;
1217 err = security_tun_dev_create();
1222 if (ifr->ifr_flags &
IFF_TUN) {
1226 }
else if (ifr->ifr_flags &
IFF_TAP) {
1234 name = ifr->ifr_name;
1241 dev_net_set(dev, net);
1244 tun = netdev_priv(dev);
1247 tun->
txflt.count = 0;
1256 sk_change_net(sk, net);
1259 tun->
socket.ops = &tun_socket_ops;
1265 tun_sk(sk)->tun = tun;
1267 security_tun_dev_post_create(sk);
1282 pr_err(
"Failed to create tun sysfs files\n");
1286 err = tun_attach(tun, file);
1311 if (netif_running(tun->
dev))
1312 netif_wake_queue(tun->
dev);
1318 tun_free_netdev(dev);
1325 static int tun_get_iff(
struct net *net,
struct tun_struct *tun,
1332 ifr->ifr_flags = tun_flags(tun);
1339 static int set_offload(
struct tun_struct *tun,
unsigned long arg)
1350 arg &= ~TUN_F_TSO_ECN;
1376 static long __tun_chr_ioctl(
struct file *file,
unsigned int cmd,
1377 unsigned long arg,
int ifreq_len)
1394 memset(&ifr, 0,
sizeof(ifr));
1402 (
unsigned int __user*)argp);
1407 tun = __tun_get(tfile);
1411 ret = tun_set_iff(tfile->
net, file, &ifr);
1430 ret = tun_get_iff(
current->nsproxy->net_ns, tun, &ifr);
1443 arg ?
"disabled" :
"enabled");
1454 arg ?
"enabled" :
"disabled");
1460 if (!uid_valid(owner)) {
1472 if (!gid_valid(group)) {
1485 "Linktype set failed because interface is up\n");
1488 tun->
dev->type = (
int) arg;
1501 ret = set_offload(tun, arg);
1509 ret = update_filter(&tun->
txflt, (
void __user *)arg);
1515 ifr.ifr_hwaddr.sa_family = tun->
dev->type;
1523 ifr.ifr_hwaddr.sa_data);
1529 sndbuf = tun->
socket.sk->sk_sndbuf;
1545 if (
copy_to_user(argp, &vnet_hdr_sz,
sizeof(vnet_hdr_sz)))
1594 static long tun_chr_ioctl(
struct file *file,
1595 unsigned int cmd,
unsigned long arg)
1597 return __tun_chr_ioctl(file, cmd, arg,
sizeof (
struct ifreq));
1600 #ifdef CONFIG_COMPAT
1601 static long tun_chr_compat_ioctl(
struct file *file,
1602 unsigned int cmd,
unsigned long arg)
1612 arg = (
unsigned long)compat_ptr(arg);
1625 return __tun_chr_ioctl(file, cmd, arg,
sizeof(
struct compat_ifreq));
1629 static int tun_chr_fasync(
int fd,
struct file *file,
int on)
1655 static int tun_chr_open(
struct inode *
inode,
struct file * file)
1666 tfile->
net = get_net(
current->nsproxy->net_ns);
1671 static int tun_chr_close(
struct inode *inode,
struct file *file)
1676 tun = __tun_get(tfile);
1688 unregister_netdevice(dev);
1695 sock_put(tun->
socket.sk);
1697 put_net(tfile->
net);
1707 .aio_read = tun_chr_aio_read,
1709 .aio_write = tun_chr_aio_write,
1710 .poll = tun_chr_poll,
1711 .unlocked_ioctl = tun_chr_ioctl,
1712 #ifdef CONFIG_COMPAT
1713 .compat_ioctl = tun_chr_compat_ioctl,
1715 .open = tun_chr_open,
1716 .release = tun_chr_close,
1717 .fasync = tun_chr_fasync
1723 .nodename =
"net/tun",
1733 ethtool_cmd_speed_set(cmd,
SPEED_10);
1779 static const struct ethtool_ops tun_ethtool_ops = {
1780 .get_settings = tun_get_settings,
1781 .get_drvinfo = tun_get_drvinfo,
1782 .get_msglevel = tun_get_msglevel,
1783 .set_msglevel = tun_set_msglevel,
1788 static int __init tun_init(
void)
1797 pr_err(
"Can't register link_ops\n");
1813 static void tun_cleanup(
void)
1826 if (file->
f_op != &tun_fops)
1828 tun = tun_get(file);