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