Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
| 22 | * Add TUNSETLINK ioctl to set the link encapsulation |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 221 | if (!skb_queue_empty(&tun->readq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 252 | if (memcpy_fromiovec(skb_put(skb, len), iv, len)) { |
| 253 | tun->stats.rx_dropped++; |
| 254 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -EFAULT; |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 493 | if (!capable(CAP_NET_ADMIN)) |
| 494 | return -EPERM; |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* Set dev type */ |
| 497 | if (ifr->ifr_flags & IFF_TUN) { |
| 498 | /* TUN device */ |
| 499 | flags |= TUN_TUN_DEV; |
| 500 | name = "tun%d"; |
| 501 | } else if (ifr->ifr_flags & IFF_TAP) { |
| 502 | /* TAP device */ |
| 503 | flags |= TUN_TAP_DEV; |
| 504 | name = "tap%d"; |
| 505 | } else |
| 506 | goto failed; |
| 507 | |
| 508 | if (*ifr->ifr_name) |
| 509 | name = ifr->ifr_name; |
| 510 | |
| 511 | dev = alloc_netdev(sizeof(struct tun_struct), name, |
| 512 | tun_setup); |
| 513 | if (!dev) |
| 514 | return -ENOMEM; |
| 515 | |
| 516 | tun = netdev_priv(dev); |
| 517 | tun->dev = dev; |
| 518 | tun->flags = flags; |
| 519 | /* Be promiscuous by default to maintain previous behaviour. */ |
| 520 | tun->if_flags = IFF_PROMISC; |
| 521 | /* Generate random Ethernet address. */ |
| 522 | *(u16 *)tun->dev_addr = htons(0x00FF); |
| 523 | get_random_bytes(tun->dev_addr + sizeof(u16), 4); |
| 524 | memset(tun->chr_filter, 0, sizeof tun->chr_filter); |
| 525 | |
| 526 | tun_net_init(dev); |
| 527 | |
| 528 | if (strchr(dev->name, '%')) { |
| 529 | err = dev_alloc_name(dev, dev->name); |
| 530 | if (err < 0) |
| 531 | goto err_free_dev; |
| 532 | } |
| 533 | |
| 534 | err = register_netdevice(tun->dev); |
| 535 | if (err < 0) |
| 536 | goto err_free_dev; |
| 537 | |
| 538 | list_add(&tun->list, &tun_dev_list); |
| 539 | } |
| 540 | |
| 541 | DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); |
| 542 | |
| 543 | if (ifr->ifr_flags & IFF_NO_PI) |
| 544 | tun->flags |= TUN_NO_PI; |
| 545 | |
| 546 | if (ifr->ifr_flags & IFF_ONE_QUEUE) |
| 547 | tun->flags |= TUN_ONE_QUEUE; |
| 548 | |
| 549 | file->private_data = tun; |
| 550 | tun->attached = 1; |
| 551 | |
| 552 | strcpy(ifr->ifr_name, tun->dev->name); |
| 553 | return 0; |
| 554 | |
| 555 | err_free_dev: |
| 556 | free_netdev(dev); |
| 557 | failed: |
| 558 | return err; |
| 559 | } |
| 560 | |
| 561 | static int tun_chr_ioctl(struct inode *inode, struct file *file, |
| 562 | unsigned int cmd, unsigned long arg) |
| 563 | { |
| 564 | struct tun_struct *tun = file->private_data; |
| 565 | void __user* argp = (void __user*)arg; |
| 566 | struct ifreq ifr; |
| 567 | |
| 568 | if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) |
| 569 | if (copy_from_user(&ifr, argp, sizeof ifr)) |
| 570 | return -EFAULT; |
| 571 | |
| 572 | if (cmd == TUNSETIFF && !tun) { |
| 573 | int err; |
| 574 | |
| 575 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
| 576 | |
| 577 | rtnl_lock(); |
| 578 | err = tun_set_iff(file, &ifr); |
| 579 | rtnl_unlock(); |
| 580 | |
| 581 | if (err) |
| 582 | return err; |
| 583 | |
| 584 | if (copy_to_user(argp, &ifr, sizeof(ifr))) |
| 585 | return -EFAULT; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | if (!tun) |
| 590 | return -EBADFD; |
| 591 | |
| 592 | DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd); |
| 593 | |
| 594 | switch (cmd) { |
| 595 | case TUNSETNOCSUM: |
| 596 | /* Disable/Enable checksum */ |
| 597 | if (arg) |
| 598 | tun->flags |= TUN_NOCHECKSUM; |
| 599 | else |
| 600 | tun->flags &= ~TUN_NOCHECKSUM; |
| 601 | |
| 602 | DBG(KERN_INFO "%s: checksum %s\n", |
| 603 | tun->dev->name, arg ? "disabled" : "enabled"); |
| 604 | break; |
| 605 | |
| 606 | case TUNSETPERSIST: |
| 607 | /* Disable/Enable persist mode */ |
| 608 | if (arg) |
| 609 | tun->flags |= TUN_PERSIST; |
| 610 | else |
| 611 | tun->flags &= ~TUN_PERSIST; |
| 612 | |
| 613 | DBG(KERN_INFO "%s: persist %s\n", |
| 614 | tun->dev->name, arg ? "disabled" : "enabled"); |
| 615 | break; |
| 616 | |
| 617 | case TUNSETOWNER: |
| 618 | /* Set owner of the device */ |
| 619 | tun->owner = (uid_t) arg; |
| 620 | |
| 621 | DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner); |
| 622 | break; |
| 623 | |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 624 | case TUNSETLINK: |
| 625 | /* Only allow setting the type when the interface is down */ |
| 626 | if (tun->dev->flags & IFF_UP) { |
| 627 | DBG(KERN_INFO "%s: Linktype set failed because interface is up\n", |
| 628 | tun->dev->name); |
| 629 | return -EBUSY; |
| 630 | } else { |
| 631 | tun->dev->type = (int) arg; |
| 632 | DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type); |
| 633 | } |
| 634 | break; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | #ifdef TUN_DEBUG |
| 637 | case TUNSETDEBUG: |
| 638 | tun->debug = arg; |
| 639 | break; |
| 640 | #endif |
| 641 | |
| 642 | case SIOCGIFFLAGS: |
| 643 | ifr.ifr_flags = tun->if_flags; |
| 644 | if (copy_to_user( argp, &ifr, sizeof ifr)) |
| 645 | return -EFAULT; |
| 646 | return 0; |
| 647 | |
| 648 | case SIOCSIFFLAGS: |
| 649 | /** Set the character device's interface flags. Currently only |
| 650 | * IFF_PROMISC and IFF_ALLMULTI are used. */ |
| 651 | tun->if_flags = ifr.ifr_flags; |
| 652 | DBG(KERN_INFO "%s: interface flags 0x%lx\n", |
| 653 | tun->dev->name, tun->if_flags); |
| 654 | return 0; |
| 655 | |
| 656 | case SIOCGIFHWADDR: |
| 657 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr, |
| 658 | min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); |
| 659 | if (copy_to_user( argp, &ifr, sizeof ifr)) |
| 660 | return -EFAULT; |
| 661 | return 0; |
| 662 | |
| 663 | case SIOCSIFHWADDR: |
| 664 | /** Set the character device's hardware address. This is used when |
| 665 | * filtering packets being sent from the network device to the character |
| 666 | * device. */ |
| 667 | memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data, |
| 668 | min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); |
| 669 | DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n", |
| 670 | tun->dev->name, |
| 671 | tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2], |
| 672 | tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]); |
| 673 | return 0; |
| 674 | |
| 675 | case SIOCADDMULTI: |
| 676 | /** Add the specified group to the character device's multicast filter |
| 677 | * list. */ |
| 678 | add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); |
| 679 | DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n", |
| 680 | tun->dev->name, |
| 681 | (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1], |
| 682 | (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3], |
| 683 | (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]); |
| 684 | return 0; |
| 685 | |
| 686 | case SIOCDELMULTI: |
| 687 | /** Remove the specified group from the character device's multicast |
| 688 | * filter list. */ |
| 689 | del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); |
| 690 | DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n", |
| 691 | tun->dev->name, |
| 692 | (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1], |
| 693 | (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3], |
| 694 | (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]); |
| 695 | return 0; |
| 696 | |
| 697 | default: |
| 698 | return -EINVAL; |
| 699 | }; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int tun_chr_fasync(int fd, struct file *file, int on) |
| 705 | { |
| 706 | struct tun_struct *tun = file->private_data; |
| 707 | int ret; |
| 708 | |
| 709 | if (!tun) |
| 710 | return -EBADFD; |
| 711 | |
| 712 | DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on); |
| 713 | |
| 714 | if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0) |
| 715 | return ret; |
| 716 | |
| 717 | if (on) { |
| 718 | ret = f_setown(file, current->pid, 0); |
| 719 | if (ret) |
| 720 | return ret; |
| 721 | tun->flags |= TUN_FASYNC; |
| 722 | } else |
| 723 | tun->flags &= ~TUN_FASYNC; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int tun_chr_open(struct inode *inode, struct file * file) |
| 729 | { |
| 730 | DBG1(KERN_INFO "tunX: tun_chr_open\n"); |
| 731 | file->private_data = NULL; |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static int tun_chr_close(struct inode *inode, struct file *file) |
| 736 | { |
| 737 | struct tun_struct *tun = file->private_data; |
| 738 | |
| 739 | if (!tun) |
| 740 | return 0; |
| 741 | |
| 742 | DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name); |
| 743 | |
| 744 | tun_chr_fasync(-1, file, 0); |
| 745 | |
| 746 | rtnl_lock(); |
| 747 | |
| 748 | /* Detach from net device */ |
| 749 | file->private_data = NULL; |
| 750 | tun->attached = 0; |
| 751 | |
| 752 | /* Drop read queue */ |
| 753 | skb_queue_purge(&tun->readq); |
| 754 | |
| 755 | if (!(tun->flags & TUN_PERSIST)) { |
| 756 | list_del(&tun->list); |
| 757 | unregister_netdevice(tun->dev); |
| 758 | } |
| 759 | |
| 760 | rtnl_unlock(); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static struct file_operations tun_fops = { |
| 766 | .owner = THIS_MODULE, |
| 767 | .llseek = no_llseek, |
| 768 | .read = tun_chr_read, |
| 769 | .readv = tun_chr_readv, |
| 770 | .write = tun_chr_write, |
| 771 | .writev = tun_chr_writev, |
| 772 | .poll = tun_chr_poll, |
| 773 | .ioctl = tun_chr_ioctl, |
| 774 | .open = tun_chr_open, |
| 775 | .release = tun_chr_close, |
| 776 | .fasync = tun_chr_fasync |
| 777 | }; |
| 778 | |
| 779 | static struct miscdevice tun_miscdev = { |
| 780 | .minor = TUN_MINOR, |
| 781 | .name = "tun", |
| 782 | .fops = &tun_fops, |
| 783 | .devfs_name = "net/tun", |
| 784 | }; |
| 785 | |
| 786 | /* ethtool interface */ |
| 787 | |
| 788 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 789 | { |
| 790 | cmd->supported = 0; |
| 791 | cmd->advertising = 0; |
| 792 | cmd->speed = SPEED_10; |
| 793 | cmd->duplex = DUPLEX_FULL; |
| 794 | cmd->port = PORT_TP; |
| 795 | cmd->phy_address = 0; |
| 796 | cmd->transceiver = XCVR_INTERNAL; |
| 797 | cmd->autoneg = AUTONEG_DISABLE; |
| 798 | cmd->maxtxpkt = 0; |
| 799 | cmd->maxrxpkt = 0; |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 804 | { |
| 805 | struct tun_struct *tun = netdev_priv(dev); |
| 806 | |
| 807 | strcpy(info->driver, DRV_NAME); |
| 808 | strcpy(info->version, DRV_VERSION); |
| 809 | strcpy(info->fw_version, "N/A"); |
| 810 | |
| 811 | switch (tun->flags & TUN_TYPE_MASK) { |
| 812 | case TUN_TUN_DEV: |
| 813 | strcpy(info->bus_info, "tun"); |
| 814 | break; |
| 815 | case TUN_TAP_DEV: |
| 816 | strcpy(info->bus_info, "tap"); |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static u32 tun_get_msglevel(struct net_device *dev) |
| 822 | { |
| 823 | #ifdef TUN_DEBUG |
| 824 | struct tun_struct *tun = netdev_priv(dev); |
| 825 | return tun->debug; |
| 826 | #else |
| 827 | return -EOPNOTSUPP; |
| 828 | #endif |
| 829 | } |
| 830 | |
| 831 | static void tun_set_msglevel(struct net_device *dev, u32 value) |
| 832 | { |
| 833 | #ifdef TUN_DEBUG |
| 834 | struct tun_struct *tun = netdev_priv(dev); |
| 835 | tun->debug = value; |
| 836 | #endif |
| 837 | } |
| 838 | |
| 839 | static u32 tun_get_link(struct net_device *dev) |
| 840 | { |
| 841 | struct tun_struct *tun = netdev_priv(dev); |
| 842 | return tun->attached; |
| 843 | } |
| 844 | |
| 845 | static u32 tun_get_rx_csum(struct net_device *dev) |
| 846 | { |
| 847 | struct tun_struct *tun = netdev_priv(dev); |
| 848 | return (tun->flags & TUN_NOCHECKSUM) == 0; |
| 849 | } |
| 850 | |
| 851 | static int tun_set_rx_csum(struct net_device *dev, u32 data) |
| 852 | { |
| 853 | struct tun_struct *tun = netdev_priv(dev); |
| 854 | if (data) |
| 855 | tun->flags &= ~TUN_NOCHECKSUM; |
| 856 | else |
| 857 | tun->flags |= TUN_NOCHECKSUM; |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static struct ethtool_ops tun_ethtool_ops = { |
| 862 | .get_settings = tun_get_settings, |
| 863 | .get_drvinfo = tun_get_drvinfo, |
| 864 | .get_msglevel = tun_get_msglevel, |
| 865 | .set_msglevel = tun_set_msglevel, |
| 866 | .get_link = tun_get_link, |
| 867 | .get_rx_csum = tun_get_rx_csum, |
| 868 | .set_rx_csum = tun_set_rx_csum |
| 869 | }; |
| 870 | |
| 871 | static int __init tun_init(void) |
| 872 | { |
| 873 | int ret = 0; |
| 874 | |
| 875 | printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
| 876 | printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT); |
| 877 | |
| 878 | ret = misc_register(&tun_miscdev); |
| 879 | if (ret) |
| 880 | printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR); |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | static void tun_cleanup(void) |
| 885 | { |
| 886 | struct tun_struct *tun, *nxt; |
| 887 | |
| 888 | misc_deregister(&tun_miscdev); |
| 889 | |
| 890 | rtnl_lock(); |
| 891 | list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) { |
| 892 | DBG(KERN_INFO "%s cleaned up\n", tun->dev->name); |
| 893 | unregister_netdevice(tun->dev); |
| 894 | } |
| 895 | rtnl_unlock(); |
| 896 | |
| 897 | } |
| 898 | |
| 899 | module_init(tun_init); |
| 900 | module_exit(tun_cleanup); |
| 901 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 902 | MODULE_AUTHOR(DRV_COPYRIGHT); |
| 903 | MODULE_LICENSE("GPL"); |
| 904 | MODULE_ALIAS_MISCDEV(TUN_MINOR); |