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 eth_random_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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt 38 39 #define DRV_NAME "tun" 40 #define DRV_VERSION "1.6" 41 #define DRV_DESCRIPTION "Universal TUN/TAP device driver" 42 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" 43 44 #include <linux/module.h> 45 #include <linux/errno.h> 46 #include <linux/kernel.h> 47 #include <linux/sched/signal.h> 48 #include <linux/major.h> 49 #include <linux/slab.h> 50 #include <linux/poll.h> 51 #include <linux/fcntl.h> 52 #include <linux/init.h> 53 #include <linux/skbuff.h> 54 #include <linux/netdevice.h> 55 #include <linux/etherdevice.h> 56 #include <linux/miscdevice.h> 57 #include <linux/ethtool.h> 58 #include <linux/rtnetlink.h> 59 #include <linux/compat.h> 60 #include <linux/if.h> 61 #include <linux/if_arp.h> 62 #include <linux/if_ether.h> 63 #include <linux/if_tun.h> 64 #include <linux/if_vlan.h> 65 #include <linux/crc32.h> 66 #include <linux/nsproxy.h> 67 #include <linux/virtio_net.h> 68 #include <linux/rcupdate.h> 69 #include <net/net_namespace.h> 70 #include <net/netns/generic.h> 71 #include <net/rtnetlink.h> 72 #include <net/sock.h> 73 #include <linux/seq_file.h> 74 #include <linux/uio.h> 75 #include <linux/skb_array.h> 76 #include <linux/bpf.h> 77 #include <linux/bpf_trace.h> 78 79 #include <linux/uaccess.h> 80 81 /* Uncomment to enable debugging */ 82 /* #define TUN_DEBUG 1 */ 83 84 #ifdef TUN_DEBUG 85 static int debug; 86 87 #define tun_debug(level, tun, fmt, args...) \ 88 do { \ 89 if (tun->debug) \ 90 netdev_printk(level, tun->dev, fmt, ##args); \ 91 } while (0) 92 #define DBG1(level, fmt, args...) \ 93 do { \ 94 if (debug == 2) \ 95 printk(level fmt, ##args); \ 96 } while (0) 97 #else 98 #define tun_debug(level, tun, fmt, args...) \ 99 do { \ 100 if (0) \ 101 netdev_printk(level, tun->dev, fmt, ##args); \ 102 } while (0) 103 #define DBG1(level, fmt, args...) \ 104 do { \ 105 if (0) \ 106 printk(level fmt, ##args); \ 107 } while (0) 108 #endif 109 110 #define TUN_HEADROOM 256 111 #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) 112 113 /* TUN device flags */ 114 115 /* IFF_ATTACH_QUEUE is never stored in device flags, 116 * overload it to mean fasync when stored there. 117 */ 118 #define TUN_FASYNC IFF_ATTACH_QUEUE 119 /* High bits in flags field are unused. */ 120 #define TUN_VNET_LE 0x80000000 121 #define TUN_VNET_BE 0x40000000 122 123 #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ 124 IFF_MULTI_QUEUE | IFF_NAPI) 125 #define GOODCOPY_LEN 128 126 127 #define FLT_EXACT_COUNT 8 128 struct tap_filter { 129 unsigned int count; /* Number of addrs. Zero means disabled */ 130 u32 mask[2]; /* Mask of the hashed addrs */ 131 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; 132 }; 133 134 /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal 135 * to max number of VCPUs in guest. */ 136 #define MAX_TAP_QUEUES 256 137 #define MAX_TAP_FLOWS 4096 138 139 #define TUN_FLOW_EXPIRE (3 * HZ) 140 141 struct tun_pcpu_stats { 142 u64 rx_packets; 143 u64 rx_bytes; 144 u64 tx_packets; 145 u64 tx_bytes; 146 struct u64_stats_sync syncp; 147 u32 rx_dropped; 148 u32 tx_dropped; 149 u32 rx_frame_errors; 150 }; 151 152 /* A tun_file connects an open character device to a tuntap netdevice. It 153 * also contains all socket related structures (except sock_fprog and tap_filter) 154 * to serve as one transmit queue for tuntap device. The sock_fprog and 155 * tap_filter were kept in tun_struct since they were used for filtering for the 156 * netdevice not for a specific queue (at least I didn't see the requirement for 157 * this). 158 * 159 * RCU usage: 160 * The tun_file and tun_struct are loosely coupled, the pointer from one to the 161 * other can only be read while rcu_read_lock or rtnl_lock is held. 162 */ 163 struct tun_file { 164 struct sock sk; 165 struct socket socket; 166 struct socket_wq wq; 167 struct tun_struct __rcu *tun; 168 struct fasync_struct *fasync; 169 /* only used for fasnyc */ 170 unsigned int flags; 171 union { 172 u16 queue_index; 173 unsigned int ifindex; 174 }; 175 struct napi_struct napi; 176 struct list_head next; 177 struct tun_struct *detached; 178 struct skb_array tx_array; 179 }; 180 181 struct tun_flow_entry { 182 struct hlist_node hash_link; 183 struct rcu_head rcu; 184 struct tun_struct *tun; 185 186 u32 rxhash; 187 u32 rps_rxhash; 188 int queue_index; 189 unsigned long updated; 190 }; 191 192 #define TUN_NUM_FLOW_ENTRIES 1024 193 194 /* Since the socket were moved to tun_file, to preserve the behavior of persist 195 * device, socket filter, sndbuf and vnet header size were restore when the 196 * file were attached to a persist device. 197 */ 198 struct tun_struct { 199 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; 200 unsigned int numqueues; 201 unsigned int flags; 202 kuid_t owner; 203 kgid_t group; 204 205 struct net_device *dev; 206 netdev_features_t set_features; 207 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ 208 NETIF_F_TSO6) 209 210 int align; 211 int vnet_hdr_sz; 212 int sndbuf; 213 struct tap_filter txflt; 214 struct sock_fprog fprog; 215 /* protected by rtnl lock */ 216 bool filter_attached; 217 #ifdef TUN_DEBUG 218 int debug; 219 #endif 220 spinlock_t lock; 221 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; 222 struct timer_list flow_gc_timer; 223 unsigned long ageing_time; 224 unsigned int numdisabled; 225 struct list_head disabled; 226 void *security; 227 u32 flow_count; 228 u32 rx_batched; 229 struct tun_pcpu_stats __percpu *pcpu_stats; 230 struct bpf_prog __rcu *xdp_prog; 231 }; 232 233 static int tun_napi_receive(struct napi_struct *napi, int budget) 234 { 235 struct tun_file *tfile = container_of(napi, struct tun_file, napi); 236 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 237 struct sk_buff_head process_queue; 238 struct sk_buff *skb; 239 int received = 0; 240 241 __skb_queue_head_init(&process_queue); 242 243 spin_lock(&queue->lock); 244 skb_queue_splice_tail_init(queue, &process_queue); 245 spin_unlock(&queue->lock); 246 247 while (received < budget && (skb = __skb_dequeue(&process_queue))) { 248 napi_gro_receive(napi, skb); 249 ++received; 250 } 251 252 if (!skb_queue_empty(&process_queue)) { 253 spin_lock(&queue->lock); 254 skb_queue_splice(&process_queue, queue); 255 spin_unlock(&queue->lock); 256 } 257 258 return received; 259 } 260 261 static int tun_napi_poll(struct napi_struct *napi, int budget) 262 { 263 unsigned int received; 264 265 received = tun_napi_receive(napi, budget); 266 267 if (received < budget) 268 napi_complete_done(napi, received); 269 270 return received; 271 } 272 273 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, 274 bool napi_en) 275 { 276 if (napi_en) { 277 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll, 278 NAPI_POLL_WEIGHT); 279 napi_enable(&tfile->napi); 280 } 281 } 282 283 static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile) 284 { 285 if (tun->flags & IFF_NAPI) 286 napi_disable(&tfile->napi); 287 } 288 289 static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile) 290 { 291 if (tun->flags & IFF_NAPI) 292 netif_napi_del(&tfile->napi); 293 } 294 295 #ifdef CONFIG_TUN_VNET_CROSS_LE 296 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 297 { 298 return tun->flags & TUN_VNET_BE ? false : 299 virtio_legacy_is_little_endian(); 300 } 301 302 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 303 { 304 int be = !!(tun->flags & TUN_VNET_BE); 305 306 if (put_user(be, argp)) 307 return -EFAULT; 308 309 return 0; 310 } 311 312 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 313 { 314 int be; 315 316 if (get_user(be, argp)) 317 return -EFAULT; 318 319 if (be) 320 tun->flags |= TUN_VNET_BE; 321 else 322 tun->flags &= ~TUN_VNET_BE; 323 324 return 0; 325 } 326 #else 327 static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) 328 { 329 return virtio_legacy_is_little_endian(); 330 } 331 332 static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) 333 { 334 return -EINVAL; 335 } 336 337 static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) 338 { 339 return -EINVAL; 340 } 341 #endif /* CONFIG_TUN_VNET_CROSS_LE */ 342 343 static inline bool tun_is_little_endian(struct tun_struct *tun) 344 { 345 return tun->flags & TUN_VNET_LE || 346 tun_legacy_is_little_endian(tun); 347 } 348 349 static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) 350 { 351 return __virtio16_to_cpu(tun_is_little_endian(tun), val); 352 } 353 354 static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) 355 { 356 return __cpu_to_virtio16(tun_is_little_endian(tun), val); 357 } 358 359 static inline u32 tun_hashfn(u32 rxhash) 360 { 361 return rxhash & 0x3ff; 362 } 363 364 static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) 365 { 366 struct tun_flow_entry *e; 367 368 hlist_for_each_entry_rcu(e, head, hash_link) { 369 if (e->rxhash == rxhash) 370 return e; 371 } 372 return NULL; 373 } 374 375 static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, 376 struct hlist_head *head, 377 u32 rxhash, u16 queue_index) 378 { 379 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); 380 381 if (e) { 382 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", 383 rxhash, queue_index); 384 e->updated = jiffies; 385 e->rxhash = rxhash; 386 e->rps_rxhash = 0; 387 e->queue_index = queue_index; 388 e->tun = tun; 389 hlist_add_head_rcu(&e->hash_link, head); 390 ++tun->flow_count; 391 } 392 return e; 393 } 394 395 static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) 396 { 397 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", 398 e->rxhash, e->queue_index); 399 hlist_del_rcu(&e->hash_link); 400 kfree_rcu(e, rcu); 401 --tun->flow_count; 402 } 403 404 static void tun_flow_flush(struct tun_struct *tun) 405 { 406 int i; 407 408 spin_lock_bh(&tun->lock); 409 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 410 struct tun_flow_entry *e; 411 struct hlist_node *n; 412 413 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) 414 tun_flow_delete(tun, e); 415 } 416 spin_unlock_bh(&tun->lock); 417 } 418 419 static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) 420 { 421 int i; 422 423 spin_lock_bh(&tun->lock); 424 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 425 struct tun_flow_entry *e; 426 struct hlist_node *n; 427 428 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 429 if (e->queue_index == queue_index) 430 tun_flow_delete(tun, e); 431 } 432 } 433 spin_unlock_bh(&tun->lock); 434 } 435 436 static void tun_flow_cleanup(unsigned long data) 437 { 438 struct tun_struct *tun = (struct tun_struct *)data; 439 unsigned long delay = tun->ageing_time; 440 unsigned long next_timer = jiffies + delay; 441 unsigned long count = 0; 442 int i; 443 444 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); 445 446 spin_lock_bh(&tun->lock); 447 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { 448 struct tun_flow_entry *e; 449 struct hlist_node *n; 450 451 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { 452 unsigned long this_timer; 453 count++; 454 this_timer = e->updated + delay; 455 if (time_before_eq(this_timer, jiffies)) 456 tun_flow_delete(tun, e); 457 else if (time_before(this_timer, next_timer)) 458 next_timer = this_timer; 459 } 460 } 461 462 if (count) 463 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); 464 spin_unlock_bh(&tun->lock); 465 } 466 467 static void tun_flow_update(struct tun_struct *tun, u32 rxhash, 468 struct tun_file *tfile) 469 { 470 struct hlist_head *head; 471 struct tun_flow_entry *e; 472 unsigned long delay = tun->ageing_time; 473 u16 queue_index = tfile->queue_index; 474 475 if (!rxhash) 476 return; 477 else 478 head = &tun->flows[tun_hashfn(rxhash)]; 479 480 rcu_read_lock(); 481 482 /* We may get a very small possibility of OOO during switching, not 483 * worth to optimize.*/ 484 if (tun->numqueues == 1 || tfile->detached) 485 goto unlock; 486 487 e = tun_flow_find(head, rxhash); 488 if (likely(e)) { 489 /* TODO: keep queueing to old queue until it's empty? */ 490 e->queue_index = queue_index; 491 e->updated = jiffies; 492 sock_rps_record_flow_hash(e->rps_rxhash); 493 } else { 494 spin_lock_bh(&tun->lock); 495 if (!tun_flow_find(head, rxhash) && 496 tun->flow_count < MAX_TAP_FLOWS) 497 tun_flow_create(tun, head, rxhash, queue_index); 498 499 if (!timer_pending(&tun->flow_gc_timer)) 500 mod_timer(&tun->flow_gc_timer, 501 round_jiffies_up(jiffies + delay)); 502 spin_unlock_bh(&tun->lock); 503 } 504 505 unlock: 506 rcu_read_unlock(); 507 } 508 509 /** 510 * Save the hash received in the stack receive path and update the 511 * flow_hash table accordingly. 512 */ 513 static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) 514 { 515 if (unlikely(e->rps_rxhash != hash)) 516 e->rps_rxhash = hash; 517 } 518 519 /* We try to identify a flow through its rxhash first. The reason that 520 * we do not check rxq no. is because some cards(e.g 82599), chooses 521 * the rxq based on the txq where the last packet of the flow comes. As 522 * the userspace application move between processors, we may get a 523 * different rxq no. here. If we could not get rxhash, then we would 524 * hope the rxq no. may help here. 525 */ 526 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, 527 void *accel_priv, select_queue_fallback_t fallback) 528 { 529 struct tun_struct *tun = netdev_priv(dev); 530 struct tun_flow_entry *e; 531 u32 txq = 0; 532 u32 numqueues = 0; 533 534 rcu_read_lock(); 535 numqueues = ACCESS_ONCE(tun->numqueues); 536 537 txq = __skb_get_hash_symmetric(skb); 538 if (txq) { 539 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); 540 if (e) { 541 tun_flow_save_rps_rxhash(e, txq); 542 txq = e->queue_index; 543 } else 544 /* use multiply and shift instead of expensive divide */ 545 txq = ((u64)txq * numqueues) >> 32; 546 } else if (likely(skb_rx_queue_recorded(skb))) { 547 txq = skb_get_rx_queue(skb); 548 while (unlikely(txq >= numqueues)) 549 txq -= numqueues; 550 } 551 552 rcu_read_unlock(); 553 return txq; 554 } 555 556 static inline bool tun_not_capable(struct tun_struct *tun) 557 { 558 const struct cred *cred = current_cred(); 559 struct net *net = dev_net(tun->dev); 560 561 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || 562 (gid_valid(tun->group) && !in_egroup_p(tun->group))) && 563 !ns_capable(net->user_ns, CAP_NET_ADMIN); 564 } 565 566 static void tun_set_real_num_queues(struct tun_struct *tun) 567 { 568 netif_set_real_num_tx_queues(tun->dev, tun->numqueues); 569 netif_set_real_num_rx_queues(tun->dev, tun->numqueues); 570 } 571 572 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) 573 { 574 tfile->detached = tun; 575 list_add_tail(&tfile->next, &tun->disabled); 576 ++tun->numdisabled; 577 } 578 579 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) 580 { 581 struct tun_struct *tun = tfile->detached; 582 583 tfile->detached = NULL; 584 list_del_init(&tfile->next); 585 --tun->numdisabled; 586 return tun; 587 } 588 589 static void tun_queue_purge(struct tun_file *tfile) 590 { 591 struct sk_buff *skb; 592 593 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) 594 kfree_skb(skb); 595 596 skb_queue_purge(&tfile->sk.sk_write_queue); 597 skb_queue_purge(&tfile->sk.sk_error_queue); 598 } 599 600 static void __tun_detach(struct tun_file *tfile, bool clean) 601 { 602 struct tun_file *ntfile; 603 struct tun_struct *tun; 604 605 tun = rtnl_dereference(tfile->tun); 606 607 if (tun && clean) { 608 tun_napi_disable(tun, tfile); 609 tun_napi_del(tun, tfile); 610 } 611 612 if (tun && !tfile->detached) { 613 u16 index = tfile->queue_index; 614 BUG_ON(index >= tun->numqueues); 615 616 rcu_assign_pointer(tun->tfiles[index], 617 tun->tfiles[tun->numqueues - 1]); 618 ntfile = rtnl_dereference(tun->tfiles[index]); 619 ntfile->queue_index = index; 620 621 --tun->numqueues; 622 if (clean) { 623 RCU_INIT_POINTER(tfile->tun, NULL); 624 sock_put(&tfile->sk); 625 } else 626 tun_disable_queue(tun, tfile); 627 628 synchronize_net(); 629 tun_flow_delete_by_queue(tun, tun->numqueues + 1); 630 /* Drop read queue */ 631 tun_queue_purge(tfile); 632 tun_set_real_num_queues(tun); 633 } else if (tfile->detached && clean) { 634 tun = tun_enable_queue(tfile); 635 sock_put(&tfile->sk); 636 } 637 638 if (clean) { 639 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { 640 netif_carrier_off(tun->dev); 641 642 if (!(tun->flags & IFF_PERSIST) && 643 tun->dev->reg_state == NETREG_REGISTERED) 644 unregister_netdevice(tun->dev); 645 } 646 if (tun) 647 skb_array_cleanup(&tfile->tx_array); 648 sock_put(&tfile->sk); 649 } 650 } 651 652 static void tun_detach(struct tun_file *tfile, bool clean) 653 { 654 rtnl_lock(); 655 __tun_detach(tfile, clean); 656 rtnl_unlock(); 657 } 658 659 static void tun_detach_all(struct net_device *dev) 660 { 661 struct tun_struct *tun = netdev_priv(dev); 662 struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog); 663 struct tun_file *tfile, *tmp; 664 int i, n = tun->numqueues; 665 666 for (i = 0; i < n; i++) { 667 tfile = rtnl_dereference(tun->tfiles[i]); 668 BUG_ON(!tfile); 669 tun_napi_disable(tun, tfile); 670 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 671 tfile->socket.sk->sk_data_ready(tfile->socket.sk); 672 RCU_INIT_POINTER(tfile->tun, NULL); 673 --tun->numqueues; 674 } 675 list_for_each_entry(tfile, &tun->disabled, next) { 676 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; 677 tfile->socket.sk->sk_data_ready(tfile->socket.sk); 678 RCU_INIT_POINTER(tfile->tun, NULL); 679 } 680 BUG_ON(tun->numqueues != 0); 681 682 synchronize_net(); 683 for (i = 0; i < n; i++) { 684 tfile = rtnl_dereference(tun->tfiles[i]); 685 tun_napi_del(tun, tfile); 686 /* Drop read queue */ 687 tun_queue_purge(tfile); 688 sock_put(&tfile->sk); 689 } 690 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { 691 tun_enable_queue(tfile); 692 tun_queue_purge(tfile); 693 sock_put(&tfile->sk); 694 } 695 BUG_ON(tun->numdisabled != 0); 696 697 if (xdp_prog) 698 bpf_prog_put(xdp_prog); 699 700 if (tun->flags & IFF_PERSIST) 701 module_put(THIS_MODULE); 702 } 703 704 static int tun_attach(struct tun_struct *tun, struct file *file, 705 bool skip_filter, bool napi) 706 { 707 struct tun_file *tfile = file->private_data; 708 struct net_device *dev = tun->dev; 709 int err; 710 711 err = security_tun_dev_attach(tfile->socket.sk, tun->security); 712 if (err < 0) 713 goto out; 714 715 err = -EINVAL; 716 if (rtnl_dereference(tfile->tun) && !tfile->detached) 717 goto out; 718 719 err = -EBUSY; 720 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) 721 goto out; 722 723 err = -E2BIG; 724 if (!tfile->detached && 725 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) 726 goto out; 727 728 err = 0; 729 730 /* Re-attach the filter to persist device */ 731 if (!skip_filter && (tun->filter_attached == true)) { 732 lock_sock(tfile->socket.sk); 733 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); 734 release_sock(tfile->socket.sk); 735 if (!err) 736 goto out; 737 } 738 739 if (!tfile->detached && 740 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { 741 err = -ENOMEM; 742 goto out; 743 } 744 745 tfile->queue_index = tun->numqueues; 746 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; 747 rcu_assign_pointer(tfile->tun, tun); 748 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); 749 tun->numqueues++; 750 751 if (tfile->detached) { 752 tun_enable_queue(tfile); 753 } else { 754 sock_hold(&tfile->sk); 755 tun_napi_init(tun, tfile, napi); 756 } 757 758 tun_set_real_num_queues(tun); 759 760 /* device is allowed to go away first, so no need to hold extra 761 * refcnt. 762 */ 763 764 out: 765 return err; 766 } 767 768 static struct tun_struct *__tun_get(struct tun_file *tfile) 769 { 770 struct tun_struct *tun; 771 772 rcu_read_lock(); 773 tun = rcu_dereference(tfile->tun); 774 if (tun) 775 dev_hold(tun->dev); 776 rcu_read_unlock(); 777 778 return tun; 779 } 780 781 static struct tun_struct *tun_get(struct file *file) 782 { 783 return __tun_get(file->private_data); 784 } 785 786 static void tun_put(struct tun_struct *tun) 787 { 788 dev_put(tun->dev); 789 } 790 791 /* TAP filtering */ 792 static void addr_hash_set(u32 *mask, const u8 *addr) 793 { 794 int n = ether_crc(ETH_ALEN, addr) >> 26; 795 mask[n >> 5] |= (1 << (n & 31)); 796 } 797 798 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) 799 { 800 int n = ether_crc(ETH_ALEN, addr) >> 26; 801 return mask[n >> 5] & (1 << (n & 31)); 802 } 803 804 static int update_filter(struct tap_filter *filter, void __user *arg) 805 { 806 struct { u8 u[ETH_ALEN]; } *addr; 807 struct tun_filter uf; 808 int err, alen, n, nexact; 809 810 if (copy_from_user(&uf, arg, sizeof(uf))) 811 return -EFAULT; 812 813 if (!uf.count) { 814 /* Disabled */ 815 filter->count = 0; 816 return 0; 817 } 818 819 alen = ETH_ALEN * uf.count; 820 addr = memdup_user(arg + sizeof(uf), alen); 821 if (IS_ERR(addr)) 822 return PTR_ERR(addr); 823 824 /* The filter is updated without holding any locks. Which is 825 * perfectly safe. We disable it first and in the worst 826 * case we'll accept a few undesired packets. */ 827 filter->count = 0; 828 wmb(); 829 830 /* Use first set of addresses as an exact filter */ 831 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) 832 memcpy(filter->addr[n], addr[n].u, ETH_ALEN); 833 834 nexact = n; 835 836 /* Remaining multicast addresses are hashed, 837 * unicast will leave the filter disabled. */ 838 memset(filter->mask, 0, sizeof(filter->mask)); 839 for (; n < uf.count; n++) { 840 if (!is_multicast_ether_addr(addr[n].u)) { 841 err = 0; /* no filter */ 842 goto free_addr; 843 } 844 addr_hash_set(filter->mask, addr[n].u); 845 } 846 847 /* For ALLMULTI just set the mask to all ones. 848 * This overrides the mask populated above. */ 849 if ((uf.flags & TUN_FLT_ALLMULTI)) 850 memset(filter->mask, ~0, sizeof(filter->mask)); 851 852 /* Now enable the filter */ 853 wmb(); 854 filter->count = nexact; 855 856 /* Return the number of exact filters */ 857 err = nexact; 858 free_addr: 859 kfree(addr); 860 return err; 861 } 862 863 /* Returns: 0 - drop, !=0 - accept */ 864 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) 865 { 866 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect 867 * at this point. */ 868 struct ethhdr *eh = (struct ethhdr *) skb->data; 869 int i; 870 871 /* Exact match */ 872 for (i = 0; i < filter->count; i++) 873 if (ether_addr_equal(eh->h_dest, filter->addr[i])) 874 return 1; 875 876 /* Inexact match (multicast only) */ 877 if (is_multicast_ether_addr(eh->h_dest)) 878 return addr_hash_test(filter->mask, eh->h_dest); 879 880 return 0; 881 } 882 883 /* 884 * Checks whether the packet is accepted or not. 885 * Returns: 0 - drop, !=0 - accept 886 */ 887 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) 888 { 889 if (!filter->count) 890 return 1; 891 892 return run_filter(filter, skb); 893 } 894 895 /* Network device part of the driver */ 896 897 static const struct ethtool_ops tun_ethtool_ops; 898 899 /* Net device detach from fd. */ 900 static void tun_net_uninit(struct net_device *dev) 901 { 902 tun_detach_all(dev); 903 } 904 905 /* Net device open. */ 906 static int tun_net_open(struct net_device *dev) 907 { 908 struct tun_struct *tun = netdev_priv(dev); 909 int i; 910 911 netif_tx_start_all_queues(dev); 912 913 for (i = 0; i < tun->numqueues; i++) { 914 struct tun_file *tfile; 915 916 tfile = rtnl_dereference(tun->tfiles[i]); 917 tfile->socket.sk->sk_write_space(tfile->socket.sk); 918 } 919 920 return 0; 921 } 922 923 /* Net device close. */ 924 static int tun_net_close(struct net_device *dev) 925 { 926 netif_tx_stop_all_queues(dev); 927 return 0; 928 } 929 930 /* Net device start xmit */ 931 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) 932 { 933 struct tun_struct *tun = netdev_priv(dev); 934 int txq = skb->queue_mapping; 935 struct tun_file *tfile; 936 u32 numqueues = 0; 937 938 rcu_read_lock(); 939 tfile = rcu_dereference(tun->tfiles[txq]); 940 numqueues = ACCESS_ONCE(tun->numqueues); 941 942 /* Drop packet if interface is not attached */ 943 if (txq >= numqueues) 944 goto drop; 945 946 #ifdef CONFIG_RPS 947 if (numqueues == 1 && static_key_false(&rps_needed)) { 948 /* Select queue was not called for the skbuff, so we extract the 949 * RPS hash and save it into the flow_table here. 950 */ 951 __u32 rxhash; 952 953 rxhash = __skb_get_hash_symmetric(skb); 954 if (rxhash) { 955 struct tun_flow_entry *e; 956 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], 957 rxhash); 958 if (e) 959 tun_flow_save_rps_rxhash(e, rxhash); 960 } 961 } 962 #endif 963 964 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); 965 966 BUG_ON(!tfile); 967 968 /* Drop if the filter does not like it. 969 * This is a noop if the filter is disabled. 970 * Filter can be enabled only for the TAP devices. */ 971 if (!check_filter(&tun->txflt, skb)) 972 goto drop; 973 974 if (tfile->socket.sk->sk_filter && 975 sk_filter(tfile->socket.sk, skb)) 976 goto drop; 977 978 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 979 goto drop; 980 981 skb_tx_timestamp(skb); 982 983 /* Orphan the skb - required as we might hang on to it 984 * for indefinite time. 985 */ 986 skb_orphan(skb); 987 988 nf_reset(skb); 989 990 if (skb_array_produce(&tfile->tx_array, skb)) 991 goto drop; 992 993 /* Notify and wake up reader process */ 994 if (tfile->flags & TUN_FASYNC) 995 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); 996 tfile->socket.sk->sk_data_ready(tfile->socket.sk); 997 998 rcu_read_unlock(); 999 return NETDEV_TX_OK; 1000 1001 drop: 1002 this_cpu_inc(tun->pcpu_stats->tx_dropped); 1003 skb_tx_error(skb); 1004 kfree_skb(skb); 1005 rcu_read_unlock(); 1006 return NET_XMIT_DROP; 1007 } 1008 1009 static void tun_net_mclist(struct net_device *dev) 1010 { 1011 /* 1012 * This callback is supposed to deal with mc filter in 1013 * _rx_ path and has nothing to do with the _tx_ path. 1014 * In rx path we always accept everything userspace gives us. 1015 */ 1016 } 1017 1018 static netdev_features_t tun_net_fix_features(struct net_device *dev, 1019 netdev_features_t features) 1020 { 1021 struct tun_struct *tun = netdev_priv(dev); 1022 1023 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); 1024 } 1025 #ifdef CONFIG_NET_POLL_CONTROLLER 1026 static void tun_poll_controller(struct net_device *dev) 1027 { 1028 /* 1029 * Tun only receives frames when: 1030 * 1) the char device endpoint gets data from user space 1031 * 2) the tun socket gets a sendmsg call from user space 1032 * If NAPI is not enabled, since both of those are synchronous 1033 * operations, we are guaranteed never to have pending data when we poll 1034 * for it so there is nothing to do here but return. 1035 * We need this though so netpoll recognizes us as an interface that 1036 * supports polling, which enables bridge devices in virt setups to 1037 * still use netconsole 1038 * If NAPI is enabled, however, we need to schedule polling for all 1039 * queues. 1040 */ 1041 struct tun_struct *tun = netdev_priv(dev); 1042 1043 if (tun->flags & IFF_NAPI) { 1044 struct tun_file *tfile; 1045 int i; 1046 1047 rcu_read_lock(); 1048 for (i = 0; i < tun->numqueues; i++) { 1049 tfile = rcu_dereference(tun->tfiles[i]); 1050 napi_schedule(&tfile->napi); 1051 } 1052 rcu_read_unlock(); 1053 } 1054 return; 1055 } 1056 #endif 1057 1058 static void tun_set_headroom(struct net_device *dev, int new_hr) 1059 { 1060 struct tun_struct *tun = netdev_priv(dev); 1061 1062 if (new_hr < NET_SKB_PAD) 1063 new_hr = NET_SKB_PAD; 1064 1065 tun->align = new_hr; 1066 } 1067 1068 static void 1069 tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 1070 { 1071 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; 1072 struct tun_struct *tun = netdev_priv(dev); 1073 struct tun_pcpu_stats *p; 1074 int i; 1075 1076 for_each_possible_cpu(i) { 1077 u64 rxpackets, rxbytes, txpackets, txbytes; 1078 unsigned int start; 1079 1080 p = per_cpu_ptr(tun->pcpu_stats, i); 1081 do { 1082 start = u64_stats_fetch_begin(&p->syncp); 1083 rxpackets = p->rx_packets; 1084 rxbytes = p->rx_bytes; 1085 txpackets = p->tx_packets; 1086 txbytes = p->tx_bytes; 1087 } while (u64_stats_fetch_retry(&p->syncp, start)); 1088 1089 stats->rx_packets += rxpackets; 1090 stats->rx_bytes += rxbytes; 1091 stats->tx_packets += txpackets; 1092 stats->tx_bytes += txbytes; 1093 1094 /* u32 counters */ 1095 rx_dropped += p->rx_dropped; 1096 rx_frame_errors += p->rx_frame_errors; 1097 tx_dropped += p->tx_dropped; 1098 } 1099 stats->rx_dropped = rx_dropped; 1100 stats->rx_frame_errors = rx_frame_errors; 1101 stats->tx_dropped = tx_dropped; 1102 } 1103 1104 static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, 1105 struct netlink_ext_ack *extack) 1106 { 1107 struct tun_struct *tun = netdev_priv(dev); 1108 struct bpf_prog *old_prog; 1109 1110 old_prog = rtnl_dereference(tun->xdp_prog); 1111 rcu_assign_pointer(tun->xdp_prog, prog); 1112 if (old_prog) 1113 bpf_prog_put(old_prog); 1114 1115 return 0; 1116 } 1117 1118 static u32 tun_xdp_query(struct net_device *dev) 1119 { 1120 struct tun_struct *tun = netdev_priv(dev); 1121 const struct bpf_prog *xdp_prog; 1122 1123 xdp_prog = rtnl_dereference(tun->xdp_prog); 1124 if (xdp_prog) 1125 return xdp_prog->aux->id; 1126 1127 return 0; 1128 } 1129 1130 static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp) 1131 { 1132 switch (xdp->command) { 1133 case XDP_SETUP_PROG: 1134 return tun_xdp_set(dev, xdp->prog, xdp->extack); 1135 case XDP_QUERY_PROG: 1136 xdp->prog_id = tun_xdp_query(dev); 1137 xdp->prog_attached = !!xdp->prog_id; 1138 return 0; 1139 default: 1140 return -EINVAL; 1141 } 1142 } 1143 1144 static const struct net_device_ops tun_netdev_ops = { 1145 .ndo_uninit = tun_net_uninit, 1146 .ndo_open = tun_net_open, 1147 .ndo_stop = tun_net_close, 1148 .ndo_start_xmit = tun_net_xmit, 1149 .ndo_fix_features = tun_net_fix_features, 1150 .ndo_select_queue = tun_select_queue, 1151 #ifdef CONFIG_NET_POLL_CONTROLLER 1152 .ndo_poll_controller = tun_poll_controller, 1153 #endif 1154 .ndo_set_rx_headroom = tun_set_headroom, 1155 .ndo_get_stats64 = tun_net_get_stats64, 1156 }; 1157 1158 static const struct net_device_ops tap_netdev_ops = { 1159 .ndo_uninit = tun_net_uninit, 1160 .ndo_open = tun_net_open, 1161 .ndo_stop = tun_net_close, 1162 .ndo_start_xmit = tun_net_xmit, 1163 .ndo_fix_features = tun_net_fix_features, 1164 .ndo_set_rx_mode = tun_net_mclist, 1165 .ndo_set_mac_address = eth_mac_addr, 1166 .ndo_validate_addr = eth_validate_addr, 1167 .ndo_select_queue = tun_select_queue, 1168 #ifdef CONFIG_NET_POLL_CONTROLLER 1169 .ndo_poll_controller = tun_poll_controller, 1170 #endif 1171 .ndo_features_check = passthru_features_check, 1172 .ndo_set_rx_headroom = tun_set_headroom, 1173 .ndo_get_stats64 = tun_net_get_stats64, 1174 .ndo_xdp = tun_xdp, 1175 }; 1176 1177 static void tun_flow_init(struct tun_struct *tun) 1178 { 1179 int i; 1180 1181 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) 1182 INIT_HLIST_HEAD(&tun->flows[i]); 1183 1184 tun->ageing_time = TUN_FLOW_EXPIRE; 1185 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); 1186 mod_timer(&tun->flow_gc_timer, 1187 round_jiffies_up(jiffies + tun->ageing_time)); 1188 } 1189 1190 static void tun_flow_uninit(struct tun_struct *tun) 1191 { 1192 del_timer_sync(&tun->flow_gc_timer); 1193 tun_flow_flush(tun); 1194 } 1195 1196 #define MIN_MTU 68 1197 #define MAX_MTU 65535 1198 1199 /* Initialize net device. */ 1200 static void tun_net_init(struct net_device *dev) 1201 { 1202 struct tun_struct *tun = netdev_priv(dev); 1203 1204 switch (tun->flags & TUN_TYPE_MASK) { 1205 case IFF_TUN: 1206 dev->netdev_ops = &tun_netdev_ops; 1207 1208 /* Point-to-Point TUN Device */ 1209 dev->hard_header_len = 0; 1210 dev->addr_len = 0; 1211 dev->mtu = 1500; 1212 1213 /* Zero header length */ 1214 dev->type = ARPHRD_NONE; 1215 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 1216 break; 1217 1218 case IFF_TAP: 1219 dev->netdev_ops = &tap_netdev_ops; 1220 /* Ethernet TAP Device */ 1221 ether_setup(dev); 1222 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1223 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1224 1225 eth_hw_addr_random(dev); 1226 1227 break; 1228 } 1229 1230 dev->min_mtu = MIN_MTU; 1231 dev->max_mtu = MAX_MTU - dev->hard_header_len; 1232 } 1233 1234 /* Character device part */ 1235 1236 /* Poll */ 1237 static unsigned int tun_chr_poll(struct file *file, poll_table *wait) 1238 { 1239 struct tun_file *tfile = file->private_data; 1240 struct tun_struct *tun = __tun_get(tfile); 1241 struct sock *sk; 1242 unsigned int mask = 0; 1243 1244 if (!tun) 1245 return POLLERR; 1246 1247 sk = tfile->socket.sk; 1248 1249 tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); 1250 1251 poll_wait(file, sk_sleep(sk), wait); 1252 1253 if (!skb_array_empty(&tfile->tx_array)) 1254 mask |= POLLIN | POLLRDNORM; 1255 1256 if (tun->dev->flags & IFF_UP && 1257 (sock_writeable(sk) || 1258 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && 1259 sock_writeable(sk)))) 1260 mask |= POLLOUT | POLLWRNORM; 1261 1262 if (tun->dev->reg_state != NETREG_REGISTERED) 1263 mask = POLLERR; 1264 1265 tun_put(tun); 1266 return mask; 1267 } 1268 1269 /* prepad is the amount to reserve at front. len is length after that. 1270 * linear is a hint as to how much to copy (usually headers). */ 1271 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, 1272 size_t prepad, size_t len, 1273 size_t linear, int noblock) 1274 { 1275 struct sock *sk = tfile->socket.sk; 1276 struct sk_buff *skb; 1277 int err; 1278 1279 /* Under a page? Don't bother with paged skb. */ 1280 if (prepad + len < PAGE_SIZE || !linear) 1281 linear = len; 1282 1283 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 1284 &err, 0); 1285 if (!skb) 1286 return ERR_PTR(err); 1287 1288 skb_reserve(skb, prepad); 1289 skb_put(skb, linear); 1290 skb->data_len = len - linear; 1291 skb->len += len - linear; 1292 1293 return skb; 1294 } 1295 1296 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, 1297 struct sk_buff *skb, int more) 1298 { 1299 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 1300 struct sk_buff_head process_queue; 1301 u32 rx_batched = tun->rx_batched; 1302 bool rcv = false; 1303 1304 if (!rx_batched || (!more && skb_queue_empty(queue))) { 1305 local_bh_disable(); 1306 netif_receive_skb(skb); 1307 local_bh_enable(); 1308 return; 1309 } 1310 1311 spin_lock(&queue->lock); 1312 if (!more || skb_queue_len(queue) == rx_batched) { 1313 __skb_queue_head_init(&process_queue); 1314 skb_queue_splice_tail_init(queue, &process_queue); 1315 rcv = true; 1316 } else { 1317 __skb_queue_tail(queue, skb); 1318 } 1319 spin_unlock(&queue->lock); 1320 1321 if (rcv) { 1322 struct sk_buff *nskb; 1323 1324 local_bh_disable(); 1325 while ((nskb = __skb_dequeue(&process_queue))) 1326 netif_receive_skb(nskb); 1327 netif_receive_skb(skb); 1328 local_bh_enable(); 1329 } 1330 } 1331 1332 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, 1333 int len, int noblock, bool zerocopy) 1334 { 1335 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 1336 return false; 1337 1338 if (tfile->socket.sk->sk_sndbuf != INT_MAX) 1339 return false; 1340 1341 if (!noblock) 1342 return false; 1343 1344 if (zerocopy) 1345 return false; 1346 1347 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + 1348 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) 1349 return false; 1350 1351 return true; 1352 } 1353 1354 static struct sk_buff *tun_build_skb(struct tun_struct *tun, 1355 struct tun_file *tfile, 1356 struct iov_iter *from, 1357 struct virtio_net_hdr *hdr, 1358 int len, int *skb_xdp) 1359 { 1360 struct page_frag *alloc_frag = ¤t->task_frag; 1361 struct sk_buff *skb; 1362 struct bpf_prog *xdp_prog; 1363 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1364 unsigned int delta = 0; 1365 char *buf; 1366 size_t copied; 1367 bool xdp_xmit = false; 1368 int err, pad = TUN_RX_PAD; 1369 1370 rcu_read_lock(); 1371 xdp_prog = rcu_dereference(tun->xdp_prog); 1372 if (xdp_prog) 1373 pad += TUN_HEADROOM; 1374 buflen += SKB_DATA_ALIGN(len + pad); 1375 rcu_read_unlock(); 1376 1377 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) 1378 return ERR_PTR(-ENOMEM); 1379 1380 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 1381 copied = copy_page_from_iter(alloc_frag->page, 1382 alloc_frag->offset + pad, 1383 len, from); 1384 if (copied != len) 1385 return ERR_PTR(-EFAULT); 1386 1387 /* There's a small window that XDP may be set after the check 1388 * of xdp_prog above, this should be rare and for simplicity 1389 * we do XDP on skb in case the headroom is not enough. 1390 */ 1391 if (hdr->gso_type || !xdp_prog) 1392 *skb_xdp = 1; 1393 else 1394 *skb_xdp = 0; 1395 1396 rcu_read_lock(); 1397 xdp_prog = rcu_dereference(tun->xdp_prog); 1398 if (xdp_prog && !*skb_xdp) { 1399 struct xdp_buff xdp; 1400 void *orig_data; 1401 u32 act; 1402 1403 xdp.data_hard_start = buf; 1404 xdp.data = buf + pad; 1405 xdp.data_end = xdp.data + len; 1406 orig_data = xdp.data; 1407 act = bpf_prog_run_xdp(xdp_prog, &xdp); 1408 1409 switch (act) { 1410 case XDP_REDIRECT: 1411 get_page(alloc_frag->page); 1412 alloc_frag->offset += buflen; 1413 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); 1414 if (err) 1415 goto err_redirect; 1416 return NULL; 1417 case XDP_TX: 1418 xdp_xmit = true; 1419 /* fall through */ 1420 case XDP_PASS: 1421 delta = orig_data - xdp.data; 1422 break; 1423 default: 1424 bpf_warn_invalid_xdp_action(act); 1425 /* fall through */ 1426 case XDP_ABORTED: 1427 trace_xdp_exception(tun->dev, xdp_prog, act); 1428 /* fall through */ 1429 case XDP_DROP: 1430 goto err_xdp; 1431 } 1432 } 1433 1434 skb = build_skb(buf, buflen); 1435 if (!skb) { 1436 rcu_read_unlock(); 1437 return ERR_PTR(-ENOMEM); 1438 } 1439 1440 skb_reserve(skb, pad - delta); 1441 skb_put(skb, len + delta); 1442 get_page(alloc_frag->page); 1443 alloc_frag->offset += buflen; 1444 1445 if (xdp_xmit) { 1446 skb->dev = tun->dev; 1447 generic_xdp_tx(skb, xdp_prog); 1448 rcu_read_lock(); 1449 return NULL; 1450 } 1451 1452 rcu_read_unlock(); 1453 1454 return skb; 1455 1456 err_redirect: 1457 put_page(alloc_frag->page); 1458 err_xdp: 1459 rcu_read_unlock(); 1460 this_cpu_inc(tun->pcpu_stats->rx_dropped); 1461 return NULL; 1462 } 1463 1464 /* Get packet from user space buffer */ 1465 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1466 void *msg_control, struct iov_iter *from, 1467 int noblock, bool more) 1468 { 1469 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; 1470 struct sk_buff *skb; 1471 size_t total_len = iov_iter_count(from); 1472 size_t len = total_len, align = tun->align, linear; 1473 struct virtio_net_hdr gso = { 0 }; 1474 struct tun_pcpu_stats *stats; 1475 int good_linear; 1476 int copylen; 1477 bool zerocopy = false; 1478 int err; 1479 u32 rxhash; 1480 int skb_xdp = 1; 1481 1482 if (!(tun->dev->flags & IFF_UP)) 1483 return -EIO; 1484 1485 if (!(tun->flags & IFF_NO_PI)) { 1486 if (len < sizeof(pi)) 1487 return -EINVAL; 1488 len -= sizeof(pi); 1489 1490 if (!copy_from_iter_full(&pi, sizeof(pi), from)) 1491 return -EFAULT; 1492 } 1493 1494 if (tun->flags & IFF_VNET_HDR) { 1495 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1496 1497 if (len < vnet_hdr_sz) 1498 return -EINVAL; 1499 len -= vnet_hdr_sz; 1500 1501 if (!copy_from_iter_full(&gso, sizeof(gso), from)) 1502 return -EFAULT; 1503 1504 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 1505 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) 1506 gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); 1507 1508 if (tun16_to_cpu(tun, gso.hdr_len) > len) 1509 return -EINVAL; 1510 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); 1511 } 1512 1513 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { 1514 align += NET_IP_ALIGN; 1515 if (unlikely(len < ETH_HLEN || 1516 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) 1517 return -EINVAL; 1518 } 1519 1520 good_linear = SKB_MAX_HEAD(align); 1521 1522 if (msg_control) { 1523 struct iov_iter i = *from; 1524 1525 /* There are 256 bytes to be copied in skb, so there is 1526 * enough room for skb expand head in case it is used. 1527 * The rest of the buffer is mapped from userspace. 1528 */ 1529 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; 1530 if (copylen > good_linear) 1531 copylen = good_linear; 1532 linear = copylen; 1533 iov_iter_advance(&i, copylen); 1534 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 1535 zerocopy = true; 1536 } 1537 1538 if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { 1539 /* For the packet that is not easy to be processed 1540 * (e.g gso or jumbo packet), we will do it at after 1541 * skb was created with generic XDP routine. 1542 */ 1543 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); 1544 if (IS_ERR(skb)) { 1545 this_cpu_inc(tun->pcpu_stats->rx_dropped); 1546 return PTR_ERR(skb); 1547 } 1548 if (!skb) 1549 return total_len; 1550 } else { 1551 if (!zerocopy) { 1552 copylen = len; 1553 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) 1554 linear = good_linear; 1555 else 1556 linear = tun16_to_cpu(tun, gso.hdr_len); 1557 } 1558 1559 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); 1560 if (IS_ERR(skb)) { 1561 if (PTR_ERR(skb) != -EAGAIN) 1562 this_cpu_inc(tun->pcpu_stats->rx_dropped); 1563 return PTR_ERR(skb); 1564 } 1565 1566 if (zerocopy) 1567 err = zerocopy_sg_from_iter(skb, from); 1568 else 1569 err = skb_copy_datagram_from_iter(skb, 0, from, len); 1570 1571 if (err) { 1572 this_cpu_inc(tun->pcpu_stats->rx_dropped); 1573 kfree_skb(skb); 1574 return -EFAULT; 1575 } 1576 } 1577 1578 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { 1579 this_cpu_inc(tun->pcpu_stats->rx_frame_errors); 1580 kfree_skb(skb); 1581 return -EINVAL; 1582 } 1583 1584 switch (tun->flags & TUN_TYPE_MASK) { 1585 case IFF_TUN: 1586 if (tun->flags & IFF_NO_PI) { 1587 switch (skb->data[0] & 0xf0) { 1588 case 0x40: 1589 pi.proto = htons(ETH_P_IP); 1590 break; 1591 case 0x60: 1592 pi.proto = htons(ETH_P_IPV6); 1593 break; 1594 default: 1595 this_cpu_inc(tun->pcpu_stats->rx_dropped); 1596 kfree_skb(skb); 1597 return -EINVAL; 1598 } 1599 } 1600 1601 skb_reset_mac_header(skb); 1602 skb->protocol = pi.proto; 1603 skb->dev = tun->dev; 1604 break; 1605 case IFF_TAP: 1606 skb->protocol = eth_type_trans(skb, tun->dev); 1607 break; 1608 } 1609 1610 /* copy skb_ubuf_info for callback when skb has no error */ 1611 if (zerocopy) { 1612 skb_shinfo(skb)->destructor_arg = msg_control; 1613 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 1614 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1615 } else if (msg_control) { 1616 struct ubuf_info *uarg = msg_control; 1617 uarg->callback(uarg, false); 1618 } 1619 1620 skb_reset_network_header(skb); 1621 skb_probe_transport_header(skb, 0); 1622 1623 if (skb_xdp) { 1624 struct bpf_prog *xdp_prog; 1625 int ret; 1626 1627 rcu_read_lock(); 1628 xdp_prog = rcu_dereference(tun->xdp_prog); 1629 if (xdp_prog) { 1630 ret = do_xdp_generic(xdp_prog, skb); 1631 if (ret != XDP_PASS) { 1632 rcu_read_unlock(); 1633 return total_len; 1634 } 1635 } 1636 rcu_read_unlock(); 1637 } 1638 1639 rxhash = __skb_get_hash_symmetric(skb); 1640 1641 if (tun->flags & IFF_NAPI) { 1642 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; 1643 int queue_len; 1644 1645 spin_lock_bh(&queue->lock); 1646 __skb_queue_tail(queue, skb); 1647 queue_len = skb_queue_len(queue); 1648 spin_unlock(&queue->lock); 1649 1650 if (!more || queue_len > NAPI_POLL_WEIGHT) 1651 napi_schedule(&tfile->napi); 1652 1653 local_bh_enable(); 1654 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) { 1655 tun_rx_batched(tun, tfile, skb, more); 1656 } else { 1657 netif_rx_ni(skb); 1658 } 1659 1660 stats = get_cpu_ptr(tun->pcpu_stats); 1661 u64_stats_update_begin(&stats->syncp); 1662 stats->rx_packets++; 1663 stats->rx_bytes += len; 1664 u64_stats_update_end(&stats->syncp); 1665 put_cpu_ptr(stats); 1666 1667 tun_flow_update(tun, rxhash, tfile); 1668 return total_len; 1669 } 1670 1671 static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) 1672 { 1673 struct file *file = iocb->ki_filp; 1674 struct tun_struct *tun = tun_get(file); 1675 struct tun_file *tfile = file->private_data; 1676 ssize_t result; 1677 1678 if (!tun) 1679 return -EBADFD; 1680 1681 result = tun_get_user(tun, tfile, NULL, from, 1682 file->f_flags & O_NONBLOCK, false); 1683 1684 tun_put(tun); 1685 return result; 1686 } 1687 1688 /* Put packet to the user space buffer */ 1689 static ssize_t tun_put_user(struct tun_struct *tun, 1690 struct tun_file *tfile, 1691 struct sk_buff *skb, 1692 struct iov_iter *iter) 1693 { 1694 struct tun_pi pi = { 0, skb->protocol }; 1695 struct tun_pcpu_stats *stats; 1696 ssize_t total; 1697 int vlan_offset = 0; 1698 int vlan_hlen = 0; 1699 int vnet_hdr_sz = 0; 1700 1701 if (skb_vlan_tag_present(skb)) 1702 vlan_hlen = VLAN_HLEN; 1703 1704 if (tun->flags & IFF_VNET_HDR) 1705 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); 1706 1707 total = skb->len + vlan_hlen + vnet_hdr_sz; 1708 1709 if (!(tun->flags & IFF_NO_PI)) { 1710 if (iov_iter_count(iter) < sizeof(pi)) 1711 return -EINVAL; 1712 1713 total += sizeof(pi); 1714 if (iov_iter_count(iter) < total) { 1715 /* Packet will be striped */ 1716 pi.flags |= TUN_PKT_STRIP; 1717 } 1718 1719 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) 1720 return -EFAULT; 1721 } 1722 1723 if (vnet_hdr_sz) { 1724 struct virtio_net_hdr gso; 1725 1726 if (iov_iter_count(iter) < vnet_hdr_sz) 1727 return -EINVAL; 1728 1729 if (virtio_net_hdr_from_skb(skb, &gso, 1730 tun_is_little_endian(tun), true)) { 1731 struct skb_shared_info *sinfo = skb_shinfo(skb); 1732 pr_err("unexpected GSO type: " 1733 "0x%x, gso_size %d, hdr_len %d\n", 1734 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), 1735 tun16_to_cpu(tun, gso.hdr_len)); 1736 print_hex_dump(KERN_ERR, "tun: ", 1737 DUMP_PREFIX_NONE, 1738 16, 1, skb->head, 1739 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); 1740 WARN_ON_ONCE(1); 1741 return -EINVAL; 1742 } 1743 1744 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) 1745 return -EFAULT; 1746 1747 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); 1748 } 1749 1750 if (vlan_hlen) { 1751 int ret; 1752 struct { 1753 __be16 h_vlan_proto; 1754 __be16 h_vlan_TCI; 1755 } veth; 1756 1757 veth.h_vlan_proto = skb->vlan_proto; 1758 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 1759 1760 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 1761 1762 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 1763 if (ret || !iov_iter_count(iter)) 1764 goto done; 1765 1766 ret = copy_to_iter(&veth, sizeof(veth), iter); 1767 if (ret != sizeof(veth) || !iov_iter_count(iter)) 1768 goto done; 1769 } 1770 1771 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); 1772 1773 done: 1774 /* caller is in process context, */ 1775 stats = get_cpu_ptr(tun->pcpu_stats); 1776 u64_stats_update_begin(&stats->syncp); 1777 stats->tx_packets++; 1778 stats->tx_bytes += skb->len + vlan_hlen; 1779 u64_stats_update_end(&stats->syncp); 1780 put_cpu_ptr(tun->pcpu_stats); 1781 1782 return total; 1783 } 1784 1785 static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, 1786 int *err) 1787 { 1788 DECLARE_WAITQUEUE(wait, current); 1789 struct sk_buff *skb = NULL; 1790 int error = 0; 1791 1792 skb = skb_array_consume(&tfile->tx_array); 1793 if (skb) 1794 goto out; 1795 if (noblock) { 1796 error = -EAGAIN; 1797 goto out; 1798 } 1799 1800 add_wait_queue(&tfile->wq.wait, &wait); 1801 current->state = TASK_INTERRUPTIBLE; 1802 1803 while (1) { 1804 skb = skb_array_consume(&tfile->tx_array); 1805 if (skb) 1806 break; 1807 if (signal_pending(current)) { 1808 error = -ERESTARTSYS; 1809 break; 1810 } 1811 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { 1812 error = -EFAULT; 1813 break; 1814 } 1815 1816 schedule(); 1817 } 1818 1819 current->state = TASK_RUNNING; 1820 remove_wait_queue(&tfile->wq.wait, &wait); 1821 1822 out: 1823 *err = error; 1824 return skb; 1825 } 1826 1827 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, 1828 struct iov_iter *to, 1829 int noblock, struct sk_buff *skb) 1830 { 1831 ssize_t ret; 1832 int err; 1833 1834 tun_debug(KERN_INFO, tun, "tun_do_read\n"); 1835 1836 if (!iov_iter_count(to)) 1837 return 0; 1838 1839 if (!skb) { 1840 /* Read frames from ring */ 1841 skb = tun_ring_recv(tfile, noblock, &err); 1842 if (!skb) 1843 return err; 1844 } 1845 1846 ret = tun_put_user(tun, tfile, skb, to); 1847 if (unlikely(ret < 0)) 1848 kfree_skb(skb); 1849 else 1850 consume_skb(skb); 1851 1852 return ret; 1853 } 1854 1855 static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) 1856 { 1857 struct file *file = iocb->ki_filp; 1858 struct tun_file *tfile = file->private_data; 1859 struct tun_struct *tun = __tun_get(tfile); 1860 ssize_t len = iov_iter_count(to), ret; 1861 1862 if (!tun) 1863 return -EBADFD; 1864 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); 1865 ret = min_t(ssize_t, ret, len); 1866 if (ret > 0) 1867 iocb->ki_pos = ret; 1868 tun_put(tun); 1869 return ret; 1870 } 1871 1872 static void tun_free_netdev(struct net_device *dev) 1873 { 1874 struct tun_struct *tun = netdev_priv(dev); 1875 1876 BUG_ON(!(list_empty(&tun->disabled))); 1877 free_percpu(tun->pcpu_stats); 1878 tun_flow_uninit(tun); 1879 security_tun_dev_free_security(tun->security); 1880 } 1881 1882 static void tun_setup(struct net_device *dev) 1883 { 1884 struct tun_struct *tun = netdev_priv(dev); 1885 1886 tun->owner = INVALID_UID; 1887 tun->group = INVALID_GID; 1888 1889 dev->ethtool_ops = &tun_ethtool_ops; 1890 dev->needs_free_netdev = true; 1891 dev->priv_destructor = tun_free_netdev; 1892 /* We prefer our own queue length */ 1893 dev->tx_queue_len = TUN_READQ_SIZE; 1894 } 1895 1896 /* Trivial set of netlink ops to allow deleting tun or tap 1897 * device with netlink. 1898 */ 1899 static int tun_validate(struct nlattr *tb[], struct nlattr *data[], 1900 struct netlink_ext_ack *extack) 1901 { 1902 return -EINVAL; 1903 } 1904 1905 static struct rtnl_link_ops tun_link_ops __read_mostly = { 1906 .kind = DRV_NAME, 1907 .priv_size = sizeof(struct tun_struct), 1908 .setup = tun_setup, 1909 .validate = tun_validate, 1910 }; 1911 1912 static void tun_sock_write_space(struct sock *sk) 1913 { 1914 struct tun_file *tfile; 1915 wait_queue_head_t *wqueue; 1916 1917 if (!sock_writeable(sk)) 1918 return; 1919 1920 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 1921 return; 1922 1923 wqueue = sk_sleep(sk); 1924 if (wqueue && waitqueue_active(wqueue)) 1925 wake_up_interruptible_sync_poll(wqueue, POLLOUT | 1926 POLLWRNORM | POLLWRBAND); 1927 1928 tfile = container_of(sk, struct tun_file, sk); 1929 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); 1930 } 1931 1932 static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) 1933 { 1934 int ret; 1935 struct tun_file *tfile = container_of(sock, struct tun_file, socket); 1936 struct tun_struct *tun = __tun_get(tfile); 1937 1938 if (!tun) 1939 return -EBADFD; 1940 1941 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, 1942 m->msg_flags & MSG_DONTWAIT, 1943 m->msg_flags & MSG_MORE); 1944 tun_put(tun); 1945 return ret; 1946 } 1947 1948 static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, 1949 int flags) 1950 { 1951 struct tun_file *tfile = container_of(sock, struct tun_file, socket); 1952 struct tun_struct *tun = __tun_get(tfile); 1953 int ret; 1954 1955 if (!tun) 1956 return -EBADFD; 1957 1958 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { 1959 ret = -EINVAL; 1960 goto out; 1961 } 1962 if (flags & MSG_ERRQUEUE) { 1963 ret = sock_recv_errqueue(sock->sk, m, total_len, 1964 SOL_PACKET, TUN_TX_TIMESTAMP); 1965 goto out; 1966 } 1967 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, 1968 m->msg_control); 1969 if (ret > (ssize_t)total_len) { 1970 m->msg_flags |= MSG_TRUNC; 1971 ret = flags & MSG_TRUNC ? ret : total_len; 1972 } 1973 out: 1974 tun_put(tun); 1975 return ret; 1976 } 1977 1978 static int tun_peek_len(struct socket *sock) 1979 { 1980 struct tun_file *tfile = container_of(sock, struct tun_file, socket); 1981 struct tun_struct *tun; 1982 int ret = 0; 1983 1984 tun = __tun_get(tfile); 1985 if (!tun) 1986 return 0; 1987 1988 ret = skb_array_peek_len(&tfile->tx_array); 1989 tun_put(tun); 1990 1991 return ret; 1992 } 1993 1994 /* Ops structure to mimic raw sockets with tun */ 1995 static const struct proto_ops tun_socket_ops = { 1996 .peek_len = tun_peek_len, 1997 .sendmsg = tun_sendmsg, 1998 .recvmsg = tun_recvmsg, 1999 }; 2000 2001 static struct proto tun_proto = { 2002 .name = "tun", 2003 .owner = THIS_MODULE, 2004 .obj_size = sizeof(struct tun_file), 2005 }; 2006 2007 static int tun_flags(struct tun_struct *tun) 2008 { 2009 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); 2010 } 2011 2012 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, 2013 char *buf) 2014 { 2015 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2016 return sprintf(buf, "0x%x\n", tun_flags(tun)); 2017 } 2018 2019 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, 2020 char *buf) 2021 { 2022 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2023 return uid_valid(tun->owner)? 2024 sprintf(buf, "%u\n", 2025 from_kuid_munged(current_user_ns(), tun->owner)): 2026 sprintf(buf, "-1\n"); 2027 } 2028 2029 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, 2030 char *buf) 2031 { 2032 struct tun_struct *tun = netdev_priv(to_net_dev(dev)); 2033 return gid_valid(tun->group) ? 2034 sprintf(buf, "%u\n", 2035 from_kgid_munged(current_user_ns(), tun->group)): 2036 sprintf(buf, "-1\n"); 2037 } 2038 2039 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); 2040 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); 2041 static DEVICE_ATTR(group, 0444, tun_show_group, NULL); 2042 2043 static struct attribute *tun_dev_attrs[] = { 2044 &dev_attr_tun_flags.attr, 2045 &dev_attr_owner.attr, 2046 &dev_attr_group.attr, 2047 NULL 2048 }; 2049 2050 static const struct attribute_group tun_attr_group = { 2051 .attrs = tun_dev_attrs 2052 }; 2053 2054 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) 2055 { 2056 struct tun_struct *tun; 2057 struct tun_file *tfile = file->private_data; 2058 struct net_device *dev; 2059 int err; 2060 2061 if (tfile->detached) 2062 return -EINVAL; 2063 2064 dev = __dev_get_by_name(net, ifr->ifr_name); 2065 if (dev) { 2066 if (ifr->ifr_flags & IFF_TUN_EXCL) 2067 return -EBUSY; 2068 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) 2069 tun = netdev_priv(dev); 2070 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) 2071 tun = netdev_priv(dev); 2072 else 2073 return -EINVAL; 2074 2075 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != 2076 !!(tun->flags & IFF_MULTI_QUEUE)) 2077 return -EINVAL; 2078 2079 if (tun_not_capable(tun)) 2080 return -EPERM; 2081 err = security_tun_dev_open(tun->security); 2082 if (err < 0) 2083 return err; 2084 2085 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, 2086 ifr->ifr_flags & IFF_NAPI); 2087 if (err < 0) 2088 return err; 2089 2090 if (tun->flags & IFF_MULTI_QUEUE && 2091 (tun->numqueues + tun->numdisabled > 1)) { 2092 /* One or more queue has already been attached, no need 2093 * to initialize the device again. 2094 */ 2095 return 0; 2096 } 2097 } 2098 else { 2099 char *name; 2100 unsigned long flags = 0; 2101 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? 2102 MAX_TAP_QUEUES : 1; 2103 2104 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2105 return -EPERM; 2106 err = security_tun_dev_create(); 2107 if (err < 0) 2108 return err; 2109 2110 /* Set dev type */ 2111 if (ifr->ifr_flags & IFF_TUN) { 2112 /* TUN device */ 2113 flags |= IFF_TUN; 2114 name = "tun%d"; 2115 } else if (ifr->ifr_flags & IFF_TAP) { 2116 /* TAP device */ 2117 flags |= IFF_TAP; 2118 name = "tap%d"; 2119 } else 2120 return -EINVAL; 2121 2122 if (*ifr->ifr_name) 2123 name = ifr->ifr_name; 2124 2125 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, 2126 NET_NAME_UNKNOWN, tun_setup, queues, 2127 queues); 2128 2129 if (!dev) 2130 return -ENOMEM; 2131 2132 dev_net_set(dev, net); 2133 dev->rtnl_link_ops = &tun_link_ops; 2134 dev->ifindex = tfile->ifindex; 2135 dev->sysfs_groups[0] = &tun_attr_group; 2136 2137 tun = netdev_priv(dev); 2138 tun->dev = dev; 2139 tun->flags = flags; 2140 tun->txflt.count = 0; 2141 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 2142 2143 tun->align = NET_SKB_PAD; 2144 tun->filter_attached = false; 2145 tun->sndbuf = tfile->socket.sk->sk_sndbuf; 2146 tun->rx_batched = 0; 2147 2148 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); 2149 if (!tun->pcpu_stats) { 2150 err = -ENOMEM; 2151 goto err_free_dev; 2152 } 2153 2154 spin_lock_init(&tun->lock); 2155 2156 err = security_tun_dev_alloc_security(&tun->security); 2157 if (err < 0) 2158 goto err_free_stat; 2159 2160 tun_net_init(dev); 2161 tun_flow_init(tun); 2162 2163 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | 2164 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 2165 NETIF_F_HW_VLAN_STAG_TX; 2166 dev->features = dev->hw_features | NETIF_F_LLTX; 2167 dev->vlan_features = dev->features & 2168 ~(NETIF_F_HW_VLAN_CTAG_TX | 2169 NETIF_F_HW_VLAN_STAG_TX); 2170 2171 INIT_LIST_HEAD(&tun->disabled); 2172 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI); 2173 if (err < 0) 2174 goto err_free_flow; 2175 2176 err = register_netdevice(tun->dev); 2177 if (err < 0) 2178 goto err_detach; 2179 } 2180 2181 netif_carrier_on(tun->dev); 2182 2183 tun_debug(KERN_INFO, tun, "tun_set_iff\n"); 2184 2185 tun->flags = (tun->flags & ~TUN_FEATURES) | 2186 (ifr->ifr_flags & TUN_FEATURES); 2187 2188 /* Make sure persistent devices do not get stuck in 2189 * xoff state. 2190 */ 2191 if (netif_running(tun->dev)) 2192 netif_tx_wake_all_queues(tun->dev); 2193 2194 strcpy(ifr->ifr_name, tun->dev->name); 2195 return 0; 2196 2197 err_detach: 2198 tun_detach_all(dev); 2199 /* register_netdevice() already called tun_free_netdev() */ 2200 goto err_free_dev; 2201 2202 err_free_flow: 2203 tun_flow_uninit(tun); 2204 security_tun_dev_free_security(tun->security); 2205 err_free_stat: 2206 free_percpu(tun->pcpu_stats); 2207 err_free_dev: 2208 free_netdev(dev); 2209 return err; 2210 } 2211 2212 static void tun_get_iff(struct net *net, struct tun_struct *tun, 2213 struct ifreq *ifr) 2214 { 2215 tun_debug(KERN_INFO, tun, "tun_get_iff\n"); 2216 2217 strcpy(ifr->ifr_name, tun->dev->name); 2218 2219 ifr->ifr_flags = tun_flags(tun); 2220 2221 } 2222 2223 /* This is like a cut-down ethtool ops, except done via tun fd so no 2224 * privs required. */ 2225 static int set_offload(struct tun_struct *tun, unsigned long arg) 2226 { 2227 netdev_features_t features = 0; 2228 2229 if (arg & TUN_F_CSUM) { 2230 features |= NETIF_F_HW_CSUM; 2231 arg &= ~TUN_F_CSUM; 2232 2233 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { 2234 if (arg & TUN_F_TSO_ECN) { 2235 features |= NETIF_F_TSO_ECN; 2236 arg &= ~TUN_F_TSO_ECN; 2237 } 2238 if (arg & TUN_F_TSO4) 2239 features |= NETIF_F_TSO; 2240 if (arg & TUN_F_TSO6) 2241 features |= NETIF_F_TSO6; 2242 arg &= ~(TUN_F_TSO4|TUN_F_TSO6); 2243 } 2244 } 2245 2246 /* This gives the user a way to test for new features in future by 2247 * trying to set them. */ 2248 if (arg) 2249 return -EINVAL; 2250 2251 tun->set_features = features; 2252 tun->dev->wanted_features &= ~TUN_USER_FEATURES; 2253 tun->dev->wanted_features |= features; 2254 netdev_update_features(tun->dev); 2255 2256 return 0; 2257 } 2258 2259 static void tun_detach_filter(struct tun_struct *tun, int n) 2260 { 2261 int i; 2262 struct tun_file *tfile; 2263 2264 for (i = 0; i < n; i++) { 2265 tfile = rtnl_dereference(tun->tfiles[i]); 2266 lock_sock(tfile->socket.sk); 2267 sk_detach_filter(tfile->socket.sk); 2268 release_sock(tfile->socket.sk); 2269 } 2270 2271 tun->filter_attached = false; 2272 } 2273 2274 static int tun_attach_filter(struct tun_struct *tun) 2275 { 2276 int i, ret = 0; 2277 struct tun_file *tfile; 2278 2279 for (i = 0; i < tun->numqueues; i++) { 2280 tfile = rtnl_dereference(tun->tfiles[i]); 2281 lock_sock(tfile->socket.sk); 2282 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); 2283 release_sock(tfile->socket.sk); 2284 if (ret) { 2285 tun_detach_filter(tun, i); 2286 return ret; 2287 } 2288 } 2289 2290 tun->filter_attached = true; 2291 return ret; 2292 } 2293 2294 static void tun_set_sndbuf(struct tun_struct *tun) 2295 { 2296 struct tun_file *tfile; 2297 int i; 2298 2299 for (i = 0; i < tun->numqueues; i++) { 2300 tfile = rtnl_dereference(tun->tfiles[i]); 2301 tfile->socket.sk->sk_sndbuf = tun->sndbuf; 2302 } 2303 } 2304 2305 static int tun_set_queue(struct file *file, struct ifreq *ifr) 2306 { 2307 struct tun_file *tfile = file->private_data; 2308 struct tun_struct *tun; 2309 int ret = 0; 2310 2311 rtnl_lock(); 2312 2313 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { 2314 tun = tfile->detached; 2315 if (!tun) { 2316 ret = -EINVAL; 2317 goto unlock; 2318 } 2319 ret = security_tun_dev_attach_queue(tun->security); 2320 if (ret < 0) 2321 goto unlock; 2322 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI); 2323 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { 2324 tun = rtnl_dereference(tfile->tun); 2325 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) 2326 ret = -EINVAL; 2327 else 2328 __tun_detach(tfile, false); 2329 } else 2330 ret = -EINVAL; 2331 2332 unlock: 2333 rtnl_unlock(); 2334 return ret; 2335 } 2336 2337 static long __tun_chr_ioctl(struct file *file, unsigned int cmd, 2338 unsigned long arg, int ifreq_len) 2339 { 2340 struct tun_file *tfile = file->private_data; 2341 struct tun_struct *tun; 2342 void __user* argp = (void __user*)arg; 2343 struct ifreq ifr; 2344 kuid_t owner; 2345 kgid_t group; 2346 int sndbuf; 2347 int vnet_hdr_sz; 2348 unsigned int ifindex; 2349 int le; 2350 int ret; 2351 2352 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { 2353 if (copy_from_user(&ifr, argp, ifreq_len)) 2354 return -EFAULT; 2355 } else { 2356 memset(&ifr, 0, sizeof(ifr)); 2357 } 2358 if (cmd == TUNGETFEATURES) { 2359 /* Currently this just means: "what IFF flags are valid?". 2360 * This is needed because we never checked for invalid flags on 2361 * TUNSETIFF. 2362 */ 2363 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, 2364 (unsigned int __user*)argp); 2365 } else if (cmd == TUNSETQUEUE) 2366 return tun_set_queue(file, &ifr); 2367 2368 ret = 0; 2369 rtnl_lock(); 2370 2371 tun = __tun_get(tfile); 2372 if (cmd == TUNSETIFF) { 2373 ret = -EEXIST; 2374 if (tun) 2375 goto unlock; 2376 2377 ifr.ifr_name[IFNAMSIZ-1] = '\0'; 2378 2379 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); 2380 2381 if (ret) 2382 goto unlock; 2383 2384 if (copy_to_user(argp, &ifr, ifreq_len)) 2385 ret = -EFAULT; 2386 goto unlock; 2387 } 2388 if (cmd == TUNSETIFINDEX) { 2389 ret = -EPERM; 2390 if (tun) 2391 goto unlock; 2392 2393 ret = -EFAULT; 2394 if (copy_from_user(&ifindex, argp, sizeof(ifindex))) 2395 goto unlock; 2396 2397 ret = 0; 2398 tfile->ifindex = ifindex; 2399 goto unlock; 2400 } 2401 2402 ret = -EBADFD; 2403 if (!tun) 2404 goto unlock; 2405 2406 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); 2407 2408 ret = 0; 2409 switch (cmd) { 2410 case TUNGETIFF: 2411 tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2412 2413 if (tfile->detached) 2414 ifr.ifr_flags |= IFF_DETACH_QUEUE; 2415 if (!tfile->socket.sk->sk_filter) 2416 ifr.ifr_flags |= IFF_NOFILTER; 2417 2418 if (copy_to_user(argp, &ifr, ifreq_len)) 2419 ret = -EFAULT; 2420 break; 2421 2422 case TUNSETNOCSUM: 2423 /* Disable/Enable checksum */ 2424 2425 /* [unimplemented] */ 2426 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", 2427 arg ? "disabled" : "enabled"); 2428 break; 2429 2430 case TUNSETPERSIST: 2431 /* Disable/Enable persist mode. Keep an extra reference to the 2432 * module to prevent the module being unprobed. 2433 */ 2434 if (arg && !(tun->flags & IFF_PERSIST)) { 2435 tun->flags |= IFF_PERSIST; 2436 __module_get(THIS_MODULE); 2437 } 2438 if (!arg && (tun->flags & IFF_PERSIST)) { 2439 tun->flags &= ~IFF_PERSIST; 2440 module_put(THIS_MODULE); 2441 } 2442 2443 tun_debug(KERN_INFO, tun, "persist %s\n", 2444 arg ? "enabled" : "disabled"); 2445 break; 2446 2447 case TUNSETOWNER: 2448 /* Set owner of the device */ 2449 owner = make_kuid(current_user_ns(), arg); 2450 if (!uid_valid(owner)) { 2451 ret = -EINVAL; 2452 break; 2453 } 2454 tun->owner = owner; 2455 tun_debug(KERN_INFO, tun, "owner set to %u\n", 2456 from_kuid(&init_user_ns, tun->owner)); 2457 break; 2458 2459 case TUNSETGROUP: 2460 /* Set group of the device */ 2461 group = make_kgid(current_user_ns(), arg); 2462 if (!gid_valid(group)) { 2463 ret = -EINVAL; 2464 break; 2465 } 2466 tun->group = group; 2467 tun_debug(KERN_INFO, tun, "group set to %u\n", 2468 from_kgid(&init_user_ns, tun->group)); 2469 break; 2470 2471 case TUNSETLINK: 2472 /* Only allow setting the type when the interface is down */ 2473 if (tun->dev->flags & IFF_UP) { 2474 tun_debug(KERN_INFO, tun, 2475 "Linktype set failed because interface is up\n"); 2476 ret = -EBUSY; 2477 } else { 2478 tun->dev->type = (int) arg; 2479 tun_debug(KERN_INFO, tun, "linktype set to %d\n", 2480 tun->dev->type); 2481 ret = 0; 2482 } 2483 break; 2484 2485 #ifdef TUN_DEBUG 2486 case TUNSETDEBUG: 2487 tun->debug = arg; 2488 break; 2489 #endif 2490 case TUNSETOFFLOAD: 2491 ret = set_offload(tun, arg); 2492 break; 2493 2494 case TUNSETTXFILTER: 2495 /* Can be set only for TAPs */ 2496 ret = -EINVAL; 2497 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2498 break; 2499 ret = update_filter(&tun->txflt, (void __user *)arg); 2500 break; 2501 2502 case SIOCGIFHWADDR: 2503 /* Get hw address */ 2504 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); 2505 ifr.ifr_hwaddr.sa_family = tun->dev->type; 2506 if (copy_to_user(argp, &ifr, ifreq_len)) 2507 ret = -EFAULT; 2508 break; 2509 2510 case SIOCSIFHWADDR: 2511 /* Set hw address */ 2512 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", 2513 ifr.ifr_hwaddr.sa_data); 2514 2515 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); 2516 break; 2517 2518 case TUNGETSNDBUF: 2519 sndbuf = tfile->socket.sk->sk_sndbuf; 2520 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) 2521 ret = -EFAULT; 2522 break; 2523 2524 case TUNSETSNDBUF: 2525 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { 2526 ret = -EFAULT; 2527 break; 2528 } 2529 2530 tun->sndbuf = sndbuf; 2531 tun_set_sndbuf(tun); 2532 break; 2533 2534 case TUNGETVNETHDRSZ: 2535 vnet_hdr_sz = tun->vnet_hdr_sz; 2536 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) 2537 ret = -EFAULT; 2538 break; 2539 2540 case TUNSETVNETHDRSZ: 2541 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { 2542 ret = -EFAULT; 2543 break; 2544 } 2545 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { 2546 ret = -EINVAL; 2547 break; 2548 } 2549 2550 tun->vnet_hdr_sz = vnet_hdr_sz; 2551 break; 2552 2553 case TUNGETVNETLE: 2554 le = !!(tun->flags & TUN_VNET_LE); 2555 if (put_user(le, (int __user *)argp)) 2556 ret = -EFAULT; 2557 break; 2558 2559 case TUNSETVNETLE: 2560 if (get_user(le, (int __user *)argp)) { 2561 ret = -EFAULT; 2562 break; 2563 } 2564 if (le) 2565 tun->flags |= TUN_VNET_LE; 2566 else 2567 tun->flags &= ~TUN_VNET_LE; 2568 break; 2569 2570 case TUNGETVNETBE: 2571 ret = tun_get_vnet_be(tun, argp); 2572 break; 2573 2574 case TUNSETVNETBE: 2575 ret = tun_set_vnet_be(tun, argp); 2576 break; 2577 2578 case TUNATTACHFILTER: 2579 /* Can be set only for TAPs */ 2580 ret = -EINVAL; 2581 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2582 break; 2583 ret = -EFAULT; 2584 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) 2585 break; 2586 2587 ret = tun_attach_filter(tun); 2588 break; 2589 2590 case TUNDETACHFILTER: 2591 /* Can be set only for TAPs */ 2592 ret = -EINVAL; 2593 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2594 break; 2595 ret = 0; 2596 tun_detach_filter(tun, tun->numqueues); 2597 break; 2598 2599 case TUNGETFILTER: 2600 ret = -EINVAL; 2601 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) 2602 break; 2603 ret = -EFAULT; 2604 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) 2605 break; 2606 ret = 0; 2607 break; 2608 2609 default: 2610 ret = -EINVAL; 2611 break; 2612 } 2613 2614 unlock: 2615 rtnl_unlock(); 2616 if (tun) 2617 tun_put(tun); 2618 return ret; 2619 } 2620 2621 static long tun_chr_ioctl(struct file *file, 2622 unsigned int cmd, unsigned long arg) 2623 { 2624 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); 2625 } 2626 2627 #ifdef CONFIG_COMPAT 2628 static long tun_chr_compat_ioctl(struct file *file, 2629 unsigned int cmd, unsigned long arg) 2630 { 2631 switch (cmd) { 2632 case TUNSETIFF: 2633 case TUNGETIFF: 2634 case TUNSETTXFILTER: 2635 case TUNGETSNDBUF: 2636 case TUNSETSNDBUF: 2637 case SIOCGIFHWADDR: 2638 case SIOCSIFHWADDR: 2639 arg = (unsigned long)compat_ptr(arg); 2640 break; 2641 default: 2642 arg = (compat_ulong_t)arg; 2643 break; 2644 } 2645 2646 /* 2647 * compat_ifreq is shorter than ifreq, so we must not access beyond 2648 * the end of that structure. All fields that are used in this 2649 * driver are compatible though, we don't need to convert the 2650 * contents. 2651 */ 2652 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); 2653 } 2654 #endif /* CONFIG_COMPAT */ 2655 2656 static int tun_chr_fasync(int fd, struct file *file, int on) 2657 { 2658 struct tun_file *tfile = file->private_data; 2659 int ret; 2660 2661 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) 2662 goto out; 2663 2664 if (on) { 2665 __f_setown(file, task_pid(current), PIDTYPE_PID, 0); 2666 tfile->flags |= TUN_FASYNC; 2667 } else 2668 tfile->flags &= ~TUN_FASYNC; 2669 ret = 0; 2670 out: 2671 return ret; 2672 } 2673 2674 static int tun_chr_open(struct inode *inode, struct file * file) 2675 { 2676 struct net *net = current->nsproxy->net_ns; 2677 struct tun_file *tfile; 2678 2679 DBG1(KERN_INFO, "tunX: tun_chr_open\n"); 2680 2681 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 2682 &tun_proto, 0); 2683 if (!tfile) 2684 return -ENOMEM; 2685 RCU_INIT_POINTER(tfile->tun, NULL); 2686 tfile->flags = 0; 2687 tfile->ifindex = 0; 2688 2689 init_waitqueue_head(&tfile->wq.wait); 2690 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); 2691 2692 tfile->socket.file = file; 2693 tfile->socket.ops = &tun_socket_ops; 2694 2695 sock_init_data(&tfile->socket, &tfile->sk); 2696 2697 tfile->sk.sk_write_space = tun_sock_write_space; 2698 tfile->sk.sk_sndbuf = INT_MAX; 2699 2700 file->private_data = tfile; 2701 INIT_LIST_HEAD(&tfile->next); 2702 2703 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); 2704 2705 return 0; 2706 } 2707 2708 static int tun_chr_close(struct inode *inode, struct file *file) 2709 { 2710 struct tun_file *tfile = file->private_data; 2711 2712 tun_detach(tfile, true); 2713 2714 return 0; 2715 } 2716 2717 #ifdef CONFIG_PROC_FS 2718 static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f) 2719 { 2720 struct tun_struct *tun; 2721 struct ifreq ifr; 2722 2723 memset(&ifr, 0, sizeof(ifr)); 2724 2725 rtnl_lock(); 2726 tun = tun_get(f); 2727 if (tun) 2728 tun_get_iff(current->nsproxy->net_ns, tun, &ifr); 2729 rtnl_unlock(); 2730 2731 if (tun) 2732 tun_put(tun); 2733 2734 seq_printf(m, "iff:\t%s\n", ifr.ifr_name); 2735 } 2736 #endif 2737 2738 static const struct file_operations tun_fops = { 2739 .owner = THIS_MODULE, 2740 .llseek = no_llseek, 2741 .read_iter = tun_chr_read_iter, 2742 .write_iter = tun_chr_write_iter, 2743 .poll = tun_chr_poll, 2744 .unlocked_ioctl = tun_chr_ioctl, 2745 #ifdef CONFIG_COMPAT 2746 .compat_ioctl = tun_chr_compat_ioctl, 2747 #endif 2748 .open = tun_chr_open, 2749 .release = tun_chr_close, 2750 .fasync = tun_chr_fasync, 2751 #ifdef CONFIG_PROC_FS 2752 .show_fdinfo = tun_chr_show_fdinfo, 2753 #endif 2754 }; 2755 2756 static struct miscdevice tun_miscdev = { 2757 .minor = TUN_MINOR, 2758 .name = "tun", 2759 .nodename = "net/tun", 2760 .fops = &tun_fops, 2761 }; 2762 2763 /* ethtool interface */ 2764 2765 static int tun_get_link_ksettings(struct net_device *dev, 2766 struct ethtool_link_ksettings *cmd) 2767 { 2768 ethtool_link_ksettings_zero_link_mode(cmd, supported); 2769 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 2770 cmd->base.speed = SPEED_10; 2771 cmd->base.duplex = DUPLEX_FULL; 2772 cmd->base.port = PORT_TP; 2773 cmd->base.phy_address = 0; 2774 cmd->base.autoneg = AUTONEG_DISABLE; 2775 return 0; 2776 } 2777 2778 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 2779 { 2780 struct tun_struct *tun = netdev_priv(dev); 2781 2782 strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 2783 strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 2784 2785 switch (tun->flags & TUN_TYPE_MASK) { 2786 case IFF_TUN: 2787 strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); 2788 break; 2789 case IFF_TAP: 2790 strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); 2791 break; 2792 } 2793 } 2794 2795 static u32 tun_get_msglevel(struct net_device *dev) 2796 { 2797 #ifdef TUN_DEBUG 2798 struct tun_struct *tun = netdev_priv(dev); 2799 return tun->debug; 2800 #else 2801 return -EOPNOTSUPP; 2802 #endif 2803 } 2804 2805 static void tun_set_msglevel(struct net_device *dev, u32 value) 2806 { 2807 #ifdef TUN_DEBUG 2808 struct tun_struct *tun = netdev_priv(dev); 2809 tun->debug = value; 2810 #endif 2811 } 2812 2813 static int tun_get_coalesce(struct net_device *dev, 2814 struct ethtool_coalesce *ec) 2815 { 2816 struct tun_struct *tun = netdev_priv(dev); 2817 2818 ec->rx_max_coalesced_frames = tun->rx_batched; 2819 2820 return 0; 2821 } 2822 2823 static int tun_set_coalesce(struct net_device *dev, 2824 struct ethtool_coalesce *ec) 2825 { 2826 struct tun_struct *tun = netdev_priv(dev); 2827 2828 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) 2829 tun->rx_batched = NAPI_POLL_WEIGHT; 2830 else 2831 tun->rx_batched = ec->rx_max_coalesced_frames; 2832 2833 return 0; 2834 } 2835 2836 static const struct ethtool_ops tun_ethtool_ops = { 2837 .get_drvinfo = tun_get_drvinfo, 2838 .get_msglevel = tun_get_msglevel, 2839 .set_msglevel = tun_set_msglevel, 2840 .get_link = ethtool_op_get_link, 2841 .get_ts_info = ethtool_op_get_ts_info, 2842 .get_coalesce = tun_get_coalesce, 2843 .set_coalesce = tun_set_coalesce, 2844 .get_link_ksettings = tun_get_link_ksettings, 2845 }; 2846 2847 static int tun_queue_resize(struct tun_struct *tun) 2848 { 2849 struct net_device *dev = tun->dev; 2850 struct tun_file *tfile; 2851 struct skb_array **arrays; 2852 int n = tun->numqueues + tun->numdisabled; 2853 int ret, i; 2854 2855 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL); 2856 if (!arrays) 2857 return -ENOMEM; 2858 2859 for (i = 0; i < tun->numqueues; i++) { 2860 tfile = rtnl_dereference(tun->tfiles[i]); 2861 arrays[i] = &tfile->tx_array; 2862 } 2863 list_for_each_entry(tfile, &tun->disabled, next) 2864 arrays[i++] = &tfile->tx_array; 2865 2866 ret = skb_array_resize_multiple(arrays, n, 2867 dev->tx_queue_len, GFP_KERNEL); 2868 2869 kfree(arrays); 2870 return ret; 2871 } 2872 2873 static int tun_device_event(struct notifier_block *unused, 2874 unsigned long event, void *ptr) 2875 { 2876 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2877 struct tun_struct *tun = netdev_priv(dev); 2878 2879 if (dev->rtnl_link_ops != &tun_link_ops) 2880 return NOTIFY_DONE; 2881 2882 switch (event) { 2883 case NETDEV_CHANGE_TX_QUEUE_LEN: 2884 if (tun_queue_resize(tun)) 2885 return NOTIFY_BAD; 2886 break; 2887 default: 2888 break; 2889 } 2890 2891 return NOTIFY_DONE; 2892 } 2893 2894 static struct notifier_block tun_notifier_block __read_mostly = { 2895 .notifier_call = tun_device_event, 2896 }; 2897 2898 static int __init tun_init(void) 2899 { 2900 int ret = 0; 2901 2902 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); 2903 2904 ret = rtnl_link_register(&tun_link_ops); 2905 if (ret) { 2906 pr_err("Can't register link_ops\n"); 2907 goto err_linkops; 2908 } 2909 2910 ret = misc_register(&tun_miscdev); 2911 if (ret) { 2912 pr_err("Can't register misc device %d\n", TUN_MINOR); 2913 goto err_misc; 2914 } 2915 2916 ret = register_netdevice_notifier(&tun_notifier_block); 2917 if (ret) { 2918 pr_err("Can't register netdevice notifier\n"); 2919 goto err_notifier; 2920 } 2921 2922 return 0; 2923 2924 err_notifier: 2925 misc_deregister(&tun_miscdev); 2926 err_misc: 2927 rtnl_link_unregister(&tun_link_ops); 2928 err_linkops: 2929 return ret; 2930 } 2931 2932 static void tun_cleanup(void) 2933 { 2934 misc_deregister(&tun_miscdev); 2935 rtnl_link_unregister(&tun_link_ops); 2936 unregister_netdevice_notifier(&tun_notifier_block); 2937 } 2938 2939 /* Get an underlying socket object from tun file. Returns error unless file is 2940 * attached to a device. The returned object works like a packet socket, it 2941 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 2942 * holding a reference to the file for as long as the socket is in use. */ 2943 struct socket *tun_get_socket(struct file *file) 2944 { 2945 struct tun_file *tfile; 2946 if (file->f_op != &tun_fops) 2947 return ERR_PTR(-EINVAL); 2948 tfile = file->private_data; 2949 if (!tfile) 2950 return ERR_PTR(-EBADFD); 2951 return &tfile->socket; 2952 } 2953 EXPORT_SYMBOL_GPL(tun_get_socket); 2954 2955 struct skb_array *tun_get_skb_array(struct file *file) 2956 { 2957 struct tun_file *tfile; 2958 2959 if (file->f_op != &tun_fops) 2960 return ERR_PTR(-EINVAL); 2961 tfile = file->private_data; 2962 if (!tfile) 2963 return ERR_PTR(-EBADFD); 2964 return &tfile->tx_array; 2965 } 2966 EXPORT_SYMBOL_GPL(tun_get_skb_array); 2967 2968 module_init(tun_init); 2969 module_exit(tun_cleanup); 2970 MODULE_DESCRIPTION(DRV_DESCRIPTION); 2971 MODULE_AUTHOR(DRV_COPYRIGHT); 2972 MODULE_LICENSE("GPL"); 2973 MODULE_ALIAS_MISCDEV(TUN_MINOR); 2974 MODULE_ALIAS("devname:net/tun"); 2975