tun.c (d040c1614c24162adc3fe106b182596999264e26) | tun.c (ab46d779661d7a03b7aa00279eead5dc3f0b3901) |
---|---|
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. --- 49 unchanged lines hidden (view full) --- 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 <net/net_namespace.h> 65#include <net/netns/generic.h> | 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. --- 49 unchanged lines hidden (view full) --- 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 <net/net_namespace.h> 65#include <net/netns/generic.h> |
66#include <net/rtnetlink.h> 67#include <net/sock.h> |
|
66 67#include <asm/system.h> 68#include <asm/uaccess.h> 69 70/* Uncomment to enable debugging */ 71/* #define TUN_DEBUG 1 */ 72 73#ifdef TUN_DEBUG --- 8 unchanged lines hidden (view full) --- 82 83#define FLT_EXACT_COUNT 8 84struct tap_filter { 85 unsigned int count; /* Number of addrs. Zero means disabled */ 86 u32 mask[2]; /* Mask of the hashed addrs */ 87 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 88}; 89 | 68 69#include <asm/system.h> 70#include <asm/uaccess.h> 71 72/* Uncomment to enable debugging */ 73/* #define TUN_DEBUG 1 */ 74 75#ifdef TUN_DEBUG --- 8 unchanged lines hidden (view full) --- 84 85#define FLT_EXACT_COUNT 8 86struct tap_filter { 87 unsigned int count; /* Number of addrs. Zero means disabled */ 88 u32 mask[2]; /* Mask of the hashed addrs */ 89 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 90}; 91 |
92struct tun_file { 93 atomic_t count; 94 struct tun_struct *tun; 95 struct net *net; 96 wait_queue_head_t read_wait; 97}; 98 99struct tun_sock; 100 |
|
90struct tun_struct { | 101struct tun_struct { |
91 struct list_head list; | 102 struct tun_file *tfile; |
92 unsigned int flags; | 103 unsigned int flags; |
93 int attached; | |
94 uid_t owner; 95 gid_t group; 96 | 104 uid_t owner; 105 gid_t group; 106 |
97 wait_queue_head_t read_wait; | |
98 struct sk_buff_head readq; 99 100 struct net_device *dev; 101 struct fasync_struct *fasync; 102 103 struct tap_filter txflt; | 107 struct sk_buff_head readq; 108 109 struct net_device *dev; 110 struct fasync_struct *fasync; 111 112 struct tap_filter txflt; |
113 struct sock *sk; 114 struct socket socket; |
|
104 105#ifdef TUN_DEBUG 106 int debug; 107#endif 108}; 109 | 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 const struct cred *cred = current_cred(); 135 int err; 136 137 ASSERT_RTNL(); 138 139 /* Check permissions */ 140 if (((tun->owner != -1 && cred->euid != tun->owner) || 141 (tun->group != -1 && !in_egroup_p(tun->group))) && 142 !capable(CAP_NET_ADMIN)) 143 return -EPERM; 144 145 netif_tx_lock_bh(tun->dev); 146 147 err = -EINVAL; 148 if (tfile->tun) 149 goto out; 150 151 err = -EBUSY; 152 if (tun->tfile) 153 goto out; 154 155 err = 0; 156 tfile->tun = tun; 157 tun->tfile = tfile; 158 dev_hold(tun->dev); 159 atomic_inc(&tfile->count); 160 161out: 162 netif_tx_unlock_bh(tun->dev); 163 return err; 164} 165 166static void __tun_detach(struct tun_struct *tun) 167{ 168 struct tun_file *tfile = tun->tfile; 169 170 /* Detach from net device */ 171 netif_tx_lock_bh(tun->dev); 172 tfile->tun = NULL; 173 tun->tfile = NULL; 174 netif_tx_unlock_bh(tun->dev); 175 176 /* Drop read queue */ 177 skb_queue_purge(&tun->readq); 178 179 /* Drop the extra count on the net device */ 180 dev_put(tun->dev); 181} 182 183static void tun_detach(struct tun_struct *tun) 184{ 185 rtnl_lock(); 186 __tun_detach(tun); 187 rtnl_unlock(); 188} 189 190static struct tun_struct *__tun_get(struct tun_file *tfile) 191{ 192 struct tun_struct *tun = NULL; 193 194 if (atomic_inc_not_zero(&tfile->count)) 195 tun = tfile->tun; 196 197 return tun; 198} 199 200static struct tun_struct *tun_get(struct file *file) 201{ 202 return __tun_get(file->private_data); 203} 204 205static void tun_put(struct tun_struct *tun) 206{ 207 struct tun_file *tfile = tun->tfile; 208 209 if (atomic_dec_and_test(&tfile->count)) 210 tun_detach(tfile->tun); 211} 212 |
|
110/* TAP filterting */ 111static void addr_hash_set(u32 *mask, const u8 *addr) 112{ 113 int n = ether_crc(ETH_ALEN, addr) >> 26; 114 mask[n >> 5] |= (1 << (n & 31)); 115} 116 117static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) --- 96 unchanged lines hidden (view full) --- 214 if (!filter->count) 215 return 1; 216 217 return run_filter(filter, skb); 218} 219 220/* Network device part of the driver */ 221 | 213/* TAP filterting */ 214static void addr_hash_set(u32 *mask, const u8 *addr) 215{ 216 int n = ether_crc(ETH_ALEN, addr) >> 26; 217 mask[n >> 5] |= (1 << (n & 31)); 218} 219 220static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) --- 96 unchanged lines hidden (view full) --- 317 if (!filter->count) 318 return 1; 319 320 return run_filter(filter, skb); 321} 322 323/* Network device part of the driver */ 324 |
222static int tun_net_id; 223struct tun_net { 224 struct list_head dev_list; 225}; 226 | |
227static const struct ethtool_ops tun_ethtool_ops; 228 | 325static const struct ethtool_ops tun_ethtool_ops; 326 |
327/* Net device detach from fd. */ 328static void tun_net_uninit(struct net_device *dev) 329{ 330 struct tun_struct *tun = netdev_priv(dev); 331 struct tun_file *tfile = tun->tfile; 332 333 /* Inform the methods they need to stop using the dev. 334 */ 335 if (tfile) { 336 wake_up_all(&tfile->read_wait); 337 if (atomic_dec_and_test(&tfile->count)) 338 __tun_detach(tun); 339 } 340} 341 |
|
229/* Net device open. */ 230static int tun_net_open(struct net_device *dev) 231{ 232 netif_start_queue(dev); 233 return 0; 234} 235 236/* Net device close. */ --- 6 unchanged lines hidden (view full) --- 243/* Net device start xmit */ 244static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 245{ 246 struct tun_struct *tun = netdev_priv(dev); 247 248 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len); 249 250 /* Drop packet if interface is not attached */ | 342/* Net device open. */ 343static int tun_net_open(struct net_device *dev) 344{ 345 netif_start_queue(dev); 346 return 0; 347} 348 349/* Net device close. */ --- 6 unchanged lines hidden (view full) --- 356/* Net device start xmit */ 357static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 358{ 359 struct tun_struct *tun = netdev_priv(dev); 360 361 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len); 362 363 /* Drop packet if interface is not attached */ |
251 if (!tun->attached) | 364 if (!tun->tfile) |
252 goto drop; 253 254 /* Drop if the filter does not like it. 255 * This is a noop if the filter is disabled. 256 * Filter can be enabled only for the TAP devices. */ 257 if (!check_filter(&tun->txflt, skb)) 258 goto drop; 259 --- 15 unchanged lines hidden (view full) --- 275 276 /* Enqueue packet */ 277 skb_queue_tail(&tun->readq, skb); 278 dev->trans_start = jiffies; 279 280 /* Notify and wake up reader process */ 281 if (tun->flags & TUN_FASYNC) 282 kill_fasync(&tun->fasync, SIGIO, POLL_IN); | 365 goto drop; 366 367 /* Drop if the filter does not like it. 368 * This is a noop if the filter is disabled. 369 * Filter can be enabled only for the TAP devices. */ 370 if (!check_filter(&tun->txflt, skb)) 371 goto drop; 372 --- 15 unchanged lines hidden (view full) --- 388 389 /* Enqueue packet */ 390 skb_queue_tail(&tun->readq, skb); 391 dev->trans_start = jiffies; 392 393 /* Notify and wake up reader process */ 394 if (tun->flags & TUN_FASYNC) 395 kill_fasync(&tun->fasync, SIGIO, POLL_IN); |
283 wake_up_interruptible(&tun->read_wait); | 396 wake_up_interruptible(&tun->tfile->read_wait); |
284 return 0; 285 286drop: 287 dev->stats.tx_dropped++; 288 kfree_skb(skb); 289 return 0; 290} 291 --- 15 unchanged lines hidden (view full) --- 307{ 308 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 309 return -EINVAL; 310 dev->mtu = new_mtu; 311 return 0; 312} 313 314static const struct net_device_ops tun_netdev_ops = { | 397 return 0; 398 399drop: 400 dev->stats.tx_dropped++; 401 kfree_skb(skb); 402 return 0; 403} 404 --- 15 unchanged lines hidden (view full) --- 420{ 421 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) 422 return -EINVAL; 423 dev->mtu = new_mtu; 424 return 0; 425} 426 427static const struct net_device_ops tun_netdev_ops = { |
428 .ndo_uninit = tun_net_uninit, |
|
315 .ndo_open = tun_net_open, 316 .ndo_stop = tun_net_close, 317 .ndo_start_xmit = tun_net_xmit, 318 .ndo_change_mtu = tun_net_change_mtu, 319}; 320 321static const struct net_device_ops tap_netdev_ops = { | 429 .ndo_open = tun_net_open, 430 .ndo_stop = tun_net_close, 431 .ndo_start_xmit = tun_net_xmit, 432 .ndo_change_mtu = tun_net_change_mtu, 433}; 434 435static const struct net_device_ops tap_netdev_ops = { |
436 .ndo_uninit = tun_net_uninit, |
|
322 .ndo_open = tun_net_open, 323 .ndo_stop = tun_net_close, 324 .ndo_start_xmit = tun_net_xmit, 325 .ndo_change_mtu = tun_net_change_mtu, 326 .ndo_set_multicast_list = tun_net_mclist, 327 .ndo_set_mac_address = eth_mac_addr, 328 .ndo_validate_addr = eth_validate_addr, 329}; --- 30 unchanged lines hidden (view full) --- 360 } 361} 362 363/* Character device part */ 364 365/* Poll */ 366static unsigned int tun_chr_poll(struct file *file, poll_table * wait) 367{ | 437 .ndo_open = tun_net_open, 438 .ndo_stop = tun_net_close, 439 .ndo_start_xmit = tun_net_xmit, 440 .ndo_change_mtu = tun_net_change_mtu, 441 .ndo_set_multicast_list = tun_net_mclist, 442 .ndo_set_mac_address = eth_mac_addr, 443 .ndo_validate_addr = eth_validate_addr, 444}; --- 30 unchanged lines hidden (view full) --- 475 } 476} 477 478/* Character device part */ 479 480/* Poll */ 481static unsigned int tun_chr_poll(struct file *file, poll_table * wait) 482{ |
368 struct tun_struct *tun = file->private_data; 369 unsigned int mask = POLLOUT | POLLWRNORM; | 483 struct tun_file *tfile = file->private_data; 484 struct tun_struct *tun = __tun_get(tfile); 485 struct sock *sk = tun->sk; 486 unsigned int mask = 0; |
370 371 if (!tun) | 487 488 if (!tun) |
372 return -EBADFD; | 489 return POLLERR; |
373 374 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name); 375 | 490 491 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name); 492 |
376 poll_wait(file, &tun->read_wait, wait); | 493 poll_wait(file, &tfile->read_wait, wait); |
377 378 if (!skb_queue_empty(&tun->readq)) 379 mask |= POLLIN | POLLRDNORM; 380 | 494 495 if (!skb_queue_empty(&tun->readq)) 496 mask |= POLLIN | POLLRDNORM; 497 |
498 if (sock_writeable(sk) || 499 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && 500 sock_writeable(sk))) 501 mask |= POLLOUT | POLLWRNORM; 502 503 if (tun->dev->reg_state != NETREG_REGISTERED) 504 mask = POLLERR; 505 506 tun_put(tun); |
|
381 return mask; 382} 383 384/* prepad is the amount to reserve at front. len is length after that. 385 * linear is a hint as to how much to copy (usually headers). */ | 507 return mask; 508} 509 510/* prepad is the amount to reserve at front. len is length after that. 511 * linear is a hint as to how much to copy (usually headers). */ |
386static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear, 387 gfp_t gfp) | 512static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun, 513 size_t prepad, size_t len, 514 size_t linear, int noblock) |
388{ | 515{ |
516 struct sock *sk = tun->sk; |
|
389 struct sk_buff *skb; | 517 struct sk_buff *skb; |
390 unsigned int i; | 518 int err; |
391 | 519 |
392 skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN); 393 if (skb) { 394 skb_reserve(skb, prepad); 395 skb_put(skb, len); 396 return skb; 397 } 398 | |
399 /* Under a page? Don't bother with paged skb. */ 400 if (prepad + len < PAGE_SIZE) | 520 /* Under a page? Don't bother with paged skb. */ 521 if (prepad + len < PAGE_SIZE) |
401 return NULL; | 522 linear = len; |
402 | 523 |
403 /* Start with a normal skb, and add pages. */ 404 skb = alloc_skb(prepad + linear, gfp); | 524 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 525 &err); |
405 if (!skb) | 526 if (!skb) |
406 return NULL; | 527 return ERR_PTR(err); |
407 408 skb_reserve(skb, prepad); 409 skb_put(skb, linear); | 528 529 skb_reserve(skb, prepad); 530 skb_put(skb, linear); |
531 skb->data_len = len - linear; 532 skb->len += len - linear; |
|
410 | 533 |
411 len -= linear; 412 413 for (i = 0; i < MAX_SKB_FRAGS; i++) { 414 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 415 416 f->page = alloc_page(gfp|__GFP_ZERO); 417 if (!f->page) 418 break; 419 420 f->page_offset = 0; 421 f->size = PAGE_SIZE; 422 423 skb->data_len += PAGE_SIZE; 424 skb->len += PAGE_SIZE; 425 skb->truesize += PAGE_SIZE; 426 skb_shinfo(skb)->nr_frags++; 427 428 if (len < PAGE_SIZE) { 429 len = 0; 430 break; 431 } 432 len -= PAGE_SIZE; 433 } 434 435 /* Too large, or alloc fail? */ 436 if (unlikely(len)) { 437 kfree_skb(skb); 438 skb = NULL; 439 } 440 | |
441 return skb; 442} 443 444/* Get packet from user space buffer */ | 534 return skb; 535} 536 537/* Get packet from user space buffer */ |
445static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count) | 538static __inline__ ssize_t tun_get_user(struct tun_struct *tun, 539 struct iovec *iv, size_t count, 540 int noblock) |
446{ | 541{ |
447 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) }; | 542 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
448 struct sk_buff *skb; 449 size_t len = count, align = 0; 450 struct virtio_net_hdr gso = { 0 }; 451 452 if (!(tun->flags & TUN_NO_PI)) { 453 if ((len -= sizeof(pi)) > count) 454 return -EINVAL; 455 --- 13 unchanged lines hidden (view full) --- 469 } 470 471 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 472 align = NET_IP_ALIGN; 473 if (unlikely(len < ETH_HLEN)) 474 return -EINVAL; 475 } 476 | 543 struct sk_buff *skb; 544 size_t len = count, align = 0; 545 struct virtio_net_hdr gso = { 0 }; 546 547 if (!(tun->flags & TUN_NO_PI)) { 548 if ((len -= sizeof(pi)) > count) 549 return -EINVAL; 550 --- 13 unchanged lines hidden (view full) --- 564 } 565 566 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 567 align = NET_IP_ALIGN; 568 if (unlikely(len < ETH_HLEN)) 569 return -EINVAL; 570 } 571 |
477 if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) { 478 tun->dev->stats.rx_dropped++; 479 return -ENOMEM; | 572 skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock); 573 if (IS_ERR(skb)) { 574 if (PTR_ERR(skb) != -EAGAIN) 575 tun->dev->stats.rx_dropped++; 576 return PTR_ERR(skb); |
480 } 481 482 if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) { 483 tun->dev->stats.rx_dropped++; 484 kfree_skb(skb); 485 return -EFAULT; 486 } 487 --- 69 unchanged lines hidden (view full) --- 557 tun->dev->stats.rx_bytes += len; 558 559 return count; 560} 561 562static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, 563 unsigned long count, loff_t pos) 564{ | 577 } 578 579 if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) { 580 tun->dev->stats.rx_dropped++; 581 kfree_skb(skb); 582 return -EFAULT; 583 } 584 --- 69 unchanged lines hidden (view full) --- 654 tun->dev->stats.rx_bytes += len; 655 656 return count; 657} 658 659static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, 660 unsigned long count, loff_t pos) 661{ |
565 struct tun_struct *tun = iocb->ki_filp->private_data; | 662 struct file *file = iocb->ki_filp; 663 struct tun_struct *tun = tun_get(file); 664 ssize_t result; |
566 567 if (!tun) 568 return -EBADFD; 569 570 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count); 571 | 665 666 if (!tun) 667 return -EBADFD; 668 669 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count); 670 |
572 return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count)); | 671 result = tun_get_user(tun, (struct iovec *)iv, iov_length(iv, count), 672 file->f_flags & O_NONBLOCK); 673 674 tun_put(tun); 675 return result; |
573} 574 575/* Put packet to the user space buffer */ 576static __inline__ ssize_t tun_put_user(struct tun_struct *tun, 577 struct sk_buff *skb, 578 struct iovec *iv, int len) 579{ 580 struct tun_pi pi = { 0, skb->protocol }; --- 56 unchanged lines hidden (view full) --- 637 638 return total; 639} 640 641static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, 642 unsigned long count, loff_t pos) 643{ 644 struct file *file = iocb->ki_filp; | 676} 677 678/* Put packet to the user space buffer */ 679static __inline__ ssize_t tun_put_user(struct tun_struct *tun, 680 struct sk_buff *skb, 681 struct iovec *iv, int len) 682{ 683 struct tun_pi pi = { 0, skb->protocol }; --- 56 unchanged lines hidden (view full) --- 740 741 return total; 742} 743 744static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, 745 unsigned long count, loff_t pos) 746{ 747 struct file *file = iocb->ki_filp; |
645 struct tun_struct *tun = file->private_data; | 748 struct tun_file *tfile = file->private_data; 749 struct tun_struct *tun = __tun_get(tfile); |
646 DECLARE_WAITQUEUE(wait, current); 647 struct sk_buff *skb; 648 ssize_t len, ret = 0; 649 650 if (!tun) 651 return -EBADFD; 652 653 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name); 654 655 len = iov_length(iv, count); | 750 DECLARE_WAITQUEUE(wait, current); 751 struct sk_buff *skb; 752 ssize_t len, ret = 0; 753 754 if (!tun) 755 return -EBADFD; 756 757 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name); 758 759 len = iov_length(iv, count); |
656 if (len < 0) 657 return -EINVAL; | 760 if (len < 0) { 761 ret = -EINVAL; 762 goto out; 763 } |
658 | 764 |
659 add_wait_queue(&tun->read_wait, &wait); | 765 add_wait_queue(&tfile->read_wait, &wait); |
660 while (len) { 661 current->state = TASK_INTERRUPTIBLE; 662 663 /* Read frames from the queue */ 664 if (!(skb=skb_dequeue(&tun->readq))) { 665 if (file->f_flags & O_NONBLOCK) { 666 ret = -EAGAIN; 667 break; 668 } 669 if (signal_pending(current)) { 670 ret = -ERESTARTSYS; 671 break; 672 } | 766 while (len) { 767 current->state = TASK_INTERRUPTIBLE; 768 769 /* Read frames from the queue */ 770 if (!(skb=skb_dequeue(&tun->readq))) { 771 if (file->f_flags & O_NONBLOCK) { 772 ret = -EAGAIN; 773 break; 774 } 775 if (signal_pending(current)) { 776 ret = -ERESTARTSYS; 777 break; 778 } |
779 if (tun->dev->reg_state != NETREG_REGISTERED) { 780 ret = -EIO; 781 break; 782 } |
|
673 674 /* Nothing to read, let's sleep */ 675 schedule(); 676 continue; 677 } 678 netif_wake_queue(tun->dev); 679 680 ret = tun_put_user(tun, skb, (struct iovec *) iv, len); 681 kfree_skb(skb); 682 break; 683 } 684 685 current->state = TASK_RUNNING; | 783 784 /* Nothing to read, let's sleep */ 785 schedule(); 786 continue; 787 } 788 netif_wake_queue(tun->dev); 789 790 ret = tun_put_user(tun, skb, (struct iovec *) iv, len); 791 kfree_skb(skb); 792 break; 793 } 794 795 current->state = TASK_RUNNING; |
686 remove_wait_queue(&tun->read_wait, &wait); | 796 remove_wait_queue(&tfile->read_wait, &wait); |
687 | 797 |
798out: 799 tun_put(tun); |
|
688 return ret; 689} 690 691static void tun_setup(struct net_device *dev) 692{ 693 struct tun_struct *tun = netdev_priv(dev); 694 695 skb_queue_head_init(&tun->readq); | 800 return ret; 801} 802 803static void tun_setup(struct net_device *dev) 804{ 805 struct tun_struct *tun = netdev_priv(dev); 806 807 skb_queue_head_init(&tun->readq); |
696 init_waitqueue_head(&tun->read_wait); | |
697 698 tun->owner = -1; 699 tun->group = -1; 700 701 dev->ethtool_ops = &tun_ethtool_ops; 702 dev->destructor = free_netdev; | 808 809 tun->owner = -1; 810 tun->group = -1; 811 812 dev->ethtool_ops = &tun_ethtool_ops; 813 dev->destructor = free_netdev; |
703 dev->features |= NETIF_F_NETNS_LOCAL; | |
704} 705 | 814} 815 |
706static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name) | 816/* Trivial set of netlink ops to allow deleting tun or tap 817 * device with netlink. 818 */ 819static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) |
707{ | 820{ |
821 return -EINVAL; 822} 823 824static struct rtnl_link_ops tun_link_ops __read_mostly = { 825 .kind = DRV_NAME, 826 .priv_size = sizeof(struct tun_struct), 827 .setup = tun_setup, 828 .validate = tun_validate, 829}; 830 831static void tun_sock_write_space(struct sock *sk) 832{ |
|
708 struct tun_struct *tun; 709 | 833 struct tun_struct *tun; 834 |
710 ASSERT_RTNL(); 711 list_for_each_entry(tun, &tn->dev_list, list) { 712 if (!strncmp(tun->dev->name, name, IFNAMSIZ)) 713 return tun; 714 } | 835 if (!sock_writeable(sk)) 836 return; |
715 | 837 |
716 return NULL; | 838 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) 839 wake_up_interruptible_sync(sk->sk_sleep); 840 841 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) 842 return; 843 844 tun = container_of(sk, struct tun_sock, sk)->tun; 845 kill_fasync(&tun->fasync, SIGIO, POLL_OUT); |
717} 718 | 846} 847 |
848static void tun_sock_destruct(struct sock *sk) 849{ 850 dev_put(container_of(sk, struct tun_sock, sk)->tun->dev); 851} 852 853static struct proto tun_proto = { 854 .name = "tun", 855 .owner = THIS_MODULE, 856 .obj_size = sizeof(struct tun_sock), 857}; 858 |
|
719static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 720{ | 859static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 860{ |
721 struct tun_net *tn; | 861 struct sock *sk; |
722 struct tun_struct *tun; 723 struct net_device *dev; | 862 struct tun_struct *tun; 863 struct net_device *dev; |
724 const struct cred *cred = current_cred(); | 864 struct tun_file *tfile = file->private_data; |
725 int err; 726 | 865 int err; 866 |
727 tn = net_generic(net, tun_net_id); 728 tun = tun_get_by_name(tn, ifr->ifr_name); 729 if (tun) { 730 if (tun->attached) 731 return -EBUSY; | 867 dev = __dev_get_by_name(net, ifr->ifr_name); 868 if (dev) { 869 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 870 tun = netdev_priv(dev); 871 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 872 tun = netdev_priv(dev); 873 else 874 return -EINVAL; |
732 | 875 |
733 /* Check permissions */ 734 if (((tun->owner != -1 && 735 cred->euid != tun->owner) || 736 (tun->group != -1 && 737 cred->egid != tun->group)) && 738 !capable(CAP_NET_ADMIN)) { 739 return -EPERM; 740 } | 876 err = tun_attach(tun, file); 877 if (err < 0) 878 return err; |
741 } | 879 } |
742 else if (__dev_get_by_name(net, ifr->ifr_name)) 743 return -EINVAL; | |
744 else { 745 char *name; 746 unsigned long flags = 0; 747 748 err = -EINVAL; 749 750 if (!capable(CAP_NET_ADMIN)) 751 return -EPERM; --- 14 unchanged lines hidden (view full) --- 766 name = ifr->ifr_name; 767 768 dev = alloc_netdev(sizeof(struct tun_struct), name, 769 tun_setup); 770 if (!dev) 771 return -ENOMEM; 772 773 dev_net_set(dev, net); | 880 else { 881 char *name; 882 unsigned long flags = 0; 883 884 err = -EINVAL; 885 886 if (!capable(CAP_NET_ADMIN)) 887 return -EPERM; --- 14 unchanged lines hidden (view full) --- 902 name = ifr->ifr_name; 903 904 dev = alloc_netdev(sizeof(struct tun_struct), name, 905 tun_setup); 906 if (!dev) 907 return -ENOMEM; 908 909 dev_net_set(dev, net); |
910 dev->rtnl_link_ops = &tun_link_ops; |
|
774 775 tun = netdev_priv(dev); 776 tun->dev = dev; 777 tun->flags = flags; 778 tun->txflt.count = 0; 779 | 911 912 tun = netdev_priv(dev); 913 tun->dev = dev; 914 tun->flags = flags; 915 tun->txflt.count = 0; 916 |
917 err = -ENOMEM; 918 sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto); 919 if (!sk) 920 goto err_free_dev; 921 922 /* This ref count is for tun->sk. */ 923 dev_hold(dev); 924 sock_init_data(&tun->socket, sk); 925 sk->sk_write_space = tun_sock_write_space; 926 sk->sk_destruct = tun_sock_destruct; 927 sk->sk_sndbuf = INT_MAX; 928 sk->sk_sleep = &tfile->read_wait; 929 930 tun->sk = sk; 931 container_of(sk, struct tun_sock, sk)->tun = tun; 932 |
|
780 tun_net_init(dev); 781 782 if (strchr(dev->name, '%')) { 783 err = dev_alloc_name(dev, dev->name); 784 if (err < 0) | 933 tun_net_init(dev); 934 935 if (strchr(dev->name, '%')) { 936 err = dev_alloc_name(dev, dev->name); 937 if (err < 0) |
785 goto err_free_dev; | 938 goto err_free_sk; |
786 } 787 | 939 } 940 |
941 err = -EINVAL; |
|
788 err = register_netdevice(tun->dev); 789 if (err < 0) 790 goto err_free_dev; 791 | 942 err = register_netdevice(tun->dev); 943 if (err < 0) 944 goto err_free_dev; 945 |
792 list_add(&tun->list, &tn->dev_list); | 946 err = tun_attach(tun, file); 947 if (err < 0) 948 goto err_free_dev; |
793 } 794 795 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); 796 797 if (ifr->ifr_flags & IFF_NO_PI) 798 tun->flags |= TUN_NO_PI; 799 else 800 tun->flags &= ~TUN_NO_PI; 801 802 if (ifr->ifr_flags & IFF_ONE_QUEUE) 803 tun->flags |= TUN_ONE_QUEUE; 804 else 805 tun->flags &= ~TUN_ONE_QUEUE; 806 807 if (ifr->ifr_flags & IFF_VNET_HDR) 808 tun->flags |= TUN_VNET_HDR; 809 else 810 tun->flags &= ~TUN_VNET_HDR; 811 | 949 } 950 951 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); 952 953 if (ifr->ifr_flags & IFF_NO_PI) 954 tun->flags |= TUN_NO_PI; 955 else 956 tun->flags &= ~TUN_NO_PI; 957 958 if (ifr->ifr_flags & IFF_ONE_QUEUE) 959 tun->flags |= TUN_ONE_QUEUE; 960 else 961 tun->flags &= ~TUN_ONE_QUEUE; 962 963 if (ifr->ifr_flags & IFF_VNET_HDR) 964 tun->flags |= TUN_VNET_HDR; 965 else 966 tun->flags &= ~TUN_VNET_HDR; 967 |
812 file->private_data = tun; 813 tun->attached = 1; 814 get_net(dev_net(tun->dev)); 815 | |
816 /* Make sure persistent devices do not get stuck in 817 * xoff state. 818 */ 819 if (netif_running(tun->dev)) 820 netif_wake_queue(tun->dev); 821 822 strcpy(ifr->ifr_name, tun->dev->name); 823 return 0; 824 | 968 /* Make sure persistent devices do not get stuck in 969 * xoff state. 970 */ 971 if (netif_running(tun->dev)) 972 netif_wake_queue(tun->dev); 973 974 strcpy(ifr->ifr_name, tun->dev->name); 975 return 0; 976 |
977 err_free_sk: 978 sock_put(sk); |
|
825 err_free_dev: 826 free_netdev(dev); 827 failed: 828 return err; 829} 830 831static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr) 832{ | 979 err_free_dev: 980 free_netdev(dev); 981 failed: 982 return err; 983} 984 985static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr) 986{ |
833 struct tun_struct *tun = file->private_data; | 987 struct tun_struct *tun = tun_get(file); |
834 835 if (!tun) 836 return -EBADFD; 837 838 DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name); 839 840 strcpy(ifr->ifr_name, tun->dev->name); 841 --- 8 unchanged lines hidden (view full) --- 850 ifr->ifr_flags |= IFF_NO_PI; 851 852 if (tun->flags & TUN_ONE_QUEUE) 853 ifr->ifr_flags |= IFF_ONE_QUEUE; 854 855 if (tun->flags & TUN_VNET_HDR) 856 ifr->ifr_flags |= IFF_VNET_HDR; 857 | 988 989 if (!tun) 990 return -EBADFD; 991 992 DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name); 993 994 strcpy(ifr->ifr_name, tun->dev->name); 995 --- 8 unchanged lines hidden (view full) --- 1004 ifr->ifr_flags |= IFF_NO_PI; 1005 1006 if (tun->flags & TUN_ONE_QUEUE) 1007 ifr->ifr_flags |= IFF_ONE_QUEUE; 1008 1009 if (tun->flags & TUN_VNET_HDR) 1010 ifr->ifr_flags |= IFF_VNET_HDR; 1011 |
1012 tun_put(tun); |
|
858 return 0; 859} 860 861/* This is like a cut-down ethtool ops, except done via tun fd so no 862 * privs required. */ 863static int set_offload(struct net_device *dev, unsigned long arg) 864{ 865 unsigned int old_features, features; --- 30 unchanged lines hidden (view full) --- 896 netdev_features_change(dev); 897 898 return 0; 899} 900 901static int tun_chr_ioctl(struct inode *inode, struct file *file, 902 unsigned int cmd, unsigned long arg) 903{ | 1013 return 0; 1014} 1015 1016/* This is like a cut-down ethtool ops, except done via tun fd so no 1017 * privs required. */ 1018static int set_offload(struct net_device *dev, unsigned long arg) 1019{ 1020 unsigned int old_features, features; --- 30 unchanged lines hidden (view full) --- 1051 netdev_features_change(dev); 1052 1053 return 0; 1054} 1055 1056static int tun_chr_ioctl(struct inode *inode, struct file *file, 1057 unsigned int cmd, unsigned long arg) 1058{ |
904 struct tun_struct *tun = file->private_data; | 1059 struct tun_file *tfile = file->private_data; 1060 struct tun_struct *tun; |
905 void __user* argp = (void __user*)arg; 906 struct ifreq ifr; | 1061 void __user* argp = (void __user*)arg; 1062 struct ifreq ifr; |
1063 int sndbuf; |
|
907 int ret; 908 909 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) 910 if (copy_from_user(&ifr, argp, sizeof ifr)) 911 return -EFAULT; 912 | 1064 int ret; 1065 1066 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) 1067 if (copy_from_user(&ifr, argp, sizeof ifr)) 1068 return -EFAULT; 1069 |
1070 if (cmd == TUNGETFEATURES) { 1071 /* Currently this just means: "what IFF flags are valid?". 1072 * This is needed because we never checked for invalid flags on 1073 * TUNSETIFF. */ 1074 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | 1075 IFF_VNET_HDR, 1076 (unsigned int __user*)argp); 1077 } 1078 1079 tun = __tun_get(tfile); |
|
913 if (cmd == TUNSETIFF && !tun) { 914 int err; 915 916 ifr.ifr_name[IFNAMSIZ-1] = '\0'; 917 918 rtnl_lock(); | 1080 if (cmd == TUNSETIFF && !tun) { 1081 int err; 1082 1083 ifr.ifr_name[IFNAMSIZ-1] = '\0'; 1084 1085 rtnl_lock(); |
919 err = tun_set_iff(current->nsproxy->net_ns, file, &ifr); | 1086 err = tun_set_iff(tfile->net, file, &ifr); |
920 rtnl_unlock(); 921 922 if (err) 923 return err; 924 925 if (copy_to_user(argp, &ifr, sizeof(ifr))) 926 return -EFAULT; 927 return 0; 928 } 929 | 1087 rtnl_unlock(); 1088 1089 if (err) 1090 return err; 1091 1092 if (copy_to_user(argp, &ifr, sizeof(ifr))) 1093 return -EFAULT; 1094 return 0; 1095 } 1096 |
930 if (cmd == TUNGETFEATURES) { 931 /* Currently this just means: "what IFF flags are valid?". 932 * This is needed because we never checked for invalid flags on 933 * TUNSETIFF. */ 934 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | 935 IFF_VNET_HDR, 936 (unsigned int __user*)argp); 937 } | |
938 939 if (!tun) 940 return -EBADFD; 941 942 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd); 943 | 1097 1098 if (!tun) 1099 return -EBADFD; 1100 1101 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd); 1102 |
1103 ret = 0; |
|
944 switch (cmd) { 945 case TUNGETIFF: 946 ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr); 947 if (ret) | 1104 switch (cmd) { 1105 case TUNGETIFF: 1106 ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr); 1107 if (ret) |
948 return ret; | 1108 break; |
949 950 if (copy_to_user(argp, &ifr, sizeof(ifr))) | 1109 1110 if (copy_to_user(argp, &ifr, sizeof(ifr))) |
951 return -EFAULT; | 1111 ret = -EFAULT; |
952 break; 953 954 case TUNSETNOCSUM: 955 /* Disable/Enable checksum */ 956 if (arg) 957 tun->flags |= TUN_NOCHECKSUM; 958 else 959 tun->flags &= ~TUN_NOCHECKSUM; --- 35 unchanged lines hidden (view full) --- 995 tun->dev->name); 996 ret = -EBUSY; 997 } else { 998 tun->dev->type = (int) arg; 999 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type); 1000 ret = 0; 1001 } 1002 rtnl_unlock(); | 1112 break; 1113 1114 case TUNSETNOCSUM: 1115 /* Disable/Enable checksum */ 1116 if (arg) 1117 tun->flags |= TUN_NOCHECKSUM; 1118 else 1119 tun->flags &= ~TUN_NOCHECKSUM; --- 35 unchanged lines hidden (view full) --- 1155 tun->dev->name); 1156 ret = -EBUSY; 1157 } else { 1158 tun->dev->type = (int) arg; 1159 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type); 1160 ret = 0; 1161 } 1162 rtnl_unlock(); |
1003 return ret; | 1163 break; |
1004 1005#ifdef TUN_DEBUG 1006 case TUNSETDEBUG: 1007 tun->debug = arg; 1008 break; 1009#endif 1010 case TUNSETOFFLOAD: 1011 rtnl_lock(); 1012 ret = set_offload(tun->dev, arg); 1013 rtnl_unlock(); | 1164 1165#ifdef TUN_DEBUG 1166 case TUNSETDEBUG: 1167 tun->debug = arg; 1168 break; 1169#endif 1170 case TUNSETOFFLOAD: 1171 rtnl_lock(); 1172 ret = set_offload(tun->dev, arg); 1173 rtnl_unlock(); |
1014 return ret; | 1174 break; |
1015 1016 case TUNSETTXFILTER: 1017 /* Can be set only for TAPs */ | 1175 1176 case TUNSETTXFILTER: 1177 /* Can be set only for TAPs */ |
1178 ret = -EINVAL; |
|
1018 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) | 1179 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
1019 return -EINVAL; | 1180 break; |
1020 rtnl_lock(); 1021 ret = update_filter(&tun->txflt, (void __user *)arg); 1022 rtnl_unlock(); | 1181 rtnl_lock(); 1182 ret = update_filter(&tun->txflt, (void __user *)arg); 1183 rtnl_unlock(); |
1023 return ret; | 1184 break; |
1024 1025 case SIOCGIFHWADDR: 1026 /* Get hw addres */ 1027 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 1028 ifr.ifr_hwaddr.sa_family = tun->dev->type; 1029 if (copy_to_user(argp, &ifr, sizeof ifr)) | 1185 1186 case SIOCGIFHWADDR: 1187 /* Get hw addres */ 1188 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 1189 ifr.ifr_hwaddr.sa_family = tun->dev->type; 1190 if (copy_to_user(argp, &ifr, sizeof ifr)) |
1030 return -EFAULT; 1031 return 0; | 1191 ret = -EFAULT; 1192 break; |
1032 1033 case SIOCSIFHWADDR: 1034 /* Set hw address */ 1035 DBG(KERN_DEBUG "%s: set hw address: %pM\n", 1036 tun->dev->name, ifr.ifr_hwaddr.sa_data); 1037 1038 rtnl_lock(); 1039 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 1040 rtnl_unlock(); | 1193 1194 case SIOCSIFHWADDR: 1195 /* Set hw address */ 1196 DBG(KERN_DEBUG "%s: set hw address: %pM\n", 1197 tun->dev->name, ifr.ifr_hwaddr.sa_data); 1198 1199 rtnl_lock(); 1200 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 1201 rtnl_unlock(); |
1041 return ret; | 1202 break; |
1042 | 1203 |
1204 case TUNGETSNDBUF: 1205 sndbuf = tun->sk->sk_sndbuf; 1206 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 1207 ret = -EFAULT; 1208 break; 1209 1210 case TUNSETSNDBUF: 1211 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 1212 ret = -EFAULT; 1213 break; 1214 } 1215 1216 tun->sk->sk_sndbuf = sndbuf; 1217 break; 1218 |
|
1043 default: | 1219 default: |
1044 return -EINVAL; | 1220 ret = -EINVAL; 1221 break; |
1045 }; 1046 | 1222 }; 1223 |
1047 return 0; | 1224 tun_put(tun); 1225 return ret; |
1048} 1049 1050static int tun_chr_fasync(int fd, struct file *file, int on) 1051{ | 1226} 1227 1228static int tun_chr_fasync(int fd, struct file *file, int on) 1229{ |
1052 struct tun_struct *tun = file->private_data; | 1230 struct tun_struct *tun = tun_get(file); |
1053 int ret; 1054 1055 if (!tun) 1056 return -EBADFD; 1057 1058 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on); 1059 1060 lock_kernel(); --- 5 unchanged lines hidden (view full) --- 1066 if (ret) 1067 goto out; 1068 tun->flags |= TUN_FASYNC; 1069 } else 1070 tun->flags &= ~TUN_FASYNC; 1071 ret = 0; 1072out: 1073 unlock_kernel(); | 1231 int ret; 1232 1233 if (!tun) 1234 return -EBADFD; 1235 1236 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on); 1237 1238 lock_kernel(); --- 5 unchanged lines hidden (view full) --- 1244 if (ret) 1245 goto out; 1246 tun->flags |= TUN_FASYNC; 1247 } else 1248 tun->flags &= ~TUN_FASYNC; 1249 ret = 0; 1250out: 1251 unlock_kernel(); |
1252 tun_put(tun); |
|
1074 return ret; 1075} 1076 1077static int tun_chr_open(struct inode *inode, struct file * file) 1078{ | 1253 return ret; 1254} 1255 1256static int tun_chr_open(struct inode *inode, struct file * file) 1257{ |
1258 struct tun_file *tfile; |
|
1079 cycle_kernel_lock(); 1080 DBG1(KERN_INFO "tunX: tun_chr_open\n"); | 1259 cycle_kernel_lock(); 1260 DBG1(KERN_INFO "tunX: tun_chr_open\n"); |
1081 file->private_data = NULL; | 1261 1262 tfile = kmalloc(sizeof(*tfile), GFP_KERNEL); 1263 if (!tfile) 1264 return -ENOMEM; 1265 atomic_set(&tfile->count, 0); 1266 tfile->tun = NULL; 1267 tfile->net = get_net(current->nsproxy->net_ns); 1268 init_waitqueue_head(&tfile->read_wait); 1269 file->private_data = tfile; |
1082 return 0; 1083} 1084 1085static int tun_chr_close(struct inode *inode, struct file *file) 1086{ | 1270 return 0; 1271} 1272 1273static int tun_chr_close(struct inode *inode, struct file *file) 1274{ |
1087 struct tun_struct *tun = file->private_data; | 1275 struct tun_file *tfile = file->private_data; 1276 struct tun_struct *tun = __tun_get(tfile); |
1088 | 1277 |
1089 if (!tun) 1090 return 0; | |
1091 | 1278 |
1092 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name); | 1279 if (tun) { 1280 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name); |
1093 | 1281 |
1094 rtnl_lock(); | 1282 rtnl_lock(); 1283 __tun_detach(tun); |
1095 | 1284 |
1096 /* Detach from net device */ 1097 file->private_data = NULL; 1098 tun->attached = 0; 1099 put_net(dev_net(tun->dev)); | 1285 /* If desireable, unregister the netdevice. */ 1286 if (!(tun->flags & TUN_PERSIST)) { 1287 sock_put(tun->sk); 1288 unregister_netdevice(tun->dev); 1289 } |
1100 | 1290 |
1101 /* Drop read queue */ 1102 skb_queue_purge(&tun->readq); 1103 1104 if (!(tun->flags & TUN_PERSIST)) { 1105 list_del(&tun->list); 1106 unregister_netdevice(tun->dev); | 1291 rtnl_unlock(); |
1107 } 1108 | 1292 } 1293 |
1109 rtnl_unlock(); | 1294 put_net(tfile->net); 1295 kfree(tfile); |
1110 1111 return 0; 1112} 1113 1114static const struct file_operations tun_fops = { 1115 .owner = THIS_MODULE, 1116 .llseek = no_llseek, 1117 .read = do_sync_read, --- 64 unchanged lines hidden (view full) --- 1182 struct tun_struct *tun = netdev_priv(dev); 1183 tun->debug = value; 1184#endif 1185} 1186 1187static u32 tun_get_link(struct net_device *dev) 1188{ 1189 struct tun_struct *tun = netdev_priv(dev); | 1296 1297 return 0; 1298} 1299 1300static const struct file_operations tun_fops = { 1301 .owner = THIS_MODULE, 1302 .llseek = no_llseek, 1303 .read = do_sync_read, --- 64 unchanged lines hidden (view full) --- 1368 struct tun_struct *tun = netdev_priv(dev); 1369 tun->debug = value; 1370#endif 1371} 1372 1373static u32 tun_get_link(struct net_device *dev) 1374{ 1375 struct tun_struct *tun = netdev_priv(dev); |
1190 return tun->attached; | 1376 return !!tun->tfile; |
1191} 1192 1193static u32 tun_get_rx_csum(struct net_device *dev) 1194{ 1195 struct tun_struct *tun = netdev_priv(dev); 1196 return (tun->flags & TUN_NOCHECKSUM) == 0; 1197} 1198 --- 12 unchanged lines hidden (view full) --- 1211 .get_drvinfo = tun_get_drvinfo, 1212 .get_msglevel = tun_get_msglevel, 1213 .set_msglevel = tun_set_msglevel, 1214 .get_link = tun_get_link, 1215 .get_rx_csum = tun_get_rx_csum, 1216 .set_rx_csum = tun_set_rx_csum 1217}; 1218 | 1377} 1378 1379static u32 tun_get_rx_csum(struct net_device *dev) 1380{ 1381 struct tun_struct *tun = netdev_priv(dev); 1382 return (tun->flags & TUN_NOCHECKSUM) == 0; 1383} 1384 --- 12 unchanged lines hidden (view full) --- 1397 .get_drvinfo = tun_get_drvinfo, 1398 .get_msglevel = tun_get_msglevel, 1399 .set_msglevel = tun_set_msglevel, 1400 .get_link = tun_get_link, 1401 .get_rx_csum = tun_get_rx_csum, 1402 .set_rx_csum = tun_set_rx_csum 1403}; 1404 |
1219static int tun_init_net(struct net *net) 1220{ 1221 struct tun_net *tn; | |
1222 | 1405 |
1223 tn = kmalloc(sizeof(*tn), GFP_KERNEL); 1224 if (tn == NULL) 1225 return -ENOMEM; 1226 1227 INIT_LIST_HEAD(&tn->dev_list); 1228 1229 if (net_assign_generic(net, tun_net_id, tn)) { 1230 kfree(tn); 1231 return -ENOMEM; 1232 } 1233 1234 return 0; 1235} 1236 1237static void tun_exit_net(struct net *net) 1238{ 1239 struct tun_net *tn; 1240 struct tun_struct *tun, *nxt; 1241 1242 tn = net_generic(net, tun_net_id); 1243 1244 rtnl_lock(); 1245 list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) { 1246 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name); 1247 unregister_netdevice(tun->dev); 1248 } 1249 rtnl_unlock(); 1250 1251 kfree(tn); 1252} 1253 1254static struct pernet_operations tun_net_ops = { 1255 .init = tun_init_net, 1256 .exit = tun_exit_net, 1257}; 1258 | |
1259static int __init tun_init(void) 1260{ 1261 int ret = 0; 1262 1263 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 1264 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT); 1265 | 1406static int __init tun_init(void) 1407{ 1408 int ret = 0; 1409 1410 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 1411 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT); 1412 |
1266 ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops); | 1413 ret = rtnl_link_register(&tun_link_ops); |
1267 if (ret) { | 1414 if (ret) { |
1268 printk(KERN_ERR "tun: Can't register pernet ops\n"); 1269 goto err_pernet; | 1415 printk(KERN_ERR "tun: Can't register link_ops\n"); 1416 goto err_linkops; |
1270 } 1271 1272 ret = misc_register(&tun_miscdev); 1273 if (ret) { 1274 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR); 1275 goto err_misc; 1276 } | 1417 } 1418 1419 ret = misc_register(&tun_miscdev); 1420 if (ret) { 1421 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR); 1422 goto err_misc; 1423 } |
1277 return 0; 1278 | 1424 return 0; |
1279err_misc: | 1425err_misc: |
1280 unregister_pernet_gen_device(tun_net_id, &tun_net_ops); 1281err_pernet: | 1426 rtnl_link_unregister(&tun_link_ops); 1427err_linkops: |
1282 return ret; 1283} 1284 1285static void tun_cleanup(void) 1286{ 1287 misc_deregister(&tun_miscdev); | 1428 return ret; 1429} 1430 1431static void tun_cleanup(void) 1432{ 1433 misc_deregister(&tun_miscdev); |
1288 unregister_pernet_gen_device(tun_net_id, &tun_net_ops); | 1434 rtnl_link_unregister(&tun_link_ops); |
1289} 1290 1291module_init(tun_init); 1292module_exit(tun_cleanup); 1293MODULE_DESCRIPTION(DRV_DESCRIPTION); 1294MODULE_AUTHOR(DRV_COPYRIGHT); 1295MODULE_LICENSE("GPL"); 1296MODULE_ALIAS_MISCDEV(TUN_MINOR); | 1435} 1436 1437module_init(tun_init); 1438module_exit(tun_cleanup); 1439MODULE_DESCRIPTION(DRV_DESCRIPTION); 1440MODULE_AUTHOR(DRV_COPYRIGHT); 1441MODULE_LICENSE("GPL"); 1442MODULE_ALIAS_MISCDEV(TUN_MINOR); |