ARM: mm: Recreate kernel mappings in early_paging_init()
[linux/fpc-iii.git] / drivers / net / tun.c
blob807815fc996839d14efd18625fb300a2f48d2577
1 /*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
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.
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.
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
19 * Changes:
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
24 * Mark Smith <markzzzsmith@yahoo.com.au>
25 * Use eth_random_addr() for tap MAC address.
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
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>"
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/if_vlan.h>
64 #include <linux/crc32.h>
65 #include <linux/nsproxy.h>
66 #include <linux/virtio_net.h>
67 #include <linux/rcupdate.h>
68 #include <net/net_namespace.h>
69 #include <net/netns/generic.h>
70 #include <net/rtnetlink.h>
71 #include <net/sock.h>
73 #include <asm/uaccess.h>
75 /* Uncomment to enable debugging */
76 /* #define TUN_DEBUG 1 */
78 #ifdef TUN_DEBUG
79 static int debug;
81 #define tun_debug(level, tun, fmt, args...) \
82 do { \
83 if (tun->debug) \
84 netdev_printk(level, tun->dev, fmt, ##args); \
85 } while (0)
86 #define DBG1(level, fmt, args...) \
87 do { \
88 if (debug == 2) \
89 printk(level fmt, ##args); \
90 } while (0)
91 #else
92 #define tun_debug(level, tun, fmt, args...) \
93 do { \
94 if (0) \
95 netdev_printk(level, tun->dev, fmt, ##args); \
96 } while (0)
97 #define DBG1(level, fmt, args...) \
98 do { \
99 if (0) \
100 printk(level fmt, ##args); \
101 } while (0)
102 #endif
104 #define GOODCOPY_LEN 128
106 #define FLT_EXACT_COUNT 8
107 struct tap_filter {
108 unsigned int count; /* Number of addrs. Zero means disabled */
109 u32 mask[2]; /* Mask of the hashed addrs */
110 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
113 /* DEFAULT_MAX_NUM_RSS_QUEUES were choosed to let the rx/tx queues allocated for
114 * the netdevice to be fit in one page. So we can make sure the success of
115 * memory allocation. TODO: increase the limit. */
116 #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
117 #define MAX_TAP_FLOWS 4096
119 #define TUN_FLOW_EXPIRE (3 * HZ)
121 /* A tun_file connects an open character device to a tuntap netdevice. It
122 * also contains all socket related strctures (except sock_fprog and tap_filter)
123 * to serve as one transmit queue for tuntap device. The sock_fprog and
124 * tap_filter were kept in tun_struct since they were used for filtering for the
125 * netdevice not for a specific queue (at least I didn't see the requirement for
126 * this).
128 * RCU usage:
129 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
130 * other can only be read while rcu_read_lock or rtnl_lock is held.
132 struct tun_file {
133 struct sock sk;
134 struct socket socket;
135 struct socket_wq wq;
136 struct tun_struct __rcu *tun;
137 struct net *net;
138 struct fasync_struct *fasync;
139 /* only used for fasnyc */
140 unsigned int flags;
141 union {
142 u16 queue_index;
143 unsigned int ifindex;
145 struct list_head next;
146 struct tun_struct *detached;
149 struct tun_flow_entry {
150 struct hlist_node hash_link;
151 struct rcu_head rcu;
152 struct tun_struct *tun;
154 u32 rxhash;
155 int queue_index;
156 unsigned long updated;
159 #define TUN_NUM_FLOW_ENTRIES 1024
161 /* Since the socket were moved to tun_file, to preserve the behavior of persist
162 * device, socket filter, sndbuf and vnet header size were restore when the
163 * file were attached to a persist device.
165 struct tun_struct {
166 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
167 unsigned int numqueues;
168 unsigned int flags;
169 kuid_t owner;
170 kgid_t group;
172 struct net_device *dev;
173 netdev_features_t set_features;
174 #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
175 NETIF_F_TSO6|NETIF_F_UFO)
177 int vnet_hdr_sz;
178 int sndbuf;
179 struct tap_filter txflt;
180 struct sock_fprog fprog;
181 /* protected by rtnl lock */
182 bool filter_attached;
183 #ifdef TUN_DEBUG
184 int debug;
185 #endif
186 spinlock_t lock;
187 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
188 struct timer_list flow_gc_timer;
189 unsigned long ageing_time;
190 unsigned int numdisabled;
191 struct list_head disabled;
192 void *security;
193 u32 flow_count;
196 static inline u32 tun_hashfn(u32 rxhash)
198 return rxhash & 0x3ff;
201 static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
203 struct tun_flow_entry *e;
205 hlist_for_each_entry_rcu(e, head, hash_link) {
206 if (e->rxhash == rxhash)
207 return e;
209 return NULL;
212 static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
213 struct hlist_head *head,
214 u32 rxhash, u16 queue_index)
216 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
218 if (e) {
219 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
220 rxhash, queue_index);
221 e->updated = jiffies;
222 e->rxhash = rxhash;
223 e->queue_index = queue_index;
224 e->tun = tun;
225 hlist_add_head_rcu(&e->hash_link, head);
226 ++tun->flow_count;
228 return e;
231 static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
233 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
234 e->rxhash, e->queue_index);
235 hlist_del_rcu(&e->hash_link);
236 kfree_rcu(e, rcu);
237 --tun->flow_count;
240 static void tun_flow_flush(struct tun_struct *tun)
242 int i;
244 spin_lock_bh(&tun->lock);
245 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
246 struct tun_flow_entry *e;
247 struct hlist_node *n;
249 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
250 tun_flow_delete(tun, e);
252 spin_unlock_bh(&tun->lock);
255 static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
257 int i;
259 spin_lock_bh(&tun->lock);
260 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
261 struct tun_flow_entry *e;
262 struct hlist_node *n;
264 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
265 if (e->queue_index == queue_index)
266 tun_flow_delete(tun, e);
269 spin_unlock_bh(&tun->lock);
272 static void tun_flow_cleanup(unsigned long data)
274 struct tun_struct *tun = (struct tun_struct *)data;
275 unsigned long delay = tun->ageing_time;
276 unsigned long next_timer = jiffies + delay;
277 unsigned long count = 0;
278 int i;
280 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
282 spin_lock_bh(&tun->lock);
283 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
284 struct tun_flow_entry *e;
285 struct hlist_node *n;
287 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
288 unsigned long this_timer;
289 count++;
290 this_timer = e->updated + delay;
291 if (time_before_eq(this_timer, jiffies))
292 tun_flow_delete(tun, e);
293 else if (time_before(this_timer, next_timer))
294 next_timer = this_timer;
298 if (count)
299 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
300 spin_unlock_bh(&tun->lock);
303 static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
304 struct tun_file *tfile)
306 struct hlist_head *head;
307 struct tun_flow_entry *e;
308 unsigned long delay = tun->ageing_time;
309 u16 queue_index = tfile->queue_index;
311 if (!rxhash)
312 return;
313 else
314 head = &tun->flows[tun_hashfn(rxhash)];
316 rcu_read_lock();
318 /* We may get a very small possibility of OOO during switching, not
319 * worth to optimize.*/
320 if (tun->numqueues == 1 || tfile->detached)
321 goto unlock;
323 e = tun_flow_find(head, rxhash);
324 if (likely(e)) {
325 /* TODO: keep queueing to old queue until it's empty? */
326 e->queue_index = queue_index;
327 e->updated = jiffies;
328 } else {
329 spin_lock_bh(&tun->lock);
330 if (!tun_flow_find(head, rxhash) &&
331 tun->flow_count < MAX_TAP_FLOWS)
332 tun_flow_create(tun, head, rxhash, queue_index);
334 if (!timer_pending(&tun->flow_gc_timer))
335 mod_timer(&tun->flow_gc_timer,
336 round_jiffies_up(jiffies + delay));
337 spin_unlock_bh(&tun->lock);
340 unlock:
341 rcu_read_unlock();
344 /* We try to identify a flow through its rxhash first. The reason that
345 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
346 * the rxq based on the txq where the last packet of the flow comes. As
347 * the userspace application move between processors, we may get a
348 * different rxq no. here. If we could not get rxhash, then we would
349 * hope the rxq no. may help here.
351 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
353 struct tun_struct *tun = netdev_priv(dev);
354 struct tun_flow_entry *e;
355 u32 txq = 0;
356 u32 numqueues = 0;
358 rcu_read_lock();
359 numqueues = ACCESS_ONCE(tun->numqueues);
361 txq = skb_get_rxhash(skb);
362 if (txq) {
363 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
364 if (e)
365 txq = e->queue_index;
366 else
367 /* use multiply and shift instead of expensive divide */
368 txq = ((u64)txq * numqueues) >> 32;
369 } else if (likely(skb_rx_queue_recorded(skb))) {
370 txq = skb_get_rx_queue(skb);
371 while (unlikely(txq >= numqueues))
372 txq -= numqueues;
375 rcu_read_unlock();
376 return txq;
379 static inline bool tun_not_capable(struct tun_struct *tun)
381 const struct cred *cred = current_cred();
382 struct net *net = dev_net(tun->dev);
384 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
385 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
386 !ns_capable(net->user_ns, CAP_NET_ADMIN);
389 static void tun_set_real_num_queues(struct tun_struct *tun)
391 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
392 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
395 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
397 tfile->detached = tun;
398 list_add_tail(&tfile->next, &tun->disabled);
399 ++tun->numdisabled;
402 static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
404 struct tun_struct *tun = tfile->detached;
406 tfile->detached = NULL;
407 list_del_init(&tfile->next);
408 --tun->numdisabled;
409 return tun;
412 static void tun_queue_purge(struct tun_file *tfile)
414 skb_queue_purge(&tfile->sk.sk_receive_queue);
415 skb_queue_purge(&tfile->sk.sk_error_queue);
418 static void __tun_detach(struct tun_file *tfile, bool clean)
420 struct tun_file *ntfile;
421 struct tun_struct *tun;
423 tun = rtnl_dereference(tfile->tun);
425 if (tun && !tfile->detached) {
426 u16 index = tfile->queue_index;
427 BUG_ON(index >= tun->numqueues);
429 rcu_assign_pointer(tun->tfiles[index],
430 tun->tfiles[tun->numqueues - 1]);
431 ntfile = rtnl_dereference(tun->tfiles[index]);
432 ntfile->queue_index = index;
434 --tun->numqueues;
435 if (clean) {
436 rcu_assign_pointer(tfile->tun, NULL);
437 sock_put(&tfile->sk);
438 } else
439 tun_disable_queue(tun, tfile);
441 synchronize_net();
442 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
443 /* Drop read queue */
444 tun_queue_purge(tfile);
445 tun_set_real_num_queues(tun);
446 } else if (tfile->detached && clean) {
447 tun = tun_enable_queue(tfile);
448 sock_put(&tfile->sk);
451 if (clean) {
452 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
453 netif_carrier_off(tun->dev);
455 if (!(tun->flags & TUN_PERSIST) &&
456 tun->dev->reg_state == NETREG_REGISTERED)
457 unregister_netdevice(tun->dev);
460 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
461 &tfile->socket.flags));
462 sk_release_kernel(&tfile->sk);
466 static void tun_detach(struct tun_file *tfile, bool clean)
468 rtnl_lock();
469 __tun_detach(tfile, clean);
470 rtnl_unlock();
473 static void tun_detach_all(struct net_device *dev)
475 struct tun_struct *tun = netdev_priv(dev);
476 struct tun_file *tfile, *tmp;
477 int i, n = tun->numqueues;
479 for (i = 0; i < n; i++) {
480 tfile = rtnl_dereference(tun->tfiles[i]);
481 BUG_ON(!tfile);
482 wake_up_all(&tfile->wq.wait);
483 rcu_assign_pointer(tfile->tun, NULL);
484 --tun->numqueues;
486 list_for_each_entry(tfile, &tun->disabled, next) {
487 wake_up_all(&tfile->wq.wait);
488 rcu_assign_pointer(tfile->tun, NULL);
490 BUG_ON(tun->numqueues != 0);
492 synchronize_net();
493 for (i = 0; i < n; i++) {
494 tfile = rtnl_dereference(tun->tfiles[i]);
495 /* Drop read queue */
496 tun_queue_purge(tfile);
497 sock_put(&tfile->sk);
499 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
500 tun_enable_queue(tfile);
501 tun_queue_purge(tfile);
502 sock_put(&tfile->sk);
504 BUG_ON(tun->numdisabled != 0);
506 if (tun->flags & TUN_PERSIST)
507 module_put(THIS_MODULE);
510 static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
512 struct tun_file *tfile = file->private_data;
513 int err;
515 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
516 if (err < 0)
517 goto out;
519 err = -EINVAL;
520 if (rtnl_dereference(tfile->tun) && !tfile->detached)
521 goto out;
523 err = -EBUSY;
524 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
525 goto out;
527 err = -E2BIG;
528 if (!tfile->detached &&
529 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
530 goto out;
532 err = 0;
534 /* Re-attach the filter to presist device */
535 if (!skip_filter && (tun->filter_attached == true)) {
536 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
537 if (!err)
538 goto out;
540 tfile->queue_index = tun->numqueues;
541 rcu_assign_pointer(tfile->tun, tun);
542 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
543 tun->numqueues++;
545 if (tfile->detached)
546 tun_enable_queue(tfile);
547 else
548 sock_hold(&tfile->sk);
550 tun_set_real_num_queues(tun);
552 /* device is allowed to go away first, so no need to hold extra
553 * refcnt.
556 out:
557 return err;
560 static struct tun_struct *__tun_get(struct tun_file *tfile)
562 struct tun_struct *tun;
564 rcu_read_lock();
565 tun = rcu_dereference(tfile->tun);
566 if (tun)
567 dev_hold(tun->dev);
568 rcu_read_unlock();
570 return tun;
573 static struct tun_struct *tun_get(struct file *file)
575 return __tun_get(file->private_data);
578 static void tun_put(struct tun_struct *tun)
580 dev_put(tun->dev);
583 /* TAP filtering */
584 static void addr_hash_set(u32 *mask, const u8 *addr)
586 int n = ether_crc(ETH_ALEN, addr) >> 26;
587 mask[n >> 5] |= (1 << (n & 31));
590 static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
592 int n = ether_crc(ETH_ALEN, addr) >> 26;
593 return mask[n >> 5] & (1 << (n & 31));
596 static int update_filter(struct tap_filter *filter, void __user *arg)
598 struct { u8 u[ETH_ALEN]; } *addr;
599 struct tun_filter uf;
600 int err, alen, n, nexact;
602 if (copy_from_user(&uf, arg, sizeof(uf)))
603 return -EFAULT;
605 if (!uf.count) {
606 /* Disabled */
607 filter->count = 0;
608 return 0;
611 alen = ETH_ALEN * uf.count;
612 addr = kmalloc(alen, GFP_KERNEL);
613 if (!addr)
614 return -ENOMEM;
616 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
617 err = -EFAULT;
618 goto done;
621 /* The filter is updated without holding any locks. Which is
622 * perfectly safe. We disable it first and in the worst
623 * case we'll accept a few undesired packets. */
624 filter->count = 0;
625 wmb();
627 /* Use first set of addresses as an exact filter */
628 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
629 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
631 nexact = n;
633 /* Remaining multicast addresses are hashed,
634 * unicast will leave the filter disabled. */
635 memset(filter->mask, 0, sizeof(filter->mask));
636 for (; n < uf.count; n++) {
637 if (!is_multicast_ether_addr(addr[n].u)) {
638 err = 0; /* no filter */
639 goto done;
641 addr_hash_set(filter->mask, addr[n].u);
644 /* For ALLMULTI just set the mask to all ones.
645 * This overrides the mask populated above. */
646 if ((uf.flags & TUN_FLT_ALLMULTI))
647 memset(filter->mask, ~0, sizeof(filter->mask));
649 /* Now enable the filter */
650 wmb();
651 filter->count = nexact;
653 /* Return the number of exact filters */
654 err = nexact;
656 done:
657 kfree(addr);
658 return err;
661 /* Returns: 0 - drop, !=0 - accept */
662 static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
664 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
665 * at this point. */
666 struct ethhdr *eh = (struct ethhdr *) skb->data;
667 int i;
669 /* Exact match */
670 for (i = 0; i < filter->count; i++)
671 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
672 return 1;
674 /* Inexact match (multicast only) */
675 if (is_multicast_ether_addr(eh->h_dest))
676 return addr_hash_test(filter->mask, eh->h_dest);
678 return 0;
682 * Checks whether the packet is accepted or not.
683 * Returns: 0 - drop, !=0 - accept
685 static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
687 if (!filter->count)
688 return 1;
690 return run_filter(filter, skb);
693 /* Network device part of the driver */
695 static const struct ethtool_ops tun_ethtool_ops;
697 /* Net device detach from fd. */
698 static void tun_net_uninit(struct net_device *dev)
700 tun_detach_all(dev);
703 /* Net device open. */
704 static int tun_net_open(struct net_device *dev)
706 netif_tx_start_all_queues(dev);
707 return 0;
710 /* Net device close. */
711 static int tun_net_close(struct net_device *dev)
713 netif_tx_stop_all_queues(dev);
714 return 0;
717 /* Net device start xmit */
718 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
720 struct tun_struct *tun = netdev_priv(dev);
721 int txq = skb->queue_mapping;
722 struct tun_file *tfile;
724 rcu_read_lock();
725 tfile = rcu_dereference(tun->tfiles[txq]);
727 /* Drop packet if interface is not attached */
728 if (txq >= tun->numqueues)
729 goto drop;
731 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
733 BUG_ON(!tfile);
735 /* Drop if the filter does not like it.
736 * This is a noop if the filter is disabled.
737 * Filter can be enabled only for the TAP devices. */
738 if (!check_filter(&tun->txflt, skb))
739 goto drop;
741 if (tfile->socket.sk->sk_filter &&
742 sk_filter(tfile->socket.sk, skb))
743 goto drop;
745 /* Limit the number of packets queued by dividing txq length with the
746 * number of queues.
748 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
749 >= dev->tx_queue_len / tun->numqueues)
750 goto drop;
752 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
753 goto drop;
755 if (skb->sk) {
756 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
757 sw_tx_timestamp(skb);
760 /* Orphan the skb - required as we might hang on to it
761 * for indefinite time.
763 skb_orphan(skb);
765 nf_reset(skb);
767 /* Enqueue packet */
768 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
770 /* Notify and wake up reader process */
771 if (tfile->flags & TUN_FASYNC)
772 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
773 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
774 POLLRDNORM | POLLRDBAND);
776 rcu_read_unlock();
777 return NETDEV_TX_OK;
779 drop:
780 dev->stats.tx_dropped++;
781 skb_tx_error(skb);
782 kfree_skb(skb);
783 rcu_read_unlock();
784 return NETDEV_TX_OK;
787 static void tun_net_mclist(struct net_device *dev)
790 * This callback is supposed to deal with mc filter in
791 * _rx_ path and has nothing to do with the _tx_ path.
792 * In rx path we always accept everything userspace gives us.
796 #define MIN_MTU 68
797 #define MAX_MTU 65535
799 static int
800 tun_net_change_mtu(struct net_device *dev, int new_mtu)
802 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
803 return -EINVAL;
804 dev->mtu = new_mtu;
805 return 0;
808 static netdev_features_t tun_net_fix_features(struct net_device *dev,
809 netdev_features_t features)
811 struct tun_struct *tun = netdev_priv(dev);
813 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
815 #ifdef CONFIG_NET_POLL_CONTROLLER
816 static void tun_poll_controller(struct net_device *dev)
819 * Tun only receives frames when:
820 * 1) the char device endpoint gets data from user space
821 * 2) the tun socket gets a sendmsg call from user space
822 * Since both of those are syncronous operations, we are guaranteed
823 * never to have pending data when we poll for it
824 * so theres nothing to do here but return.
825 * We need this though so netpoll recognizes us as an interface that
826 * supports polling, which enables bridge devices in virt setups to
827 * still use netconsole
829 return;
831 #endif
832 static const struct net_device_ops tun_netdev_ops = {
833 .ndo_uninit = tun_net_uninit,
834 .ndo_open = tun_net_open,
835 .ndo_stop = tun_net_close,
836 .ndo_start_xmit = tun_net_xmit,
837 .ndo_change_mtu = tun_net_change_mtu,
838 .ndo_fix_features = tun_net_fix_features,
839 .ndo_select_queue = tun_select_queue,
840 #ifdef CONFIG_NET_POLL_CONTROLLER
841 .ndo_poll_controller = tun_poll_controller,
842 #endif
845 static const struct net_device_ops tap_netdev_ops = {
846 .ndo_uninit = tun_net_uninit,
847 .ndo_open = tun_net_open,
848 .ndo_stop = tun_net_close,
849 .ndo_start_xmit = tun_net_xmit,
850 .ndo_change_mtu = tun_net_change_mtu,
851 .ndo_fix_features = tun_net_fix_features,
852 .ndo_set_rx_mode = tun_net_mclist,
853 .ndo_set_mac_address = eth_mac_addr,
854 .ndo_validate_addr = eth_validate_addr,
855 .ndo_select_queue = tun_select_queue,
856 #ifdef CONFIG_NET_POLL_CONTROLLER
857 .ndo_poll_controller = tun_poll_controller,
858 #endif
861 static void tun_flow_init(struct tun_struct *tun)
863 int i;
865 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
866 INIT_HLIST_HEAD(&tun->flows[i]);
868 tun->ageing_time = TUN_FLOW_EXPIRE;
869 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
870 mod_timer(&tun->flow_gc_timer,
871 round_jiffies_up(jiffies + tun->ageing_time));
874 static void tun_flow_uninit(struct tun_struct *tun)
876 del_timer_sync(&tun->flow_gc_timer);
877 tun_flow_flush(tun);
880 /* Initialize net device. */
881 static void tun_net_init(struct net_device *dev)
883 struct tun_struct *tun = netdev_priv(dev);
885 switch (tun->flags & TUN_TYPE_MASK) {
886 case TUN_TUN_DEV:
887 dev->netdev_ops = &tun_netdev_ops;
889 /* Point-to-Point TUN Device */
890 dev->hard_header_len = 0;
891 dev->addr_len = 0;
892 dev->mtu = 1500;
894 /* Zero header length */
895 dev->type = ARPHRD_NONE;
896 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
897 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
898 break;
900 case TUN_TAP_DEV:
901 dev->netdev_ops = &tap_netdev_ops;
902 /* Ethernet TAP Device */
903 ether_setup(dev);
904 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
905 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
907 eth_hw_addr_random(dev);
909 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
910 break;
914 /* Character device part */
916 /* Poll */
917 static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
919 struct tun_file *tfile = file->private_data;
920 struct tun_struct *tun = __tun_get(tfile);
921 struct sock *sk;
922 unsigned int mask = 0;
924 if (!tun)
925 return POLLERR;
927 sk = tfile->socket.sk;
929 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
931 poll_wait(file, &tfile->wq.wait, wait);
933 if (!skb_queue_empty(&sk->sk_receive_queue))
934 mask |= POLLIN | POLLRDNORM;
936 if (sock_writeable(sk) ||
937 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
938 sock_writeable(sk)))
939 mask |= POLLOUT | POLLWRNORM;
941 if (tun->dev->reg_state != NETREG_REGISTERED)
942 mask = POLLERR;
944 tun_put(tun);
945 return mask;
948 /* prepad is the amount to reserve at front. len is length after that.
949 * linear is a hint as to how much to copy (usually headers). */
950 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
951 size_t prepad, size_t len,
952 size_t linear, int noblock)
954 struct sock *sk = tfile->socket.sk;
955 struct sk_buff *skb;
956 int err;
958 /* Under a page? Don't bother with paged skb. */
959 if (prepad + len < PAGE_SIZE || !linear)
960 linear = len;
962 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
963 &err, 0);
964 if (!skb)
965 return ERR_PTR(err);
967 skb_reserve(skb, prepad);
968 skb_put(skb, linear);
969 skb->data_len = len - linear;
970 skb->len += len - linear;
972 return skb;
975 /* Get packet from user space buffer */
976 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
977 void *msg_control, const struct iovec *iv,
978 size_t total_len, size_t count, int noblock)
980 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
981 struct sk_buff *skb;
982 size_t len = total_len, align = NET_SKB_PAD, linear;
983 struct virtio_net_hdr gso = { 0 };
984 int offset = 0;
985 int copylen;
986 bool zerocopy = false;
987 int err;
988 u32 rxhash;
990 if (!(tun->flags & TUN_NO_PI)) {
991 if (len < sizeof(pi))
992 return -EINVAL;
993 len -= sizeof(pi);
995 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
996 return -EFAULT;
997 offset += sizeof(pi);
1000 if (tun->flags & TUN_VNET_HDR) {
1001 if (len < tun->vnet_hdr_sz)
1002 return -EINVAL;
1003 len -= tun->vnet_hdr_sz;
1005 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
1006 return -EFAULT;
1008 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1009 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1010 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1012 if (gso.hdr_len > len)
1013 return -EINVAL;
1014 offset += tun->vnet_hdr_sz;
1017 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
1018 align += NET_IP_ALIGN;
1019 if (unlikely(len < ETH_HLEN ||
1020 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
1021 return -EINVAL;
1024 if (msg_control) {
1025 /* There are 256 bytes to be copied in skb, so there is
1026 * enough room for skb expand head in case it is used.
1027 * The rest of the buffer is mapped from userspace.
1029 copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
1030 linear = copylen;
1031 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1032 zerocopy = true;
1035 if (!zerocopy) {
1036 copylen = len;
1037 linear = gso.hdr_len;
1040 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
1041 if (IS_ERR(skb)) {
1042 if (PTR_ERR(skb) != -EAGAIN)
1043 tun->dev->stats.rx_dropped++;
1044 return PTR_ERR(skb);
1047 if (zerocopy)
1048 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
1049 else {
1050 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
1051 if (!err && msg_control) {
1052 struct ubuf_info *uarg = msg_control;
1053 uarg->callback(uarg, false);
1057 if (err) {
1058 tun->dev->stats.rx_dropped++;
1059 kfree_skb(skb);
1060 return -EFAULT;
1063 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1064 if (!skb_partial_csum_set(skb, gso.csum_start,
1065 gso.csum_offset)) {
1066 tun->dev->stats.rx_frame_errors++;
1067 kfree_skb(skb);
1068 return -EINVAL;
1072 switch (tun->flags & TUN_TYPE_MASK) {
1073 case TUN_TUN_DEV:
1074 if (tun->flags & TUN_NO_PI) {
1075 switch (skb->data[0] & 0xf0) {
1076 case 0x40:
1077 pi.proto = htons(ETH_P_IP);
1078 break;
1079 case 0x60:
1080 pi.proto = htons(ETH_P_IPV6);
1081 break;
1082 default:
1083 tun->dev->stats.rx_dropped++;
1084 kfree_skb(skb);
1085 return -EINVAL;
1089 skb_reset_mac_header(skb);
1090 skb->protocol = pi.proto;
1091 skb->dev = tun->dev;
1092 break;
1093 case TUN_TAP_DEV:
1094 skb->protocol = eth_type_trans(skb, tun->dev);
1095 break;
1098 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1099 pr_debug("GSO!\n");
1100 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1101 case VIRTIO_NET_HDR_GSO_TCPV4:
1102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1103 break;
1104 case VIRTIO_NET_HDR_GSO_TCPV6:
1105 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1106 break;
1107 case VIRTIO_NET_HDR_GSO_UDP:
1108 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1109 break;
1110 default:
1111 tun->dev->stats.rx_frame_errors++;
1112 kfree_skb(skb);
1113 return -EINVAL;
1116 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1119 skb_shinfo(skb)->gso_size = gso.gso_size;
1120 if (skb_shinfo(skb)->gso_size == 0) {
1121 tun->dev->stats.rx_frame_errors++;
1122 kfree_skb(skb);
1123 return -EINVAL;
1126 /* Header must be checked, and gso_segs computed. */
1127 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1128 skb_shinfo(skb)->gso_segs = 0;
1131 /* copy skb_ubuf_info for callback when skb has no error */
1132 if (zerocopy) {
1133 skb_shinfo(skb)->destructor_arg = msg_control;
1134 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1135 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1138 skb_reset_network_header(skb);
1139 skb_probe_transport_header(skb, 0);
1141 rxhash = skb_get_rxhash(skb);
1142 netif_rx_ni(skb);
1144 tun->dev->stats.rx_packets++;
1145 tun->dev->stats.rx_bytes += len;
1147 tun_flow_update(tun, rxhash, tfile);
1148 return total_len;
1151 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1152 unsigned long count, loff_t pos)
1154 struct file *file = iocb->ki_filp;
1155 struct tun_struct *tun = tun_get(file);
1156 struct tun_file *tfile = file->private_data;
1157 ssize_t result;
1159 if (!tun)
1160 return -EBADFD;
1162 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
1164 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1165 count, file->f_flags & O_NONBLOCK);
1167 tun_put(tun);
1168 return result;
1171 /* Put packet to the user space buffer */
1172 static ssize_t tun_put_user(struct tun_struct *tun,
1173 struct tun_file *tfile,
1174 struct sk_buff *skb,
1175 const struct iovec *iv, int len)
1177 struct tun_pi pi = { 0, skb->protocol };
1178 ssize_t total = 0;
1179 int vlan_offset = 0;
1181 if (!(tun->flags & TUN_NO_PI)) {
1182 if ((len -= sizeof(pi)) < 0)
1183 return -EINVAL;
1185 if (len < skb->len) {
1186 /* Packet will be striped */
1187 pi.flags |= TUN_PKT_STRIP;
1190 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
1191 return -EFAULT;
1192 total += sizeof(pi);
1195 if (tun->flags & TUN_VNET_HDR) {
1196 struct virtio_net_hdr gso = { 0 }; /* no info leak */
1197 if ((len -= tun->vnet_hdr_sz) < 0)
1198 return -EINVAL;
1200 if (skb_is_gso(skb)) {
1201 struct skb_shared_info *sinfo = skb_shinfo(skb);
1203 /* This is a hint as to how much should be linear. */
1204 gso.hdr_len = skb_headlen(skb);
1205 gso.gso_size = sinfo->gso_size;
1206 if (sinfo->gso_type & SKB_GSO_TCPV4)
1207 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1208 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1209 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1210 else if (sinfo->gso_type & SKB_GSO_UDP)
1211 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1212 else {
1213 pr_err("unexpected GSO type: "
1214 "0x%x, gso_size %d, hdr_len %d\n",
1215 sinfo->gso_type, gso.gso_size,
1216 gso.hdr_len);
1217 print_hex_dump(KERN_ERR, "tun: ",
1218 DUMP_PREFIX_NONE,
1219 16, 1, skb->head,
1220 min((int)gso.hdr_len, 64), true);
1221 WARN_ON_ONCE(1);
1222 return -EINVAL;
1224 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1225 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1226 } else
1227 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1229 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1230 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1231 gso.csum_start = skb_checksum_start_offset(skb);
1232 gso.csum_offset = skb->csum_offset;
1233 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1234 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1235 } /* else everything is zero */
1237 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1238 sizeof(gso))))
1239 return -EFAULT;
1240 total += tun->vnet_hdr_sz;
1243 if (!vlan_tx_tag_present(skb)) {
1244 len = min_t(int, skb->len, len);
1245 } else {
1246 int copy, ret;
1247 struct {
1248 __be16 h_vlan_proto;
1249 __be16 h_vlan_TCI;
1250 } veth;
1252 veth.h_vlan_proto = skb->vlan_proto;
1253 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
1255 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
1256 len = min_t(int, skb->len + VLAN_HLEN, len);
1258 copy = min_t(int, vlan_offset, len);
1259 ret = skb_copy_datagram_const_iovec(skb, 0, iv, total, copy);
1260 len -= copy;
1261 total += copy;
1262 if (ret || !len)
1263 goto done;
1265 copy = min_t(int, sizeof(veth), len);
1266 ret = memcpy_toiovecend(iv, (void *)&veth, total, copy);
1267 len -= copy;
1268 total += copy;
1269 if (ret || !len)
1270 goto done;
1273 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, total, len);
1274 total += len;
1276 done:
1277 tun->dev->stats.tx_packets++;
1278 tun->dev->stats.tx_bytes += len;
1280 return total;
1283 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1284 struct kiocb *iocb, const struct iovec *iv,
1285 ssize_t len, int noblock)
1287 DECLARE_WAITQUEUE(wait, current);
1288 struct sk_buff *skb;
1289 ssize_t ret = 0;
1291 tun_debug(KERN_INFO, tun, "tun_do_read\n");
1293 if (unlikely(!noblock))
1294 add_wait_queue(&tfile->wq.wait, &wait);
1295 while (len) {
1296 current->state = TASK_INTERRUPTIBLE;
1298 /* Read frames from the queue */
1299 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
1300 if (noblock) {
1301 ret = -EAGAIN;
1302 break;
1304 if (signal_pending(current)) {
1305 ret = -ERESTARTSYS;
1306 break;
1308 if (tun->dev->reg_state != NETREG_REGISTERED) {
1309 ret = -EIO;
1310 break;
1313 /* Nothing to read, let's sleep */
1314 schedule();
1315 continue;
1318 ret = tun_put_user(tun, tfile, skb, iv, len);
1319 kfree_skb(skb);
1320 break;
1323 current->state = TASK_RUNNING;
1324 if (unlikely(!noblock))
1325 remove_wait_queue(&tfile->wq.wait, &wait);
1327 return ret;
1330 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1331 unsigned long count, loff_t pos)
1333 struct file *file = iocb->ki_filp;
1334 struct tun_file *tfile = file->private_data;
1335 struct tun_struct *tun = __tun_get(tfile);
1336 ssize_t len, ret;
1338 if (!tun)
1339 return -EBADFD;
1340 len = iov_length(iv, count);
1341 if (len < 0) {
1342 ret = -EINVAL;
1343 goto out;
1346 ret = tun_do_read(tun, tfile, iocb, iv, len,
1347 file->f_flags & O_NONBLOCK);
1348 ret = min_t(ssize_t, ret, len);
1349 out:
1350 tun_put(tun);
1351 return ret;
1354 static void tun_free_netdev(struct net_device *dev)
1356 struct tun_struct *tun = netdev_priv(dev);
1358 BUG_ON(!(list_empty(&tun->disabled)));
1359 tun_flow_uninit(tun);
1360 security_tun_dev_free_security(tun->security);
1361 free_netdev(dev);
1364 static void tun_setup(struct net_device *dev)
1366 struct tun_struct *tun = netdev_priv(dev);
1368 tun->owner = INVALID_UID;
1369 tun->group = INVALID_GID;
1371 dev->ethtool_ops = &tun_ethtool_ops;
1372 dev->destructor = tun_free_netdev;
1375 /* Trivial set of netlink ops to allow deleting tun or tap
1376 * device with netlink.
1378 static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1380 return -EINVAL;
1383 static struct rtnl_link_ops tun_link_ops __read_mostly = {
1384 .kind = DRV_NAME,
1385 .priv_size = sizeof(struct tun_struct),
1386 .setup = tun_setup,
1387 .validate = tun_validate,
1390 static void tun_sock_write_space(struct sock *sk)
1392 struct tun_file *tfile;
1393 wait_queue_head_t *wqueue;
1395 if (!sock_writeable(sk))
1396 return;
1398 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1399 return;
1401 wqueue = sk_sleep(sk);
1402 if (wqueue && waitqueue_active(wqueue))
1403 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
1404 POLLWRNORM | POLLWRBAND);
1406 tfile = container_of(sk, struct tun_file, sk);
1407 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
1410 static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1411 struct msghdr *m, size_t total_len)
1413 int ret;
1414 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1415 struct tun_struct *tun = __tun_get(tfile);
1417 if (!tun)
1418 return -EBADFD;
1419 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1420 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1421 tun_put(tun);
1422 return ret;
1425 static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1426 struct msghdr *m, size_t total_len,
1427 int flags)
1429 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1430 struct tun_struct *tun = __tun_get(tfile);
1431 int ret;
1433 if (!tun)
1434 return -EBADFD;
1436 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
1437 ret = -EINVAL;
1438 goto out;
1440 if (flags & MSG_ERRQUEUE) {
1441 ret = sock_recv_errqueue(sock->sk, m, total_len,
1442 SOL_PACKET, TUN_TX_TIMESTAMP);
1443 goto out;
1445 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
1446 flags & MSG_DONTWAIT);
1447 if (ret > total_len) {
1448 m->msg_flags |= MSG_TRUNC;
1449 ret = flags & MSG_TRUNC ? ret : total_len;
1451 out:
1452 tun_put(tun);
1453 return ret;
1456 static int tun_release(struct socket *sock)
1458 if (sock->sk)
1459 sock_put(sock->sk);
1460 return 0;
1463 /* Ops structure to mimic raw sockets with tun */
1464 static const struct proto_ops tun_socket_ops = {
1465 .sendmsg = tun_sendmsg,
1466 .recvmsg = tun_recvmsg,
1467 .release = tun_release,
1470 static struct proto tun_proto = {
1471 .name = "tun",
1472 .owner = THIS_MODULE,
1473 .obj_size = sizeof(struct tun_file),
1476 static int tun_flags(struct tun_struct *tun)
1478 int flags = 0;
1480 if (tun->flags & TUN_TUN_DEV)
1481 flags |= IFF_TUN;
1482 else
1483 flags |= IFF_TAP;
1485 if (tun->flags & TUN_NO_PI)
1486 flags |= IFF_NO_PI;
1488 /* This flag has no real effect. We track the value for backwards
1489 * compatibility.
1491 if (tun->flags & TUN_ONE_QUEUE)
1492 flags |= IFF_ONE_QUEUE;
1494 if (tun->flags & TUN_VNET_HDR)
1495 flags |= IFF_VNET_HDR;
1497 if (tun->flags & TUN_TAP_MQ)
1498 flags |= IFF_MULTI_QUEUE;
1500 if (tun->flags & TUN_PERSIST)
1501 flags |= IFF_PERSIST;
1503 return flags;
1506 static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1507 char *buf)
1509 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1510 return sprintf(buf, "0x%x\n", tun_flags(tun));
1513 static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1514 char *buf)
1516 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1517 return uid_valid(tun->owner)?
1518 sprintf(buf, "%u\n",
1519 from_kuid_munged(current_user_ns(), tun->owner)):
1520 sprintf(buf, "-1\n");
1523 static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1524 char *buf)
1526 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1527 return gid_valid(tun->group) ?
1528 sprintf(buf, "%u\n",
1529 from_kgid_munged(current_user_ns(), tun->group)):
1530 sprintf(buf, "-1\n");
1533 static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1534 static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1535 static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1537 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1539 struct tun_struct *tun;
1540 struct tun_file *tfile = file->private_data;
1541 struct net_device *dev;
1542 int err;
1544 if (tfile->detached)
1545 return -EINVAL;
1547 dev = __dev_get_by_name(net, ifr->ifr_name);
1548 if (dev) {
1549 if (ifr->ifr_flags & IFF_TUN_EXCL)
1550 return -EBUSY;
1551 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1552 tun = netdev_priv(dev);
1553 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1554 tun = netdev_priv(dev);
1555 else
1556 return -EINVAL;
1558 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
1559 !!(tun->flags & TUN_TAP_MQ))
1560 return -EINVAL;
1562 if (tun_not_capable(tun))
1563 return -EPERM;
1564 err = security_tun_dev_open(tun->security);
1565 if (err < 0)
1566 return err;
1568 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1569 if (err < 0)
1570 return err;
1572 if (tun->flags & TUN_TAP_MQ &&
1573 (tun->numqueues + tun->numdisabled > 1)) {
1574 /* One or more queue has already been attached, no need
1575 * to initialize the device again.
1577 return 0;
1580 else {
1581 char *name;
1582 unsigned long flags = 0;
1583 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1584 MAX_TAP_QUEUES : 1;
1586 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1587 return -EPERM;
1588 err = security_tun_dev_create();
1589 if (err < 0)
1590 return err;
1592 /* Set dev type */
1593 if (ifr->ifr_flags & IFF_TUN) {
1594 /* TUN device */
1595 flags |= TUN_TUN_DEV;
1596 name = "tun%d";
1597 } else if (ifr->ifr_flags & IFF_TAP) {
1598 /* TAP device */
1599 flags |= TUN_TAP_DEV;
1600 name = "tap%d";
1601 } else
1602 return -EINVAL;
1604 if (*ifr->ifr_name)
1605 name = ifr->ifr_name;
1607 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1608 tun_setup, queues, queues);
1610 if (!dev)
1611 return -ENOMEM;
1613 dev_net_set(dev, net);
1614 dev->rtnl_link_ops = &tun_link_ops;
1615 dev->ifindex = tfile->ifindex;
1617 tun = netdev_priv(dev);
1618 tun->dev = dev;
1619 tun->flags = flags;
1620 tun->txflt.count = 0;
1621 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1623 tun->filter_attached = false;
1624 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
1626 spin_lock_init(&tun->lock);
1628 err = security_tun_dev_alloc_security(&tun->security);
1629 if (err < 0)
1630 goto err_free_dev;
1632 tun_net_init(dev);
1633 tun_flow_init(tun);
1635 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1636 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1637 NETIF_F_HW_VLAN_STAG_TX;
1638 dev->features = dev->hw_features;
1639 dev->vlan_features = dev->features;
1641 INIT_LIST_HEAD(&tun->disabled);
1642 err = tun_attach(tun, file, false);
1643 if (err < 0)
1644 goto err_free_flow;
1646 err = register_netdevice(tun->dev);
1647 if (err < 0)
1648 goto err_detach;
1650 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1651 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1652 device_create_file(&tun->dev->dev, &dev_attr_group))
1653 pr_err("Failed to create tun sysfs files\n");
1656 netif_carrier_on(tun->dev);
1658 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1660 if (ifr->ifr_flags & IFF_NO_PI)
1661 tun->flags |= TUN_NO_PI;
1662 else
1663 tun->flags &= ~TUN_NO_PI;
1665 /* This flag has no real effect. We track the value for backwards
1666 * compatibility.
1668 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1669 tun->flags |= TUN_ONE_QUEUE;
1670 else
1671 tun->flags &= ~TUN_ONE_QUEUE;
1673 if (ifr->ifr_flags & IFF_VNET_HDR)
1674 tun->flags |= TUN_VNET_HDR;
1675 else
1676 tun->flags &= ~TUN_VNET_HDR;
1678 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1679 tun->flags |= TUN_TAP_MQ;
1680 else
1681 tun->flags &= ~TUN_TAP_MQ;
1683 /* Make sure persistent devices do not get stuck in
1684 * xoff state.
1686 if (netif_running(tun->dev))
1687 netif_tx_wake_all_queues(tun->dev);
1689 strcpy(ifr->ifr_name, tun->dev->name);
1690 return 0;
1692 err_detach:
1693 tun_detach_all(dev);
1694 err_free_flow:
1695 tun_flow_uninit(tun);
1696 security_tun_dev_free_security(tun->security);
1697 err_free_dev:
1698 free_netdev(dev);
1699 return err;
1702 static void tun_get_iff(struct net *net, struct tun_struct *tun,
1703 struct ifreq *ifr)
1705 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1707 strcpy(ifr->ifr_name, tun->dev->name);
1709 ifr->ifr_flags = tun_flags(tun);
1713 /* This is like a cut-down ethtool ops, except done via tun fd so no
1714 * privs required. */
1715 static int set_offload(struct tun_struct *tun, unsigned long arg)
1717 netdev_features_t features = 0;
1719 if (arg & TUN_F_CSUM) {
1720 features |= NETIF_F_HW_CSUM;
1721 arg &= ~TUN_F_CSUM;
1723 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1724 if (arg & TUN_F_TSO_ECN) {
1725 features |= NETIF_F_TSO_ECN;
1726 arg &= ~TUN_F_TSO_ECN;
1728 if (arg & TUN_F_TSO4)
1729 features |= NETIF_F_TSO;
1730 if (arg & TUN_F_TSO6)
1731 features |= NETIF_F_TSO6;
1732 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1735 if (arg & TUN_F_UFO) {
1736 features |= NETIF_F_UFO;
1737 arg &= ~TUN_F_UFO;
1741 /* This gives the user a way to test for new features in future by
1742 * trying to set them. */
1743 if (arg)
1744 return -EINVAL;
1746 tun->set_features = features;
1747 netdev_update_features(tun->dev);
1749 return 0;
1752 static void tun_detach_filter(struct tun_struct *tun, int n)
1754 int i;
1755 struct tun_file *tfile;
1757 for (i = 0; i < n; i++) {
1758 tfile = rtnl_dereference(tun->tfiles[i]);
1759 sk_detach_filter(tfile->socket.sk);
1762 tun->filter_attached = false;
1765 static int tun_attach_filter(struct tun_struct *tun)
1767 int i, ret = 0;
1768 struct tun_file *tfile;
1770 for (i = 0; i < tun->numqueues; i++) {
1771 tfile = rtnl_dereference(tun->tfiles[i]);
1772 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1773 if (ret) {
1774 tun_detach_filter(tun, i);
1775 return ret;
1779 tun->filter_attached = true;
1780 return ret;
1783 static void tun_set_sndbuf(struct tun_struct *tun)
1785 struct tun_file *tfile;
1786 int i;
1788 for (i = 0; i < tun->numqueues; i++) {
1789 tfile = rtnl_dereference(tun->tfiles[i]);
1790 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1794 static int tun_set_queue(struct file *file, struct ifreq *ifr)
1796 struct tun_file *tfile = file->private_data;
1797 struct tun_struct *tun;
1798 int ret = 0;
1800 rtnl_lock();
1802 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
1803 tun = tfile->detached;
1804 if (!tun) {
1805 ret = -EINVAL;
1806 goto unlock;
1808 ret = security_tun_dev_attach_queue(tun->security);
1809 if (ret < 0)
1810 goto unlock;
1811 ret = tun_attach(tun, file, false);
1812 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1813 tun = rtnl_dereference(tfile->tun);
1814 if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
1815 ret = -EINVAL;
1816 else
1817 __tun_detach(tfile, false);
1818 } else
1819 ret = -EINVAL;
1821 unlock:
1822 rtnl_unlock();
1823 return ret;
1826 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1827 unsigned long arg, int ifreq_len)
1829 struct tun_file *tfile = file->private_data;
1830 struct tun_struct *tun;
1831 void __user* argp = (void __user*)arg;
1832 struct ifreq ifr;
1833 kuid_t owner;
1834 kgid_t group;
1835 int sndbuf;
1836 int vnet_hdr_sz;
1837 unsigned int ifindex;
1838 int ret;
1840 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
1841 if (copy_from_user(&ifr, argp, ifreq_len))
1842 return -EFAULT;
1843 } else {
1844 memset(&ifr, 0, sizeof(ifr));
1846 if (cmd == TUNGETFEATURES) {
1847 /* Currently this just means: "what IFF flags are valid?".
1848 * This is needed because we never checked for invalid flags on
1849 * TUNSETIFF. */
1850 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1851 IFF_VNET_HDR | IFF_MULTI_QUEUE,
1852 (unsigned int __user*)argp);
1853 } else if (cmd == TUNSETQUEUE)
1854 return tun_set_queue(file, &ifr);
1856 ret = 0;
1857 rtnl_lock();
1859 tun = __tun_get(tfile);
1860 if (cmd == TUNSETIFF && !tun) {
1861 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1863 ret = tun_set_iff(tfile->net, file, &ifr);
1865 if (ret)
1866 goto unlock;
1868 if (copy_to_user(argp, &ifr, ifreq_len))
1869 ret = -EFAULT;
1870 goto unlock;
1872 if (cmd == TUNSETIFINDEX) {
1873 ret = -EPERM;
1874 if (tun)
1875 goto unlock;
1877 ret = -EFAULT;
1878 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1879 goto unlock;
1881 ret = 0;
1882 tfile->ifindex = ifindex;
1883 goto unlock;
1886 ret = -EBADFD;
1887 if (!tun)
1888 goto unlock;
1890 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1892 ret = 0;
1893 switch (cmd) {
1894 case TUNGETIFF:
1895 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1897 if (tfile->detached)
1898 ifr.ifr_flags |= IFF_DETACH_QUEUE;
1899 if (!tfile->socket.sk->sk_filter)
1900 ifr.ifr_flags |= IFF_NOFILTER;
1902 if (copy_to_user(argp, &ifr, ifreq_len))
1903 ret = -EFAULT;
1904 break;
1906 case TUNSETNOCSUM:
1907 /* Disable/Enable checksum */
1909 /* [unimplemented] */
1910 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
1911 arg ? "disabled" : "enabled");
1912 break;
1914 case TUNSETPERSIST:
1915 /* Disable/Enable persist mode. Keep an extra reference to the
1916 * module to prevent the module being unprobed.
1918 if (arg && !(tun->flags & TUN_PERSIST)) {
1919 tun->flags |= TUN_PERSIST;
1920 __module_get(THIS_MODULE);
1922 if (!arg && (tun->flags & TUN_PERSIST)) {
1923 tun->flags &= ~TUN_PERSIST;
1924 module_put(THIS_MODULE);
1927 tun_debug(KERN_INFO, tun, "persist %s\n",
1928 arg ? "enabled" : "disabled");
1929 break;
1931 case TUNSETOWNER:
1932 /* Set owner of the device */
1933 owner = make_kuid(current_user_ns(), arg);
1934 if (!uid_valid(owner)) {
1935 ret = -EINVAL;
1936 break;
1938 tun->owner = owner;
1939 tun_debug(KERN_INFO, tun, "owner set to %u\n",
1940 from_kuid(&init_user_ns, tun->owner));
1941 break;
1943 case TUNSETGROUP:
1944 /* Set group of the device */
1945 group = make_kgid(current_user_ns(), arg);
1946 if (!gid_valid(group)) {
1947 ret = -EINVAL;
1948 break;
1950 tun->group = group;
1951 tun_debug(KERN_INFO, tun, "group set to %u\n",
1952 from_kgid(&init_user_ns, tun->group));
1953 break;
1955 case TUNSETLINK:
1956 /* Only allow setting the type when the interface is down */
1957 if (tun->dev->flags & IFF_UP) {
1958 tun_debug(KERN_INFO, tun,
1959 "Linktype set failed because interface is up\n");
1960 ret = -EBUSY;
1961 } else {
1962 tun->dev->type = (int) arg;
1963 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1964 tun->dev->type);
1965 ret = 0;
1967 break;
1969 #ifdef TUN_DEBUG
1970 case TUNSETDEBUG:
1971 tun->debug = arg;
1972 break;
1973 #endif
1974 case TUNSETOFFLOAD:
1975 ret = set_offload(tun, arg);
1976 break;
1978 case TUNSETTXFILTER:
1979 /* Can be set only for TAPs */
1980 ret = -EINVAL;
1981 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1982 break;
1983 ret = update_filter(&tun->txflt, (void __user *)arg);
1984 break;
1986 case SIOCGIFHWADDR:
1987 /* Get hw address */
1988 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1989 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1990 if (copy_to_user(argp, &ifr, ifreq_len))
1991 ret = -EFAULT;
1992 break;
1994 case SIOCSIFHWADDR:
1995 /* Set hw address */
1996 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1997 ifr.ifr_hwaddr.sa_data);
1999 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2000 break;
2002 case TUNGETSNDBUF:
2003 sndbuf = tfile->socket.sk->sk_sndbuf;
2004 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2005 ret = -EFAULT;
2006 break;
2008 case TUNSETSNDBUF:
2009 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2010 ret = -EFAULT;
2011 break;
2014 tun->sndbuf = sndbuf;
2015 tun_set_sndbuf(tun);
2016 break;
2018 case TUNGETVNETHDRSZ:
2019 vnet_hdr_sz = tun->vnet_hdr_sz;
2020 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2021 ret = -EFAULT;
2022 break;
2024 case TUNSETVNETHDRSZ:
2025 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2026 ret = -EFAULT;
2027 break;
2029 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2030 ret = -EINVAL;
2031 break;
2034 tun->vnet_hdr_sz = vnet_hdr_sz;
2035 break;
2037 case TUNATTACHFILTER:
2038 /* Can be set only for TAPs */
2039 ret = -EINVAL;
2040 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2041 break;
2042 ret = -EFAULT;
2043 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
2044 break;
2046 ret = tun_attach_filter(tun);
2047 break;
2049 case TUNDETACHFILTER:
2050 /* Can be set only for TAPs */
2051 ret = -EINVAL;
2052 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2053 break;
2054 ret = 0;
2055 tun_detach_filter(tun, tun->numqueues);
2056 break;
2058 case TUNGETFILTER:
2059 ret = -EINVAL;
2060 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2061 break;
2062 ret = -EFAULT;
2063 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2064 break;
2065 ret = 0;
2066 break;
2068 default:
2069 ret = -EINVAL;
2070 break;
2073 unlock:
2074 rtnl_unlock();
2075 if (tun)
2076 tun_put(tun);
2077 return ret;
2080 static long tun_chr_ioctl(struct file *file,
2081 unsigned int cmd, unsigned long arg)
2083 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2086 #ifdef CONFIG_COMPAT
2087 static long tun_chr_compat_ioctl(struct file *file,
2088 unsigned int cmd, unsigned long arg)
2090 switch (cmd) {
2091 case TUNSETIFF:
2092 case TUNGETIFF:
2093 case TUNSETTXFILTER:
2094 case TUNGETSNDBUF:
2095 case TUNSETSNDBUF:
2096 case SIOCGIFHWADDR:
2097 case SIOCSIFHWADDR:
2098 arg = (unsigned long)compat_ptr(arg);
2099 break;
2100 default:
2101 arg = (compat_ulong_t)arg;
2102 break;
2106 * compat_ifreq is shorter than ifreq, so we must not access beyond
2107 * the end of that structure. All fields that are used in this
2108 * driver are compatible though, we don't need to convert the
2109 * contents.
2111 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2113 #endif /* CONFIG_COMPAT */
2115 static int tun_chr_fasync(int fd, struct file *file, int on)
2117 struct tun_file *tfile = file->private_data;
2118 int ret;
2120 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
2121 goto out;
2123 if (on) {
2124 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
2125 if (ret)
2126 goto out;
2127 tfile->flags |= TUN_FASYNC;
2128 } else
2129 tfile->flags &= ~TUN_FASYNC;
2130 ret = 0;
2131 out:
2132 return ret;
2135 static int tun_chr_open(struct inode *inode, struct file * file)
2137 struct tun_file *tfile;
2139 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2141 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2142 &tun_proto);
2143 if (!tfile)
2144 return -ENOMEM;
2145 rcu_assign_pointer(tfile->tun, NULL);
2146 tfile->net = get_net(current->nsproxy->net_ns);
2147 tfile->flags = 0;
2148 tfile->ifindex = 0;
2150 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
2151 init_waitqueue_head(&tfile->wq.wait);
2153 tfile->socket.file = file;
2154 tfile->socket.ops = &tun_socket_ops;
2156 sock_init_data(&tfile->socket, &tfile->sk);
2157 sk_change_net(&tfile->sk, tfile->net);
2159 tfile->sk.sk_write_space = tun_sock_write_space;
2160 tfile->sk.sk_sndbuf = INT_MAX;
2162 file->private_data = tfile;
2163 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
2164 INIT_LIST_HEAD(&tfile->next);
2166 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2168 return 0;
2171 static int tun_chr_close(struct inode *inode, struct file *file)
2173 struct tun_file *tfile = file->private_data;
2174 struct net *net = tfile->net;
2176 tun_detach(tfile, true);
2177 put_net(net);
2179 return 0;
2182 static const struct file_operations tun_fops = {
2183 .owner = THIS_MODULE,
2184 .llseek = no_llseek,
2185 .read = do_sync_read,
2186 .aio_read = tun_chr_aio_read,
2187 .write = do_sync_write,
2188 .aio_write = tun_chr_aio_write,
2189 .poll = tun_chr_poll,
2190 .unlocked_ioctl = tun_chr_ioctl,
2191 #ifdef CONFIG_COMPAT
2192 .compat_ioctl = tun_chr_compat_ioctl,
2193 #endif
2194 .open = tun_chr_open,
2195 .release = tun_chr_close,
2196 .fasync = tun_chr_fasync
2199 static struct miscdevice tun_miscdev = {
2200 .minor = TUN_MINOR,
2201 .name = "tun",
2202 .nodename = "net/tun",
2203 .fops = &tun_fops,
2206 /* ethtool interface */
2208 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2210 cmd->supported = 0;
2211 cmd->advertising = 0;
2212 ethtool_cmd_speed_set(cmd, SPEED_10);
2213 cmd->duplex = DUPLEX_FULL;
2214 cmd->port = PORT_TP;
2215 cmd->phy_address = 0;
2216 cmd->transceiver = XCVR_INTERNAL;
2217 cmd->autoneg = AUTONEG_DISABLE;
2218 cmd->maxtxpkt = 0;
2219 cmd->maxrxpkt = 0;
2220 return 0;
2223 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2225 struct tun_struct *tun = netdev_priv(dev);
2227 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2228 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2230 switch (tun->flags & TUN_TYPE_MASK) {
2231 case TUN_TUN_DEV:
2232 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
2233 break;
2234 case TUN_TAP_DEV:
2235 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
2236 break;
2240 static u32 tun_get_msglevel(struct net_device *dev)
2242 #ifdef TUN_DEBUG
2243 struct tun_struct *tun = netdev_priv(dev);
2244 return tun->debug;
2245 #else
2246 return -EOPNOTSUPP;
2247 #endif
2250 static void tun_set_msglevel(struct net_device *dev, u32 value)
2252 #ifdef TUN_DEBUG
2253 struct tun_struct *tun = netdev_priv(dev);
2254 tun->debug = value;
2255 #endif
2258 static const struct ethtool_ops tun_ethtool_ops = {
2259 .get_settings = tun_get_settings,
2260 .get_drvinfo = tun_get_drvinfo,
2261 .get_msglevel = tun_get_msglevel,
2262 .set_msglevel = tun_set_msglevel,
2263 .get_link = ethtool_op_get_link,
2264 .get_ts_info = ethtool_op_get_ts_info,
2268 static int __init tun_init(void)
2270 int ret = 0;
2272 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2273 pr_info("%s\n", DRV_COPYRIGHT);
2275 ret = rtnl_link_register(&tun_link_ops);
2276 if (ret) {
2277 pr_err("Can't register link_ops\n");
2278 goto err_linkops;
2281 ret = misc_register(&tun_miscdev);
2282 if (ret) {
2283 pr_err("Can't register misc device %d\n", TUN_MINOR);
2284 goto err_misc;
2286 return 0;
2287 err_misc:
2288 rtnl_link_unregister(&tun_link_ops);
2289 err_linkops:
2290 return ret;
2293 static void tun_cleanup(void)
2295 misc_deregister(&tun_miscdev);
2296 rtnl_link_unregister(&tun_link_ops);
2299 /* Get an underlying socket object from tun file. Returns error unless file is
2300 * attached to a device. The returned object works like a packet socket, it
2301 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2302 * holding a reference to the file for as long as the socket is in use. */
2303 struct socket *tun_get_socket(struct file *file)
2305 struct tun_file *tfile;
2306 if (file->f_op != &tun_fops)
2307 return ERR_PTR(-EINVAL);
2308 tfile = file->private_data;
2309 if (!tfile)
2310 return ERR_PTR(-EBADFD);
2311 return &tfile->socket;
2313 EXPORT_SYMBOL_GPL(tun_get_socket);
2315 module_init(tun_init);
2316 module_exit(tun_cleanup);
2317 MODULE_DESCRIPTION(DRV_DESCRIPTION);
2318 MODULE_AUTHOR(DRV_COPYRIGHT);
2319 MODULE_LICENSE("GPL");
2320 MODULE_ALIAS_MISCDEV(TUN_MINOR);
2321 MODULE_ALIAS("devname:net/tun");