1 /* 2 * TUN - Universal TUN/TAP device driver. 3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ 16 */ 17 18 /* 19 * Changes: 20 * 21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 22 * Add TUNSETLINK ioctl to set the link encapsulation 23 * 24 * Mark Smith <markzzzsmith@yahoo.com.au> 25 * Use 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/config.h> 43 #include <linux/module.h> 44 #include <linux/errno.h> 45 #include <linux/kernel.h> 46 #include <linux/major.h> 47 #include <linux/slab.h> 48 #include <linux/poll.h> 49 #include <linux/fcntl.h> 50 #include <linux/init.h> 51 #include <linux/skbuff.h> 52 #include <linux/netdevice.h> 53 #include <linux/etherdevice.h> 54 #include <linux/miscdevice.h> 55 #include <linux/ethtool.h> 56 #include <linux/rtnetlink.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 63 #include <asm/system.h> 64 #include <asm/uaccess.h> 65 66 #ifdef TUN_DEBUG 67 static int debug; 68 #endif 69 70 /* Network device part of the driver */ 71 72 static LIST_HEAD(tun_dev_list); 73 static struct ethtool_ops tun_ethtool_ops; 74 75 /* Net device open. */ 76 static int tun_net_open(struct net_device *dev) 77 { 78 netif_start_queue(dev); 79 return 0; 80 } 81 82 /* Net device close. */ 83 static int tun_net_close(struct net_device *dev) 84 { 85 netif_stop_queue(dev); 86 return 0; 87 } 88 89 /* Net device start xmit */ 90 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 91 { 92 struct tun_struct *tun = netdev_priv(dev); 93 94 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len); 95 96 /* Drop packet if interface is not attached */ 97 if (!tun->attached) 98 goto drop; 99 100 /* Packet dropping */ 101 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) { 102 if (!(tun->flags & TUN_ONE_QUEUE)) { 103 /* Normal queueing mode. */ 104 /* Packet scheduler handles dropping of further packets. */ 105 netif_stop_queue(dev); 106 107 /* We won't see all dropped packets individually, so overrun 108 * error is more appropriate. */ 109 tun->stats.tx_fifo_errors++; 110 } else { 111 /* Single queue mode. 112 * Driver handles dropping of all packets itself. */ 113 goto drop; 114 } 115 } 116 117 /* Queue packet */ 118 skb_queue_tail(&tun->readq, skb); 119 dev->trans_start = jiffies; 120 121 /* Notify and wake up reader process */ 122 if (tun->flags & TUN_FASYNC) 123 kill_fasync(&tun->fasync, SIGIO, POLL_IN); 124 wake_up_interruptible(&tun->read_wait); 125 return 0; 126 127 drop: 128 tun->stats.tx_dropped++; 129 kfree_skb(skb); 130 return 0; 131 } 132 133 /** Add the specified Ethernet address to this multicast filter. */ 134 static void 135 add_multi(u32* filter, const u8* addr) 136 { 137 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26; 138 filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 139 } 140 141 /** Remove the specified Ethernet addres from this multicast filter. */ 142 static void 143 del_multi(u32* filter, const u8* addr) 144 { 145 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26; 146 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31)); 147 } 148 149 /** Update the list of multicast groups to which the network device belongs. 150 * This list is used to filter packets being sent from the character device to 151 * the network device. */ 152 static void 153 tun_net_mclist(struct net_device *dev) 154 { 155 struct tun_struct *tun = netdev_priv(dev); 156 const struct dev_mc_list *mclist; 157 int i; 158 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n", 159 dev->name, dev->mc_count); 160 memset(tun->chr_filter, 0, sizeof tun->chr_filter); 161 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL; 162 i++, mclist = mclist->next) { 163 add_multi(tun->net_filter, mclist->dmi_addr); 164 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n", 165 dev->name, 166 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2], 167 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]); 168 } 169 } 170 171 static struct net_device_stats *tun_net_stats(struct net_device *dev) 172 { 173 struct tun_struct *tun = netdev_priv(dev); 174 return &tun->stats; 175 } 176 177 /* Initialize net device. */ 178 static void tun_net_init(struct net_device *dev) 179 { 180 struct tun_struct *tun = netdev_priv(dev); 181 182 switch (tun->flags & TUN_TYPE_MASK) { 183 case TUN_TUN_DEV: 184 /* Point-to-Point TUN Device */ 185 dev->hard_header_len = 0; 186 dev->addr_len = 0; 187 dev->mtu = 1500; 188 189 /* Zero header length */ 190 dev->type = ARPHRD_NONE; 191 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 192 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 193 break; 194 195 case TUN_TAP_DEV: 196 /* Ethernet TAP Device */ 197 dev->set_multicast_list = tun_net_mclist; 198 199 ether_setup(dev); 200 random_ether_addr(dev->dev_addr); 201 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ 202 break; 203 } 204 } 205 206 /* Character device part */ 207 208 /* Poll */ 209 static unsigned int tun_chr_poll(struct file *file, poll_table * wait) 210 { 211 struct tun_struct *tun = file->private_data; 212 unsigned int mask = POLLOUT | POLLWRNORM; 213 214 if (!tun) 215 return -EBADFD; 216 217 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name); 218 219 poll_wait(file, &tun->read_wait, wait); 220 221 if (!skb_queue_empty(&tun->readq)) 222 mask |= POLLIN | POLLRDNORM; 223 224 return mask; 225 } 226 227 /* Get packet from user space buffer */ 228 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count) 229 { 230 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) }; 231 struct sk_buff *skb; 232 size_t len = count, align = 0; 233 234 if (!(tun->flags & TUN_NO_PI)) { 235 if ((len -= sizeof(pi)) > count) 236 return -EINVAL; 237 238 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi))) 239 return -EFAULT; 240 } 241 242 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) 243 align = NET_IP_ALIGN; 244 245 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) { 246 tun->stats.rx_dropped++; 247 return -ENOMEM; 248 } 249 250 if (align) 251 skb_reserve(skb, align); 252 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) { 253 tun->stats.rx_dropped++; 254 kfree_skb(skb); 255 return -EFAULT; 256 } 257 258 skb->dev = tun->dev; 259 switch (tun->flags & TUN_TYPE_MASK) { 260 case TUN_TUN_DEV: 261 skb->mac.raw = skb->data; 262 skb->protocol = pi.proto; 263 break; 264 case TUN_TAP_DEV: 265 skb->protocol = eth_type_trans(skb, tun->dev); 266 break; 267 }; 268 269 if (tun->flags & TUN_NOCHECKSUM) 270 skb->ip_summed = CHECKSUM_UNNECESSARY; 271 272 netif_rx_ni(skb); 273 tun->dev->last_rx = jiffies; 274 275 tun->stats.rx_packets++; 276 tun->stats.rx_bytes += len; 277 278 return count; 279 } 280 281 static inline size_t iov_total(const struct iovec *iv, unsigned long count) 282 { 283 unsigned long i; 284 size_t len; 285 286 for (i = 0, len = 0; i < count; i++) 287 len += iv[i].iov_len; 288 289 return len; 290 } 291 292 /* Writev */ 293 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv, 294 unsigned long count, loff_t *pos) 295 { 296 struct tun_struct *tun = file->private_data; 297 298 if (!tun) 299 return -EBADFD; 300 301 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count); 302 303 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count)); 304 } 305 306 /* Write */ 307 static ssize_t tun_chr_write(struct file * file, const char __user * buf, 308 size_t count, loff_t *pos) 309 { 310 struct iovec iv = { (void __user *) buf, count }; 311 return tun_chr_writev(file, &iv, 1, pos); 312 } 313 314 /* Put packet to the user space buffer */ 315 static __inline__ ssize_t tun_put_user(struct tun_struct *tun, 316 struct sk_buff *skb, 317 struct iovec *iv, int len) 318 { 319 struct tun_pi pi = { 0, skb->protocol }; 320 ssize_t total = 0; 321 322 if (!(tun->flags & TUN_NO_PI)) { 323 if ((len -= sizeof(pi)) < 0) 324 return -EINVAL; 325 326 if (len < skb->len) { 327 /* Packet will be striped */ 328 pi.flags |= TUN_PKT_STRIP; 329 } 330 331 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi))) 332 return -EFAULT; 333 total += sizeof(pi); 334 } 335 336 len = min_t(int, skb->len, len); 337 338 skb_copy_datagram_iovec(skb, 0, iv, len); 339 total += len; 340 341 tun->stats.tx_packets++; 342 tun->stats.tx_bytes += len; 343 344 return total; 345 } 346 347 /* Readv */ 348 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv, 349 unsigned long count, loff_t *pos) 350 { 351 struct tun_struct *tun = file->private_data; 352 DECLARE_WAITQUEUE(wait, current); 353 struct sk_buff *skb; 354 ssize_t len, ret = 0; 355 356 if (!tun) 357 return -EBADFD; 358 359 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name); 360 361 len = iov_total(iv, count); 362 if (len < 0) 363 return -EINVAL; 364 365 add_wait_queue(&tun->read_wait, &wait); 366 while (len) { 367 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 368 u8 addr[ ETH_ALEN]; 369 int bit_nr; 370 371 current->state = TASK_INTERRUPTIBLE; 372 373 /* Read frames from the queue */ 374 if (!(skb=skb_dequeue(&tun->readq))) { 375 if (file->f_flags & O_NONBLOCK) { 376 ret = -EAGAIN; 377 break; 378 } 379 if (signal_pending(current)) { 380 ret = -ERESTARTSYS; 381 break; 382 } 383 384 /* Nothing to read, let's sleep */ 385 schedule(); 386 continue; 387 } 388 netif_wake_queue(tun->dev); 389 390 /** Decide whether to accept this packet. This code is designed to 391 * behave identically to an Ethernet interface. Accept the packet if 392 * - we are promiscuous. 393 * - the packet is addressed to us. 394 * - the packet is broadcast. 395 * - the packet is multicast and 396 * - we are multicast promiscous. 397 * - we belong to the multicast group. 398 */ 399 memcpy(addr, skb->data, 400 min_t(size_t, sizeof addr, skb->len)); 401 bit_nr = ether_crc(sizeof addr, addr) >> 26; 402 if ((tun->if_flags & IFF_PROMISC) || 403 memcmp(addr, tun->dev_addr, sizeof addr) == 0 || 404 memcmp(addr, ones, sizeof addr) == 0 || 405 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) || 406 (addr[0] == 0x33 && addr[1] == 0x33)) && 407 ((tun->if_flags & IFF_ALLMULTI) || 408 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) { 409 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n", 410 tun->dev->name, addr[0], addr[1], addr[2], 411 addr[3], addr[4], addr[5]); 412 ret = tun_put_user(tun, skb, (struct iovec *) iv, len); 413 kfree_skb(skb); 414 break; 415 } else { 416 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n", 417 tun->dev->name, addr[0], addr[1], addr[2], 418 addr[3], addr[4], addr[5]); 419 kfree_skb(skb); 420 continue; 421 } 422 } 423 424 current->state = TASK_RUNNING; 425 remove_wait_queue(&tun->read_wait, &wait); 426 427 return ret; 428 } 429 430 /* Read */ 431 static ssize_t tun_chr_read(struct file * file, char __user * buf, 432 size_t count, loff_t *pos) 433 { 434 struct iovec iv = { buf, count }; 435 return tun_chr_readv(file, &iv, 1, pos); 436 } 437 438 static void tun_setup(struct net_device *dev) 439 { 440 struct tun_struct *tun = netdev_priv(dev); 441 442 skb_queue_head_init(&tun->readq); 443 init_waitqueue_head(&tun->read_wait); 444 445 tun->owner = -1; 446 447 SET_MODULE_OWNER(dev); 448 dev->open = tun_net_open; 449 dev->hard_start_xmit = tun_net_xmit; 450 dev->stop = tun_net_close; 451 dev->get_stats = tun_net_stats; 452 dev->ethtool_ops = &tun_ethtool_ops; 453 dev->destructor = free_netdev; 454 } 455 456 static struct tun_struct *tun_get_by_name(const char *name) 457 { 458 struct tun_struct *tun; 459 460 ASSERT_RTNL(); 461 list_for_each_entry(tun, &tun_dev_list, list) { 462 if (!strncmp(tun->dev->name, name, IFNAMSIZ)) 463 return tun; 464 } 465 466 return NULL; 467 } 468 469 static int tun_set_iff(struct file *file, struct ifreq *ifr) 470 { 471 struct tun_struct *tun; 472 struct net_device *dev; 473 int err; 474 475 tun = tun_get_by_name(ifr->ifr_name); 476 if (tun) { 477 if (tun->attached) 478 return -EBUSY; 479 480 /* Check permissions */ 481 if (tun->owner != -1 && 482 current->euid != tun->owner && !capable(CAP_NET_ADMIN)) 483 return -EPERM; 484 } 485 else if (__dev_get_by_name(ifr->ifr_name)) 486 return -EINVAL; 487 else { 488 char *name; 489 unsigned long flags = 0; 490 491 err = -EINVAL; 492 493 /* Set dev type */ 494 if (ifr->ifr_flags & IFF_TUN) { 495 /* TUN device */ 496 flags |= TUN_TUN_DEV; 497 name = "tun%d"; 498 } else if (ifr->ifr_flags & IFF_TAP) { 499 /* TAP device */ 500 flags |= TUN_TAP_DEV; 501 name = "tap%d"; 502 } else 503 goto failed; 504 505 if (*ifr->ifr_name) 506 name = ifr->ifr_name; 507 508 dev = alloc_netdev(sizeof(struct tun_struct), name, 509 tun_setup); 510 if (!dev) 511 return -ENOMEM; 512 513 tun = netdev_priv(dev); 514 tun->dev = dev; 515 tun->flags = flags; 516 /* Be promiscuous by default to maintain previous behaviour. */ 517 tun->if_flags = IFF_PROMISC; 518 /* Generate random Ethernet address. */ 519 *(u16 *)tun->dev_addr = htons(0x00FF); 520 get_random_bytes(tun->dev_addr + sizeof(u16), 4); 521 memset(tun->chr_filter, 0, sizeof tun->chr_filter); 522 523 tun_net_init(dev); 524 525 if (strchr(dev->name, '%')) { 526 err = dev_alloc_name(dev, dev->name); 527 if (err < 0) 528 goto err_free_dev; 529 } 530 531 err = register_netdevice(tun->dev); 532 if (err < 0) 533 goto err_free_dev; 534 535 list_add(&tun->list, &tun_dev_list); 536 } 537 538 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); 539 540 if (ifr->ifr_flags & IFF_NO_PI) 541 tun->flags |= TUN_NO_PI; 542 543 if (ifr->ifr_flags & IFF_ONE_QUEUE) 544 tun->flags |= TUN_ONE_QUEUE; 545 546 file->private_data = tun; 547 tun->attached = 1; 548 549 strcpy(ifr->ifr_name, tun->dev->name); 550 return 0; 551 552 err_free_dev: 553 free_netdev(dev); 554 failed: 555 return err; 556 } 557 558 static int tun_chr_ioctl(struct inode *inode, struct file *file, 559 unsigned int cmd, unsigned long arg) 560 { 561 struct tun_struct *tun = file->private_data; 562 void __user* argp = (void __user*)arg; 563 struct ifreq ifr; 564 565 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) 566 if (copy_from_user(&ifr, argp, sizeof ifr)) 567 return -EFAULT; 568 569 if (cmd == TUNSETIFF && !tun) { 570 int err; 571 572 ifr.ifr_name[IFNAMSIZ-1] = '\0'; 573 574 rtnl_lock(); 575 err = tun_set_iff(file, &ifr); 576 rtnl_unlock(); 577 578 if (err) 579 return err; 580 581 if (copy_to_user(argp, &ifr, sizeof(ifr))) 582 return -EFAULT; 583 return 0; 584 } 585 586 if (!tun) 587 return -EBADFD; 588 589 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd); 590 591 switch (cmd) { 592 case TUNSETNOCSUM: 593 /* Disable/Enable checksum */ 594 if (arg) 595 tun->flags |= TUN_NOCHECKSUM; 596 else 597 tun->flags &= ~TUN_NOCHECKSUM; 598 599 DBG(KERN_INFO "%s: checksum %s\n", 600 tun->dev->name, arg ? "disabled" : "enabled"); 601 break; 602 603 case TUNSETPERSIST: 604 /* Disable/Enable persist mode */ 605 if (arg) 606 tun->flags |= TUN_PERSIST; 607 else 608 tun->flags &= ~TUN_PERSIST; 609 610 DBG(KERN_INFO "%s: persist %s\n", 611 tun->dev->name, arg ? "disabled" : "enabled"); 612 break; 613 614 case TUNSETOWNER: 615 /* Set owner of the device */ 616 tun->owner = (uid_t) arg; 617 618 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner); 619 break; 620 621 case TUNSETLINK: 622 /* Only allow setting the type when the interface is down */ 623 if (tun->dev->flags & IFF_UP) { 624 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n", 625 tun->dev->name); 626 return -EBUSY; 627 } else { 628 tun->dev->type = (int) arg; 629 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type); 630 } 631 break; 632 633 #ifdef TUN_DEBUG 634 case TUNSETDEBUG: 635 tun->debug = arg; 636 break; 637 #endif 638 639 case SIOCGIFFLAGS: 640 ifr.ifr_flags = tun->if_flags; 641 if (copy_to_user( argp, &ifr, sizeof ifr)) 642 return -EFAULT; 643 return 0; 644 645 case SIOCSIFFLAGS: 646 /** Set the character device's interface flags. Currently only 647 * IFF_PROMISC and IFF_ALLMULTI are used. */ 648 tun->if_flags = ifr.ifr_flags; 649 DBG(KERN_INFO "%s: interface flags 0x%lx\n", 650 tun->dev->name, tun->if_flags); 651 return 0; 652 653 case SIOCGIFHWADDR: 654 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr, 655 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); 656 if (copy_to_user( argp, &ifr, sizeof ifr)) 657 return -EFAULT; 658 return 0; 659 660 case SIOCSIFHWADDR: 661 /** Set the character device's hardware address. This is used when 662 * filtering packets being sent from the network device to the character 663 * device. */ 664 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data, 665 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); 666 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n", 667 tun->dev->name, 668 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2], 669 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]); 670 return 0; 671 672 case SIOCADDMULTI: 673 /** Add the specified group to the character device's multicast filter 674 * list. */ 675 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); 676 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n", 677 tun->dev->name, 678 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1], 679 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3], 680 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]); 681 return 0; 682 683 case SIOCDELMULTI: 684 /** Remove the specified group from the character device's multicast 685 * filter list. */ 686 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); 687 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n", 688 tun->dev->name, 689 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1], 690 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3], 691 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]); 692 return 0; 693 694 default: 695 return -EINVAL; 696 }; 697 698 return 0; 699 } 700 701 static int tun_chr_fasync(int fd, struct file *file, int on) 702 { 703 struct tun_struct *tun = file->private_data; 704 int ret; 705 706 if (!tun) 707 return -EBADFD; 708 709 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on); 710 711 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0) 712 return ret; 713 714 if (on) { 715 ret = f_setown(file, current->pid, 0); 716 if (ret) 717 return ret; 718 tun->flags |= TUN_FASYNC; 719 } else 720 tun->flags &= ~TUN_FASYNC; 721 722 return 0; 723 } 724 725 static int tun_chr_open(struct inode *inode, struct file * file) 726 { 727 DBG1(KERN_INFO "tunX: tun_chr_open\n"); 728 file->private_data = NULL; 729 return 0; 730 } 731 732 static int tun_chr_close(struct inode *inode, struct file *file) 733 { 734 struct tun_struct *tun = file->private_data; 735 736 if (!tun) 737 return 0; 738 739 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name); 740 741 tun_chr_fasync(-1, file, 0); 742 743 rtnl_lock(); 744 745 /* Detach from net device */ 746 file->private_data = NULL; 747 tun->attached = 0; 748 749 /* Drop read queue */ 750 skb_queue_purge(&tun->readq); 751 752 if (!(tun->flags & TUN_PERSIST)) { 753 list_del(&tun->list); 754 unregister_netdevice(tun->dev); 755 } 756 757 rtnl_unlock(); 758 759 return 0; 760 } 761 762 static struct file_operations tun_fops = { 763 .owner = THIS_MODULE, 764 .llseek = no_llseek, 765 .read = tun_chr_read, 766 .readv = tun_chr_readv, 767 .write = tun_chr_write, 768 .writev = tun_chr_writev, 769 .poll = tun_chr_poll, 770 .ioctl = tun_chr_ioctl, 771 .open = tun_chr_open, 772 .release = tun_chr_close, 773 .fasync = tun_chr_fasync 774 }; 775 776 static struct miscdevice tun_miscdev = { 777 .minor = TUN_MINOR, 778 .name = "tun", 779 .fops = &tun_fops, 780 .devfs_name = "net/tun", 781 }; 782 783 /* ethtool interface */ 784 785 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 786 { 787 cmd->supported = 0; 788 cmd->advertising = 0; 789 cmd->speed = SPEED_10; 790 cmd->duplex = DUPLEX_FULL; 791 cmd->port = PORT_TP; 792 cmd->phy_address = 0; 793 cmd->transceiver = XCVR_INTERNAL; 794 cmd->autoneg = AUTONEG_DISABLE; 795 cmd->maxtxpkt = 0; 796 cmd->maxrxpkt = 0; 797 return 0; 798 } 799 800 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 801 { 802 struct tun_struct *tun = netdev_priv(dev); 803 804 strcpy(info->driver, DRV_NAME); 805 strcpy(info->version, DRV_VERSION); 806 strcpy(info->fw_version, "N/A"); 807 808 switch (tun->flags & TUN_TYPE_MASK) { 809 case TUN_TUN_DEV: 810 strcpy(info->bus_info, "tun"); 811 break; 812 case TUN_TAP_DEV: 813 strcpy(info->bus_info, "tap"); 814 break; 815 } 816 } 817 818 static u32 tun_get_msglevel(struct net_device *dev) 819 { 820 #ifdef TUN_DEBUG 821 struct tun_struct *tun = netdev_priv(dev); 822 return tun->debug; 823 #else 824 return -EOPNOTSUPP; 825 #endif 826 } 827 828 static void tun_set_msglevel(struct net_device *dev, u32 value) 829 { 830 #ifdef TUN_DEBUG 831 struct tun_struct *tun = netdev_priv(dev); 832 tun->debug = value; 833 #endif 834 } 835 836 static u32 tun_get_link(struct net_device *dev) 837 { 838 struct tun_struct *tun = netdev_priv(dev); 839 return tun->attached; 840 } 841 842 static u32 tun_get_rx_csum(struct net_device *dev) 843 { 844 struct tun_struct *tun = netdev_priv(dev); 845 return (tun->flags & TUN_NOCHECKSUM) == 0; 846 } 847 848 static int tun_set_rx_csum(struct net_device *dev, u32 data) 849 { 850 struct tun_struct *tun = netdev_priv(dev); 851 if (data) 852 tun->flags &= ~TUN_NOCHECKSUM; 853 else 854 tun->flags |= TUN_NOCHECKSUM; 855 return 0; 856 } 857 858 static struct ethtool_ops tun_ethtool_ops = { 859 .get_settings = tun_get_settings, 860 .get_drvinfo = tun_get_drvinfo, 861 .get_msglevel = tun_get_msglevel, 862 .set_msglevel = tun_set_msglevel, 863 .get_link = tun_get_link, 864 .get_rx_csum = tun_get_rx_csum, 865 .set_rx_csum = tun_set_rx_csum 866 }; 867 868 static int __init tun_init(void) 869 { 870 int ret = 0; 871 872 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 873 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT); 874 875 ret = misc_register(&tun_miscdev); 876 if (ret) 877 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR); 878 return ret; 879 } 880 881 static void tun_cleanup(void) 882 { 883 struct tun_struct *tun, *nxt; 884 885 misc_deregister(&tun_miscdev); 886 887 rtnl_lock(); 888 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) { 889 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name); 890 unregister_netdevice(tun->dev); 891 } 892 rtnl_unlock(); 893 894 } 895 896 module_init(tun_init); 897 module_exit(tun_cleanup); 898 MODULE_DESCRIPTION(DRV_DESCRIPTION); 899 MODULE_AUTHOR(DRV_COPYRIGHT); 900 MODULE_LICENSE("GPL"); 901 MODULE_ALIAS_MISCDEV(TUN_MINOR); 902