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