blob: cc09b67c23bcd30402d73a93594f7aef9cb100da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070021 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000025 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
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
Joe Perches6b8a66e2011-03-02 07:18:10 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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>
Arnd Bergmann50857e22009-11-06 22:52:32 -080058#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#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>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070064#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070065#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000066#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070067#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070068#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080069#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080070#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/uaccess.h>
73
Rusty Russell14daa022008-04-12 18:48:58 -070074/* Uncomment to enable debugging */
75/* #define TUN_DEBUG 1 */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef TUN_DEBUG
78static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070079
Joe Perches6b8a66e2011-03-02 07:18:10 +000080#define tun_debug(level, tun, fmt, args...) \
81do { \
82 if (tun->debug) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
84} while (0)
85#define DBG1(level, fmt, args...) \
86do { \
87 if (debug == 2) \
88 printk(level fmt, ##args); \
89} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070090#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000091#define tun_debug(level, tun, fmt, args...) \
92do { \
93 if (0) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
95} while (0)
96#define DBG1(level, fmt, args...) \
97do { \
98 if (0) \
99 printk(level fmt, ##args); \
100} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000103#define GOODCOPY_LEN 128
104
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700105#define FLT_EXACT_COUNT 8
106struct tap_filter {
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
110};
111
Jason Wangedfb6a12013-01-23 03:59:12 +0000112/* DEFAULT_MAX_NUM_RSS_QUEUES were choosed to let the rx/tx queues allocated for
113 * the netdevice to be fit in one page. So we can make sure the success of
114 * memory allocation. TODO: increase the limit. */
115#define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
Jason Wangb8732fb2013-01-23 03:59:13 +0000116#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000117
Jason Wang96442e422012-10-31 19:46:02 +0000118#define TUN_FLOW_EXPIRE (3 * HZ)
119
Jason Wang54f968d2012-10-31 19:45:57 +0000120/* A tun_file connects an open character device to a tuntap netdevice. It
121 * also contains all socket related strctures (except sock_fprog and tap_filter)
122 * to serve as one transmit queue for tuntap device. The sock_fprog and
123 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000124 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000125 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000126 *
127 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000128 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000129 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000130 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000131struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000132 struct sock sk;
133 struct socket socket;
134 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000135 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000136 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000137 struct fasync_struct *fasync;
138 /* only used for fasnyc */
139 unsigned int flags;
Jason Wangc8d68e62012-10-31 19:46:00 +0000140 u16 queue_index;
Jason Wang4008e972012-12-13 23:53:30 +0000141 struct list_head next;
142 struct tun_struct *detached;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000143};
144
Jason Wang96442e422012-10-31 19:46:02 +0000145struct tun_flow_entry {
146 struct hlist_node hash_link;
147 struct rcu_head rcu;
148 struct tun_struct *tun;
149
150 u32 rxhash;
151 int queue_index;
152 unsigned long updated;
153};
154
155#define TUN_NUM_FLOW_ENTRIES 1024
156
Jason Wang54f968d2012-10-31 19:45:57 +0000157/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000158 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000159 * file were attached to a persist device.
160 */
Rusty Russell14daa022008-04-12 18:48:58 -0700161struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000162 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
163 unsigned int numqueues;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700164 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800165 kuid_t owner;
166 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700167
Rusty Russell14daa022008-04-12 18:48:58 -0700168 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000169 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000170#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
171 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200172
173 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000174 int sndbuf;
175 struct tap_filter txflt;
176 struct sock_fprog fprog;
177 /* protected by rtnl lock */
178 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700179#ifdef TUN_DEBUG
180 int debug;
181#endif
Jason Wang96442e422012-10-31 19:46:02 +0000182 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000183 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
184 struct timer_list flow_gc_timer;
185 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000186 unsigned int numdisabled;
187 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000188 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000189 u32 flow_count;
Rusty Russell14daa022008-04-12 18:48:58 -0700190};
191
Jason Wang96442e422012-10-31 19:46:02 +0000192static inline u32 tun_hashfn(u32 rxhash)
193{
194 return rxhash & 0x3ff;
195}
196
197static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
198{
199 struct tun_flow_entry *e;
200 struct hlist_node *n;
201
202 hlist_for_each_entry_rcu(e, n, head, hash_link) {
203 if (e->rxhash == rxhash)
204 return e;
205 }
206 return NULL;
207}
208
209static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
210 struct hlist_head *head,
211 u32 rxhash, u16 queue_index)
212{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000213 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
214
Jason Wang96442e422012-10-31 19:46:02 +0000215 if (e) {
216 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
217 rxhash, queue_index);
218 e->updated = jiffies;
219 e->rxhash = rxhash;
220 e->queue_index = queue_index;
221 e->tun = tun;
222 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000223 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000224 }
225 return e;
226}
227
Jason Wang96442e422012-10-31 19:46:02 +0000228static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
229{
230 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
231 e->rxhash, e->queue_index);
232 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000233 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000234 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000235}
236
237static void tun_flow_flush(struct tun_struct *tun)
238{
239 int i;
240
241 spin_lock_bh(&tun->lock);
242 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
243 struct tun_flow_entry *e;
244 struct hlist_node *h, *n;
245
246 hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link)
247 tun_flow_delete(tun, e);
248 }
249 spin_unlock_bh(&tun->lock);
250}
251
252static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
253{
254 int i;
255
256 spin_lock_bh(&tun->lock);
257 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
258 struct tun_flow_entry *e;
259 struct hlist_node *h, *n;
260
261 hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
262 if (e->queue_index == queue_index)
263 tun_flow_delete(tun, e);
264 }
265 }
266 spin_unlock_bh(&tun->lock);
267}
268
269static void tun_flow_cleanup(unsigned long data)
270{
271 struct tun_struct *tun = (struct tun_struct *)data;
272 unsigned long delay = tun->ageing_time;
273 unsigned long next_timer = jiffies + delay;
274 unsigned long count = 0;
275 int i;
276
277 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
278
279 spin_lock_bh(&tun->lock);
280 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
281 struct tun_flow_entry *e;
282 struct hlist_node *h, *n;
283
284 hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
285 unsigned long this_timer;
286 count++;
287 this_timer = e->updated + delay;
288 if (time_before_eq(this_timer, jiffies))
289 tun_flow_delete(tun, e);
290 else if (time_before(this_timer, next_timer))
291 next_timer = this_timer;
292 }
293 }
294
295 if (count)
296 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
297 spin_unlock_bh(&tun->lock);
298}
299
Eric Dumazet49974422012-12-12 19:22:57 +0000300static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang96442e422012-10-31 19:46:02 +0000301 u16 queue_index)
302{
303 struct hlist_head *head;
304 struct tun_flow_entry *e;
305 unsigned long delay = tun->ageing_time;
Jason Wang96442e422012-10-31 19:46:02 +0000306
307 if (!rxhash)
308 return;
309 else
310 head = &tun->flows[tun_hashfn(rxhash)];
311
312 rcu_read_lock();
313
314 if (tun->numqueues == 1)
315 goto unlock;
316
317 e = tun_flow_find(head, rxhash);
318 if (likely(e)) {
319 /* TODO: keep queueing to old queue until it's empty? */
320 e->queue_index = queue_index;
321 e->updated = jiffies;
322 } else {
323 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000324 if (!tun_flow_find(head, rxhash) &&
325 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000326 tun_flow_create(tun, head, rxhash, queue_index);
327
328 if (!timer_pending(&tun->flow_gc_timer))
329 mod_timer(&tun->flow_gc_timer,
330 round_jiffies_up(jiffies + delay));
331 spin_unlock_bh(&tun->lock);
332 }
333
334unlock:
335 rcu_read_unlock();
336}
337
Jason Wangc8d68e62012-10-31 19:46:00 +0000338/* We try to identify a flow through its rxhash first. The reason that
339 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
340 * the rxq based on the txq where the last packet of the flow comes. As
341 * the userspace application move between processors, we may get a
342 * different rxq no. here. If we could not get rxhash, then we would
343 * hope the rxq no. may help here.
344 */
345static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
346{
347 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000348 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000349 u32 txq = 0;
350 u32 numqueues = 0;
351
352 rcu_read_lock();
353 numqueues = tun->numqueues;
354
355 txq = skb_get_rxhash(skb);
356 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000357 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
358 if (e)
359 txq = e->queue_index;
360 else
361 /* use multiply and shift instead of expensive divide */
362 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000363 } else if (likely(skb_rx_queue_recorded(skb))) {
364 txq = skb_get_rx_queue(skb);
365 while (unlikely(txq >= numqueues))
366 txq -= numqueues;
367 }
368
369 rcu_read_unlock();
370 return txq;
371}
372
Jason Wangcde8b152012-10-31 19:46:01 +0000373static inline bool tun_not_capable(struct tun_struct *tun)
374{
375 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000376 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000377
378 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
379 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000380 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000381}
382
Jason Wangc8d68e62012-10-31 19:46:00 +0000383static void tun_set_real_num_queues(struct tun_struct *tun)
384{
385 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
386 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
387}
388
Jason Wang4008e972012-12-13 23:53:30 +0000389static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
390{
391 tfile->detached = tun;
392 list_add_tail(&tfile->next, &tun->disabled);
393 ++tun->numdisabled;
394}
395
Jason Wangd32649d2012-12-18 11:00:27 +0800396static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000397{
398 struct tun_struct *tun = tfile->detached;
399
400 tfile->detached = NULL;
401 list_del_init(&tfile->next);
402 --tun->numdisabled;
403 return tun;
404}
405
Jason Wangc8d68e62012-10-31 19:46:00 +0000406static void __tun_detach(struct tun_file *tfile, bool clean)
407{
408 struct tun_file *ntfile;
409 struct tun_struct *tun;
410 struct net_device *dev;
411
Jason Wangb8deabd2013-01-11 16:59:32 +0000412 tun = rtnl_dereference(tfile->tun);
413
Jason Wangc8d68e62012-10-31 19:46:00 +0000414 if (tun) {
415 u16 index = tfile->queue_index;
416 BUG_ON(index >= tun->numqueues);
417 dev = tun->dev;
418
419 rcu_assign_pointer(tun->tfiles[index],
420 tun->tfiles[tun->numqueues - 1]);
421 rcu_assign_pointer(tfile->tun, NULL);
Jason Wangb8deabd2013-01-11 16:59:32 +0000422 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000423 ntfile->queue_index = index;
424
425 --tun->numqueues;
Jason Wang4008e972012-12-13 23:53:30 +0000426 if (clean)
427 sock_put(&tfile->sk);
428 else
429 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000430
431 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000432 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000433 /* Drop read queue */
434 skb_queue_purge(&tfile->sk.sk_receive_queue);
435 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000436 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000437 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000438 sock_put(&tfile->sk);
439 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000440
441 if (clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000442 if (tun && tun->numqueues == 0 && tun->numdisabled == 0 &&
443 !(tun->flags & TUN_PERSIST))
444 if (tun->dev->reg_state == NETREG_REGISTERED)
445 unregister_netdevice(tun->dev);
446
Jason Wangc8d68e62012-10-31 19:46:00 +0000447 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
448 &tfile->socket.flags));
449 sk_release_kernel(&tfile->sk);
450 }
451}
452
453static void tun_detach(struct tun_file *tfile, bool clean)
454{
455 rtnl_lock();
456 __tun_detach(tfile, clean);
457 rtnl_unlock();
458}
459
460static void tun_detach_all(struct net_device *dev)
461{
462 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000463 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000464 int i, n = tun->numqueues;
465
466 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000467 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000468 BUG_ON(!tfile);
469 wake_up_all(&tfile->wq.wait);
470 rcu_assign_pointer(tfile->tun, NULL);
471 --tun->numqueues;
472 }
473 BUG_ON(tun->numqueues != 0);
474
475 synchronize_net();
476 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000477 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000478 /* Drop read queue */
479 skb_queue_purge(&tfile->sk.sk_receive_queue);
480 sock_put(&tfile->sk);
481 }
Jason Wang4008e972012-12-13 23:53:30 +0000482 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
483 tun_enable_queue(tfile);
484 skb_queue_purge(&tfile->sk.sk_receive_queue);
485 sock_put(&tfile->sk);
486 }
487 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000488
489 if (tun->flags & TUN_PERSIST)
490 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000491}
492
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000493static int tun_attach(struct tun_struct *tun, struct file *file)
494{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000495 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000496 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000497
Paul Moore5dbbaf22013-01-14 07:12:19 +0000498 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
499 if (err < 0)
500 goto out;
501
Eric W. Biederman38231b72009-01-20 11:02:28 +0000502 err = -EINVAL;
Jason Wangb8deabd2013-01-11 16:59:32 +0000503 if (rtnl_dereference(tfile->tun))
Eric W. Biederman38231b72009-01-20 11:02:28 +0000504 goto out;
505
506 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000507 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
508 goto out;
509
510 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000511 if (!tfile->detached &&
512 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000513 goto out;
514
515 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000516
Jason Wangc8d68e62012-10-31 19:46:00 +0000517 /* Re-attach the filter to presist device */
Jason Wang54f968d2012-10-31 19:45:57 +0000518 if (tun->filter_attached == true) {
519 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
520 if (!err)
521 goto out;
522 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000523 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000524 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000525 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000526 tun->numqueues++;
527
Jason Wang4008e972012-12-13 23:53:30 +0000528 if (tfile->detached)
529 tun_enable_queue(tfile);
530 else
531 sock_hold(&tfile->sk);
532
Jason Wangc8d68e62012-10-31 19:46:00 +0000533 tun_set_real_num_queues(tun);
534
Jason Wangc8d68e62012-10-31 19:46:00 +0000535 /* device is allowed to go away first, so no need to hold extra
536 * refcnt.
537 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000538
Eric W. Biederman38231b72009-01-20 11:02:28 +0000539out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000540 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000541}
542
Eric W. Biederman631ab462009-01-20 11:00:40 +0000543static struct tun_struct *__tun_get(struct tun_file *tfile)
544{
Jason Wang6e914fc2012-10-31 19:45:58 +0000545 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000546
Jason Wang6e914fc2012-10-31 19:45:58 +0000547 rcu_read_lock();
548 tun = rcu_dereference(tfile->tun);
549 if (tun)
550 dev_hold(tun->dev);
551 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000552
553 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000554}
555
556static struct tun_struct *tun_get(struct file *file)
557{
558 return __tun_get(file->private_data);
559}
560
561static void tun_put(struct tun_struct *tun)
562{
Jason Wang6e914fc2012-10-31 19:45:58 +0000563 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000564}
565
Joe Perches6b8a66e2011-03-02 07:18:10 +0000566/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700567static void addr_hash_set(u32 *mask, const u8 *addr)
568{
569 int n = ether_crc(ETH_ALEN, addr) >> 26;
570 mask[n >> 5] |= (1 << (n & 31));
571}
572
573static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
574{
575 int n = ether_crc(ETH_ALEN, addr) >> 26;
576 return mask[n >> 5] & (1 << (n & 31));
577}
578
579static int update_filter(struct tap_filter *filter, void __user *arg)
580{
581 struct { u8 u[ETH_ALEN]; } *addr;
582 struct tun_filter uf;
583 int err, alen, n, nexact;
584
585 if (copy_from_user(&uf, arg, sizeof(uf)))
586 return -EFAULT;
587
588 if (!uf.count) {
589 /* Disabled */
590 filter->count = 0;
591 return 0;
592 }
593
594 alen = ETH_ALEN * uf.count;
595 addr = kmalloc(alen, GFP_KERNEL);
596 if (!addr)
597 return -ENOMEM;
598
599 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
600 err = -EFAULT;
601 goto done;
602 }
603
604 /* The filter is updated without holding any locks. Which is
605 * perfectly safe. We disable it first and in the worst
606 * case we'll accept a few undesired packets. */
607 filter->count = 0;
608 wmb();
609
610 /* Use first set of addresses as an exact filter */
611 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
612 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
613
614 nexact = n;
615
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800616 /* Remaining multicast addresses are hashed,
617 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700618 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800619 for (; n < uf.count; n++) {
620 if (!is_multicast_ether_addr(addr[n].u)) {
621 err = 0; /* no filter */
622 goto done;
623 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700624 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800625 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700626
627 /* For ALLMULTI just set the mask to all ones.
628 * This overrides the mask populated above. */
629 if ((uf.flags & TUN_FLT_ALLMULTI))
630 memset(filter->mask, ~0, sizeof(filter->mask));
631
632 /* Now enable the filter */
633 wmb();
634 filter->count = nexact;
635
636 /* Return the number of exact filters */
637 err = nexact;
638
639done:
640 kfree(addr);
641 return err;
642}
643
644/* Returns: 0 - drop, !=0 - accept */
645static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
646{
647 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
648 * at this point. */
649 struct ethhdr *eh = (struct ethhdr *) skb->data;
650 int i;
651
652 /* Exact match */
653 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000654 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700655 return 1;
656
657 /* Inexact match (multicast only) */
658 if (is_multicast_ether_addr(eh->h_dest))
659 return addr_hash_test(filter->mask, eh->h_dest);
660
661 return 0;
662}
663
664/*
665 * Checks whether the packet is accepted or not.
666 * Returns: 0 - drop, !=0 - accept
667 */
668static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
669{
670 if (!filter->count)
671 return 1;
672
673 return run_filter(filter, skb);
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/* Network device part of the driver */
677
Jeff Garzik7282d492006-09-13 14:30:00 -0400678static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000680/* Net device detach from fd. */
681static void tun_net_uninit(struct net_device *dev)
682{
Jason Wangc8d68e62012-10-31 19:46:00 +0000683 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/* Net device open. */
687static int tun_net_open(struct net_device *dev)
688{
Jason Wangc8d68e62012-10-31 19:46:00 +0000689 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691}
692
693/* Net device close. */
694static int tun_net_close(struct net_device *dev)
695{
Jason Wangc8d68e62012-10-31 19:46:00 +0000696 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
700/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000701static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000704 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000705 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jason Wang6e914fc2012-10-31 19:45:58 +0000707 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000708 tfile = rcu_dereference(tun->tfiles[txq]);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000711 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto drop;
713
Jason Wang6e914fc2012-10-31 19:45:58 +0000714 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
715
Jason Wangc8d68e62012-10-31 19:46:00 +0000716 BUG_ON(!tfile);
717
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700718 /* Drop if the filter does not like it.
719 * This is a noop if the filter is disabled.
720 * Filter can be enabled only for the TAP devices. */
721 if (!check_filter(&tun->txflt, skb))
722 goto drop;
723
Jason Wang54f968d2012-10-31 19:45:57 +0000724 if (tfile->socket.sk->sk_filter &&
725 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000726 goto drop;
727
Rami Rosen36fe8c092012-11-25 22:07:40 +0000728 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000729 * number of queues.
730 */
Jason Wang54f968d2012-10-31 19:45:57 +0000731 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000732 >= dev->tx_queue_len / tun->numqueues)
733 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000735 /* Orphan the skb - required as we might hang on to it
736 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000737 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
738 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000739 skb_orphan(skb);
740
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700741 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000742 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000745 if (tfile->flags & TUN_FASYNC)
746 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
747 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000748 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000749
750 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000751 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700754 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000755 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000757 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000758 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700761static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700763 /*
764 * This callback is supposed to deal with mc filter in
765 * _rx_ path and has nothing to do with the _tx_ path.
766 * In rx path we always accept everything userspace gives us.
767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Ed Swierk4885a502007-09-16 12:21:38 -0700770#define MIN_MTU 68
771#define MAX_MTU 65535
772
773static int
774tun_net_change_mtu(struct net_device *dev, int new_mtu)
775{
776 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
777 return -EINVAL;
778 dev->mtu = new_mtu;
779 return 0;
780}
781
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000782static netdev_features_t tun_net_fix_features(struct net_device *dev,
783 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000784{
785 struct tun_struct *tun = netdev_priv(dev);
786
787 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
788}
Neil Hormanbebd0972011-06-15 05:25:01 +0000789#ifdef CONFIG_NET_POLL_CONTROLLER
790static void tun_poll_controller(struct net_device *dev)
791{
792 /*
793 * Tun only receives frames when:
794 * 1) the char device endpoint gets data from user space
795 * 2) the tun socket gets a sendmsg call from user space
796 * Since both of those are syncronous operations, we are guaranteed
797 * never to have pending data when we poll for it
798 * so theres nothing to do here but return.
799 * We need this though so netpoll recognizes us as an interface that
800 * supports polling, which enables bridge devices in virt setups to
801 * still use netconsole
802 */
803 return;
804}
805#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800806static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000807 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800808 .ndo_open = tun_net_open,
809 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800810 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800811 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000812 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000813 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000814#ifdef CONFIG_NET_POLL_CONTROLLER
815 .ndo_poll_controller = tun_poll_controller,
816#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800817};
818
819static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000820 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800821 .ndo_open = tun_net_open,
822 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800823 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800824 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000825 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000826 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800827 .ndo_set_mac_address = eth_mac_addr,
828 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000829 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000830#ifdef CONFIG_NET_POLL_CONTROLLER
831 .ndo_poll_controller = tun_poll_controller,
832#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800833};
834
Jason Wang96442e422012-10-31 19:46:02 +0000835static int tun_flow_init(struct tun_struct *tun)
836{
837 int i;
838
Jason Wang96442e422012-10-31 19:46:02 +0000839 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
840 INIT_HLIST_HEAD(&tun->flows[i]);
841
842 tun->ageing_time = TUN_FLOW_EXPIRE;
843 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
844 mod_timer(&tun->flow_gc_timer,
845 round_jiffies_up(jiffies + tun->ageing_time));
846
847 return 0;
848}
849
850static void tun_flow_uninit(struct tun_struct *tun)
851{
852 del_timer_sync(&tun->flow_gc_timer);
853 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/* Initialize net device. */
857static void tun_net_init(struct net_device *dev)
858{
859 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 switch (tun->flags & TUN_TYPE_MASK) {
862 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800863 dev->netdev_ops = &tun_netdev_ops;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* Point-to-Point TUN Device */
866 dev->hard_header_len = 0;
867 dev->addr_len = 0;
868 dev->mtu = 1500;
869
870 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400871 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
873 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
874 break;
875
876 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800877 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700879 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000880 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000881 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000883 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
886 break;
887 }
888}
889
890/* Character device part */
891
892/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000893static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400894{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000895 struct tun_file *tfile = file->private_data;
896 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000897 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800898 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000901 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jason Wang54f968d2012-10-31 19:45:57 +0000903 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000904
Joe Perches6b8a66e2011-03-02 07:18:10 +0000905 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jason Wang54f968d2012-10-31 19:45:57 +0000907 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400908
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000909 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 mask |= POLLIN | POLLRDNORM;
911
Herbert Xu33dccbb2009-02-05 21:25:32 -0800912 if (sock_writeable(sk) ||
913 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
914 sock_writeable(sk)))
915 mask |= POLLOUT | POLLWRNORM;
916
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000917 if (tun->dev->reg_state != NETREG_REGISTERED)
918 mask = POLLERR;
919
Eric W. Biederman631ab462009-01-20 11:00:40 +0000920 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return mask;
922}
923
Rusty Russellf42157c2008-08-15 15:15:10 -0700924/* prepad is the amount to reserve at front. len is length after that.
925 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000926static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000927 size_t prepad, size_t len,
928 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700929{
Jason Wang54f968d2012-10-31 19:45:57 +0000930 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700931 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800932 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700933
934 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700935 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800936 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700937
Herbert Xu33dccbb2009-02-05 21:25:32 -0800938 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
939 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700940 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800941 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700942
943 skb_reserve(skb, prepad);
944 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800945 skb->data_len = len - linear;
946 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700947
948 return skb;
949}
950
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000951/* set skb frags from iovec, this can move to core network code for reuse */
952static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
953 int offset, size_t count)
954{
955 int len = iov_length(from, count) - offset;
956 int copy = skb_headlen(skb);
957 int size, offset1 = 0;
958 int i = 0;
959
960 /* Skip over from offset */
961 while (count && (offset >= from->iov_len)) {
962 offset -= from->iov_len;
963 ++from;
964 --count;
965 }
966
967 /* copy up to skb headlen */
968 while (count && (copy > 0)) {
969 size = min_t(unsigned int, copy, from->iov_len - offset);
970 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
971 size))
972 return -EFAULT;
973 if (copy > size) {
974 ++from;
975 --count;
976 offset = 0;
977 } else
978 offset += size;
979 copy -= size;
980 offset1 += size;
981 }
982
983 if (len == offset1)
984 return 0;
985
986 while (count--) {
987 struct page *page[MAX_SKB_FRAGS];
988 int num_pages;
989 unsigned long base;
990 unsigned long truesize;
991
992 len = from->iov_len - offset;
993 if (!len) {
994 offset = 0;
995 ++from;
996 continue;
997 }
998 base = (unsigned long)from->iov_base + offset;
999 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
1000 if (i + size > MAX_SKB_FRAGS)
1001 return -EMSGSIZE;
1002 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
1003 if (num_pages != size) {
1004 for (i = 0; i < num_pages; i++)
1005 put_page(page[i]);
1006 return -EFAULT;
1007 }
1008 truesize = size * PAGE_SIZE;
1009 skb->data_len += len;
1010 skb->len += len;
1011 skb->truesize += truesize;
1012 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
1013 while (len) {
1014 int off = base & ~PAGE_MASK;
1015 int size = min_t(int, len, PAGE_SIZE - off);
1016 __skb_fill_page_desc(skb, i, page[i], off, size);
1017 skb_shinfo(skb)->nr_frags++;
1018 /* increase sk_wmem_alloc */
1019 base += size;
1020 len -= size;
1021 i++;
1022 }
1023 offset = 0;
1024 ++from;
1025 }
1026 return 0;
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001030static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1031 void *msg_control, const struct iovec *iv,
1032 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Harvey Harrison09640e62009-02-01 00:45:17 -08001034 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001036 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -07001037 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001038 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001039 int copylen;
1040 bool zerocopy = false;
1041 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001042 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001045 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return -EINVAL;
1047
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001048 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001050 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Rusty Russellf43798c2008-07-03 03:48:02 -07001053 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001054 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001055 return -EINVAL;
1056
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001057 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001058 return -EFAULT;
1059
Herbert Xu49091222009-06-08 00:20:01 -07001060 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1061 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1062 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1063
Rusty Russellf43798c2008-07-03 03:48:02 -07001064 if (gso.hdr_len > len)
1065 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001066 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001067 }
1068
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001069 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +00001070 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001071 if (unlikely(len < ETH_HLEN ||
1072 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001073 return -EINVAL;
1074 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001075
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001076 if (msg_control)
1077 zerocopy = true;
1078
1079 if (zerocopy) {
1080 /* Userspace may produce vectors with count greater than
1081 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
1082 * to let the rest of data to be fit in the frags.
1083 */
1084 if (count > MAX_SKB_FRAGS) {
1085 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
1086 if (copylen < offset)
1087 copylen = 0;
1088 else
1089 copylen -= offset;
1090 } else
1091 copylen = 0;
1092 /* There are 256 bytes to be copied in skb, so there is enough
1093 * room for skb expand head in case it is used.
1094 * The rest of the buffer is mapped from userspace.
1095 */
1096 if (copylen < gso.hdr_len)
1097 copylen = gso.hdr_len;
1098 if (!copylen)
1099 copylen = GOODCOPY_LEN;
1100 } else
1101 copylen = len;
1102
Jason Wang54f968d2012-10-31 19:45:57 +00001103 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001104 if (IS_ERR(skb)) {
1105 if (PTR_ERR(skb) != -EAGAIN)
1106 tun->dev->stats.rx_dropped++;
1107 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001110 if (zerocopy)
1111 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
1112 else
1113 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
1114
1115 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001116 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001117 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Rusty Russellf43798c2008-07-03 03:48:02 -07001121 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1122 if (!skb_partial_csum_set(skb, gso.csum_start,
1123 gso.csum_offset)) {
1124 tun->dev->stats.rx_frame_errors++;
1125 kfree_skb(skb);
1126 return -EINVAL;
1127 }
Michał Mirosław88255372011-04-19 06:13:10 +00001128 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 switch (tun->flags & TUN_TYPE_MASK) {
1131 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001132 if (tun->flags & TUN_NO_PI) {
1133 switch (skb->data[0] & 0xf0) {
1134 case 0x40:
1135 pi.proto = htons(ETH_P_IP);
1136 break;
1137 case 0x60:
1138 pi.proto = htons(ETH_P_IPV6);
1139 break;
1140 default:
1141 tun->dev->stats.rx_dropped++;
1142 kfree_skb(skb);
1143 return -EINVAL;
1144 }
1145 }
1146
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001147 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001149 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 case TUN_TAP_DEV:
1152 skb->protocol = eth_type_trans(skb, tun->dev);
1153 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Rusty Russellf43798c2008-07-03 03:48:02 -07001156 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1157 pr_debug("GSO!\n");
1158 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1159 case VIRTIO_NET_HDR_GSO_TCPV4:
1160 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1161 break;
1162 case VIRTIO_NET_HDR_GSO_TCPV6:
1163 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1164 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001165 case VIRTIO_NET_HDR_GSO_UDP:
1166 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1167 break;
Rusty Russellf43798c2008-07-03 03:48:02 -07001168 default:
1169 tun->dev->stats.rx_frame_errors++;
1170 kfree_skb(skb);
1171 return -EINVAL;
1172 }
1173
1174 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1175 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1176
1177 skb_shinfo(skb)->gso_size = gso.gso_size;
1178 if (skb_shinfo(skb)->gso_size == 0) {
1179 tun->dev->stats.rx_frame_errors++;
1180 kfree_skb(skb);
1181 return -EINVAL;
1182 }
1183
1184 /* Header must be checked, and gso_segs computed. */
1185 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1186 skb_shinfo(skb)->gso_segs = 0;
1187 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001188
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001189 /* copy skb_ubuf_info for callback when skb has no error */
1190 if (zerocopy) {
1191 skb_shinfo(skb)->destructor_arg = msg_control;
1192 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1193 }
1194
Eric Dumazet76fe4582012-12-17 04:39:20 +00001195 skb_reset_network_header(skb);
Eric Dumazet49974422012-12-12 19:22:57 +00001196 rxhash = skb_get_rxhash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001198
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001199 tun->dev->stats.rx_packets++;
1200 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Eric Dumazet49974422012-12-12 19:22:57 +00001202 tun_flow_update(tun, rxhash, tfile->queue_index);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001203 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001206static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1207 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001209 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001210 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001211 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001212 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (!tun)
1215 return -EBADFD;
1216
Joe Perches6b8a66e2011-03-02 07:18:10 +00001217 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jason Wang54f968d2012-10-31 19:45:57 +00001219 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1220 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001221
1222 tun_put(tun);
1223 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001227static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001228 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001229 struct sk_buff *skb,
1230 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct tun_pi pi = { 0, skb->protocol };
1233 ssize_t total = 0;
1234
1235 if (!(tun->flags & TUN_NO_PI)) {
1236 if ((len -= sizeof(pi)) < 0)
1237 return -EINVAL;
1238
1239 if (len < skb->len) {
1240 /* Packet will be striped */
1241 pi.flags |= TUN_PKT_STRIP;
1242 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001243
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001244 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EFAULT;
1246 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Rusty Russellf43798c2008-07-03 03:48:02 -07001249 if (tun->flags & TUN_VNET_HDR) {
1250 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001251 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001252 return -EINVAL;
1253
1254 if (skb_is_gso(skb)) {
1255 struct skb_shared_info *sinfo = skb_shinfo(skb);
1256
1257 /* This is a hint as to how much should be linear. */
1258 gso.hdr_len = skb_headlen(skb);
1259 gso.gso_size = sinfo->gso_size;
1260 if (sinfo->gso_type & SKB_GSO_TCPV4)
1261 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1262 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1263 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001264 else if (sinfo->gso_type & SKB_GSO_UDP)
1265 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001266 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001267 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001268 "0x%x, gso_size %d, hdr_len %d\n",
1269 sinfo->gso_type, gso.gso_size,
1270 gso.hdr_len);
1271 print_hex_dump(KERN_ERR, "tun: ",
1272 DUMP_PREFIX_NONE,
1273 16, 1, skb->head,
1274 min((int)gso.hdr_len, 64), true);
1275 WARN_ON_ONCE(1);
1276 return -EINVAL;
1277 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001278 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1279 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1280 } else
1281 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1282
1283 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1284 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001285 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001286 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001287 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1288 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001289 } /* else everything is zero */
1290
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001291 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1292 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001293 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001294 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001295 }
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 len = min_t(int, skb->len, len);
1298
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001299 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001300 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001302 tun->dev->stats.tx_packets++;
1303 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 return total;
1306}
1307
Jason Wang54f968d2012-10-31 19:45:57 +00001308static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001309 struct kiocb *iocb, const struct iovec *iv,
1310 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 DECLARE_WAITQUEUE(wait, current);
1313 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001314 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Rami Rosen3872baf2012-11-25 22:07:41 +00001316 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Amos Kong61a5ff12011-06-09 00:27:10 -07001318 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001319 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 current->state = TASK_INTERRUPTIBLE;
1322
1323 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001324 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001325 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ret = -EAGAIN;
1327 break;
1328 }
1329 if (signal_pending(current)) {
1330 ret = -ERESTARTSYS;
1331 break;
1332 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001333 if (tun->dev->reg_state != NETREG_REGISTERED) {
1334 ret = -EIO;
1335 break;
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* Nothing to read, let's sleep */
1339 schedule();
1340 continue;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jason Wang54f968d2012-10-31 19:45:57 +00001343 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001344 kfree_skb(skb);
1345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
1348 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001349 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001350 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001352 return ret;
1353}
1354
1355static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1356 unsigned long count, loff_t pos)
1357{
1358 struct file *file = iocb->ki_filp;
1359 struct tun_file *tfile = file->private_data;
1360 struct tun_struct *tun = __tun_get(tfile);
1361 ssize_t len, ret;
1362
1363 if (!tun)
1364 return -EBADFD;
1365 len = iov_length(iv, count);
1366 if (len < 0) {
1367 ret = -EINVAL;
1368 goto out;
1369 }
1370
Jason Wang54f968d2012-10-31 19:45:57 +00001371 ret = tun_do_read(tun, tfile, iocb, iv, len,
1372 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001373 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001374out:
1375 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return ret;
1377}
1378
Jason Wang96442e422012-10-31 19:46:02 +00001379static void tun_free_netdev(struct net_device *dev)
1380{
1381 struct tun_struct *tun = netdev_priv(dev);
1382
Jason Wang4008e972012-12-13 23:53:30 +00001383 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001384 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001385 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001386 free_netdev(dev);
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static void tun_setup(struct net_device *dev)
1390{
1391 struct tun_struct *tun = netdev_priv(dev);
1392
Eric W. Biederman0625c882012-02-07 16:48:55 -08001393 tun->owner = INVALID_UID;
1394 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001397 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001400/* Trivial set of netlink ops to allow deleting tun or tap
1401 * device with netlink.
1402 */
1403static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1404{
1405 return -EINVAL;
1406}
1407
1408static struct rtnl_link_ops tun_link_ops __read_mostly = {
1409 .kind = DRV_NAME,
1410 .priv_size = sizeof(struct tun_struct),
1411 .setup = tun_setup,
1412 .validate = tun_validate,
1413};
1414
Herbert Xu33dccbb2009-02-05 21:25:32 -08001415static void tun_sock_write_space(struct sock *sk)
1416{
Jason Wang54f968d2012-10-31 19:45:57 +00001417 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001418 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001419
1420 if (!sock_writeable(sk))
1421 return;
1422
Herbert Xu33dccbb2009-02-05 21:25:32 -08001423 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1424 return;
1425
Eric Dumazet43815482010-04-29 11:01:49 +00001426 wqueue = sk_sleep(sk);
1427 if (wqueue && waitqueue_active(wqueue))
1428 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001429 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001430
Jason Wang54f968d2012-10-31 19:45:57 +00001431 tfile = container_of(sk, struct tun_file, sk);
1432 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001433}
1434
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001435static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1436 struct msghdr *m, size_t total_len)
1437{
Jason Wang54f968d2012-10-31 19:45:57 +00001438 int ret;
1439 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1440 struct tun_struct *tun = __tun_get(tfile);
1441
1442 if (!tun)
1443 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001444 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1445 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1446 tun_put(tun);
1447 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001448}
1449
Jason Wang54f968d2012-10-31 19:45:57 +00001450
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001451static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1452 struct msghdr *m, size_t total_len,
1453 int flags)
1454{
Jason Wang54f968d2012-10-31 19:45:57 +00001455 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1456 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001457 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001458
1459 if (!tun)
1460 return -EBADFD;
1461
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001462 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1463 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001464 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001465 flags & MSG_DONTWAIT);
1466 if (ret > total_len) {
1467 m->msg_flags |= MSG_TRUNC;
1468 ret = flags & MSG_TRUNC ? ret : total_len;
1469 }
Jason Wang54f968d2012-10-31 19:45:57 +00001470 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001471 return ret;
1472}
1473
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001474static int tun_release(struct socket *sock)
1475{
1476 if (sock->sk)
1477 sock_put(sock->sk);
1478 return 0;
1479}
1480
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001481/* Ops structure to mimic raw sockets with tun */
1482static const struct proto_ops tun_socket_ops = {
1483 .sendmsg = tun_sendmsg,
1484 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001485 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001486};
1487
Herbert Xu33dccbb2009-02-05 21:25:32 -08001488static struct proto tun_proto = {
1489 .name = "tun",
1490 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001491 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001492};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001493
David Woodhouse980c9e82009-05-09 22:54:21 -07001494static int tun_flags(struct tun_struct *tun)
1495{
1496 int flags = 0;
1497
1498 if (tun->flags & TUN_TUN_DEV)
1499 flags |= IFF_TUN;
1500 else
1501 flags |= IFF_TAP;
1502
1503 if (tun->flags & TUN_NO_PI)
1504 flags |= IFF_NO_PI;
1505
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001506 /* This flag has no real effect. We track the value for backwards
1507 * compatibility.
1508 */
David Woodhouse980c9e82009-05-09 22:54:21 -07001509 if (tun->flags & TUN_ONE_QUEUE)
1510 flags |= IFF_ONE_QUEUE;
1511
1512 if (tun->flags & TUN_VNET_HDR)
1513 flags |= IFF_VNET_HDR;
1514
Jason Wangc8d68e62012-10-31 19:46:00 +00001515 if (tun->flags & TUN_TAP_MQ)
1516 flags |= IFF_MULTI_QUEUE;
1517
David Woodhouse980c9e82009-05-09 22:54:21 -07001518 return flags;
1519}
1520
1521static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1522 char *buf)
1523{
1524 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1525 return sprintf(buf, "0x%x\n", tun_flags(tun));
1526}
1527
1528static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1529 char *buf)
1530{
1531 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001532 return uid_valid(tun->owner)?
1533 sprintf(buf, "%u\n",
1534 from_kuid_munged(current_user_ns(), tun->owner)):
1535 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001536}
1537
1538static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1539 char *buf)
1540{
1541 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001542 return gid_valid(tun->group) ?
1543 sprintf(buf, "%u\n",
1544 from_kgid_munged(current_user_ns(), tun->group)):
1545 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001546}
1547
1548static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1549static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1550static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1551
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001552static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001555 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct net_device *dev;
1557 int err;
1558
Jason Wang7c0c3b12013-01-11 16:59:33 +00001559 if (tfile->detached)
1560 return -EINVAL;
1561
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001562 dev = __dev_get_by_name(net, ifr->ifr_name);
1563 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001564 if (ifr->ifr_flags & IFF_TUN_EXCL)
1565 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001566 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1567 tun = netdev_priv(dev);
1568 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1569 tun = netdev_priv(dev);
1570 else
1571 return -EINVAL;
1572
Jason Wangcde8b152012-10-31 19:46:01 +00001573 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001574 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001575 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001576 if (err < 0)
1577 return err;
1578
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001579 err = tun_attach(tun, file);
1580 if (err < 0)
1581 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001582
1583 if (tun->flags & TUN_TAP_MQ &&
1584 (tun->numqueues + tun->numdisabled > 1))
1585 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 else {
1588 char *name;
1589 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001590 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1591 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001593 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001594 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001595 err = security_tun_dev_create();
1596 if (err < 0)
1597 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* Set dev type */
1600 if (ifr->ifr_flags & IFF_TUN) {
1601 /* TUN device */
1602 flags |= TUN_TUN_DEV;
1603 name = "tun%d";
1604 } else if (ifr->ifr_flags & IFF_TAP) {
1605 /* TAP device */
1606 flags |= TUN_TAP_DEV;
1607 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001608 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001609 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (*ifr->ifr_name)
1612 name = ifr->ifr_name;
1613
Jason Wangc8d68e62012-10-31 19:46:00 +00001614 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Jason Wangedfb6a12013-01-23 03:59:12 +00001615 tun_setup, queues, queues);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (!dev)
1618 return -ENOMEM;
1619
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001620 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001621 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 tun = netdev_priv(dev);
1624 tun->dev = dev;
1625 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001626 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001627 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Jason Wang54f968d2012-10-31 19:45:57 +00001629 tun->filter_attached = false;
1630 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001631
Jason Wang96442e422012-10-31 19:46:02 +00001632 spin_lock_init(&tun->lock);
1633
Paul Moore5dbbaf22013-01-14 07:12:19 +00001634 err = security_tun_dev_alloc_security(&tun->security);
1635 if (err < 0)
1636 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 tun_net_init(dev);
1639
Paul Mooreb3943ae2012-12-06 05:48:38 +00001640 err = tun_flow_init(tun);
1641 if (err < 0)
Jason Wang96442e422012-10-31 19:46:02 +00001642 goto err_free_dev;
1643
Michał Mirosław88255372011-04-19 06:13:10 +00001644 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1645 TUN_USER_FEATURES;
1646 dev->features = dev->hw_features;
1647
Jason Wang4008e972012-12-13 23:53:30 +00001648 INIT_LIST_HEAD(&tun->disabled);
Jason Wangeb0fb362012-12-02 17:19:45 +00001649 err = tun_attach(tun, file);
1650 if (err < 0)
1651 goto err_free_dev;
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 err = register_netdevice(tun->dev);
1654 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001655 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001656
David Woodhouse980c9e82009-05-09 22:54:21 -07001657 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1658 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1659 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001660 pr_err("Failed to create tun sysfs files\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001661
Jason Wangeb0fb362012-12-02 17:19:45 +00001662 netif_carrier_on(tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
Joe Perches6b8a66e2011-03-02 07:18:10 +00001665 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (ifr->ifr_flags & IFF_NO_PI)
1668 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001669 else
1670 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001672 /* This flag has no real effect. We track the value for backwards
1673 * compatibility.
1674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1676 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001677 else
1678 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Rusty Russellf43798c2008-07-03 03:48:02 -07001680 if (ifr->ifr_flags & IFF_VNET_HDR)
1681 tun->flags |= TUN_VNET_HDR;
1682 else
1683 tun->flags &= ~TUN_VNET_HDR;
1684
Jason Wangc8d68e62012-10-31 19:46:00 +00001685 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1686 tun->flags |= TUN_TAP_MQ;
1687 else
1688 tun->flags &= ~TUN_TAP_MQ;
1689
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001690 /* Make sure persistent devices do not get stuck in
1691 * xoff state.
1692 */
1693 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001694 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 strcpy(ifr->ifr_name, tun->dev->name);
1697 return 0;
1698
1699 err_free_dev:
1700 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return err;
1702}
1703
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001704static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001705 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001706{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001707 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001708
1709 strcpy(ifr->ifr_name, tun->dev->name);
1710
David Woodhouse980c9e82009-05-09 22:54:21 -07001711 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001712
Mark McLoughline3b99552008-08-15 15:09:56 -07001713}
1714
Rusty Russell5228ddc2008-07-03 03:46:16 -07001715/* This is like a cut-down ethtool ops, except done via tun fd so no
1716 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001717static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001718{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001719 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001720
1721 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001722 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001723 arg &= ~TUN_F_CSUM;
1724
1725 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1726 if (arg & TUN_F_TSO_ECN) {
1727 features |= NETIF_F_TSO_ECN;
1728 arg &= ~TUN_F_TSO_ECN;
1729 }
1730 if (arg & TUN_F_TSO4)
1731 features |= NETIF_F_TSO;
1732 if (arg & TUN_F_TSO6)
1733 features |= NETIF_F_TSO6;
1734 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1735 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001736
1737 if (arg & TUN_F_UFO) {
1738 features |= NETIF_F_UFO;
1739 arg &= ~TUN_F_UFO;
1740 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001741 }
1742
1743 /* This gives the user a way to test for new features in future by
1744 * trying to set them. */
1745 if (arg)
1746 return -EINVAL;
1747
Michał Mirosław88255372011-04-19 06:13:10 +00001748 tun->set_features = features;
1749 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001750
1751 return 0;
1752}
1753
Jason Wangc8d68e62012-10-31 19:46:00 +00001754static void tun_detach_filter(struct tun_struct *tun, int n)
1755{
1756 int i;
1757 struct tun_file *tfile;
1758
1759 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001760 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001761 sk_detach_filter(tfile->socket.sk);
1762 }
1763
1764 tun->filter_attached = false;
1765}
1766
1767static int tun_attach_filter(struct tun_struct *tun)
1768{
1769 int i, ret = 0;
1770 struct tun_file *tfile;
1771
1772 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001773 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001774 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1775 if (ret) {
1776 tun_detach_filter(tun, i);
1777 return ret;
1778 }
1779 }
1780
1781 tun->filter_attached = true;
1782 return ret;
1783}
1784
1785static void tun_set_sndbuf(struct tun_struct *tun)
1786{
1787 struct tun_file *tfile;
1788 int i;
1789
1790 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001791 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001792 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1793 }
1794}
1795
Jason Wangcde8b152012-10-31 19:46:01 +00001796static int tun_set_queue(struct file *file, struct ifreq *ifr)
1797{
1798 struct tun_file *tfile = file->private_data;
1799 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001800 int ret = 0;
1801
1802 rtnl_lock();
1803
1804 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001805 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001806 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001807 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001808 goto unlock;
1809 }
1810 ret = security_tun_dev_attach_queue(tun->security);
1811 if (ret < 0)
1812 goto unlock;
1813 ret = tun_attach(tun, file);
Jason Wang4008e972012-12-13 23:53:30 +00001814 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001815 tun = rtnl_dereference(tfile->tun);
Jason Wang4008e972012-12-13 23:53:30 +00001816 if (!tun || !(tun->flags & TUN_TAP_MQ))
1817 ret = -EINVAL;
1818 else
1819 __tun_detach(tfile, false);
1820 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001821 ret = -EINVAL;
1822
Paul Moore5dbbaf22013-01-14 07:12:19 +00001823unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001824 rtnl_unlock();
1825 return ret;
1826}
1827
Arnd Bergmann50857e22009-11-06 22:52:32 -08001828static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1829 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001831 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001832 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 void __user* argp = (void __user*)arg;
1834 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001835 kuid_t owner;
1836 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001837 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001838 int vnet_hdr_sz;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001839 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jason Wangcde8b152012-10-31 19:46:01 +00001841 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001842 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001844 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001845 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001846 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001847 if (cmd == TUNGETFEATURES) {
1848 /* Currently this just means: "what IFF flags are valid?".
1849 * This is needed because we never checked for invalid flags on
1850 * TUNSETIFF. */
1851 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
Jason Wangcde8b152012-10-31 19:46:01 +00001852 IFF_VNET_HDR | IFF_MULTI_QUEUE,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001853 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001854 } else if (cmd == TUNSETQUEUE)
1855 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001856
Jason Wangc8d68e62012-10-31 19:46:00 +00001857 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001858 rtnl_lock();
1859
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001860 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1863
Herbert Xu876bfd42009-08-06 14:22:44 +00001864 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Herbert Xu876bfd42009-08-06 14:22:44 +00001866 if (ret)
1867 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Arnd Bergmann50857e22009-11-06 22:52:32 -08001869 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001870 ret = -EFAULT;
1871 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873
Herbert Xu876bfd42009-08-06 14:22:44 +00001874 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001876 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Jason Wang1e588332012-10-31 19:45:56 +00001878 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Eric W. Biederman631ab462009-01-20 11:00:40 +00001880 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001882 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001883 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001884
Arnd Bergmann50857e22009-11-06 22:52:32 -08001885 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001886 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001887 break;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 case TUNSETNOCSUM:
1890 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Michał Mirosław88255372011-04-19 06:13:10 +00001892 /* [unimplemented] */
1893 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001894 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 break;
1896
1897 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001898 /* Disable/Enable persist mode. Keep an extra reference to the
1899 * module to prevent the module being unprobed.
1900 */
Jason Wangdd38bd82013-01-11 16:59:34 +00001901 if (arg && !(tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001903 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001904 }
1905 if (!arg && (tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001907 module_put(THIS_MODULE);
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Joe Perches6b8a66e2011-03-02 07:18:10 +00001910 tun_debug(KERN_INFO, tun, "persist %s\n",
1911 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 break;
1913
1914 case TUNSETOWNER:
1915 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001916 owner = make_kuid(current_user_ns(), arg);
1917 if (!uid_valid(owner)) {
1918 ret = -EINVAL;
1919 break;
1920 }
1921 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001922 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001923 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925
Guido Guenther8c644622007-07-02 22:50:25 -07001926 case TUNSETGROUP:
1927 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001928 group = make_kgid(current_user_ns(), arg);
1929 if (!gid_valid(group)) {
1930 ret = -EINVAL;
1931 break;
1932 }
1933 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001934 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001935 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001936 break;
1937
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001938 case TUNSETLINK:
1939 /* Only allow setting the type when the interface is down */
1940 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001941 tun_debug(KERN_INFO, tun,
1942 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001943 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001944 } else {
1945 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001946 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1947 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001948 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001949 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001950 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952#ifdef TUN_DEBUG
1953 case TUNSETDEBUG:
1954 tun->debug = arg;
1955 break;
1956#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001957 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001958 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001959 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001960
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001961 case TUNSETTXFILTER:
1962 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001963 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001964 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001965 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001966 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001970 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001971 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1972 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001973 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001974 ret = -EFAULT;
1975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001978 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001979 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1980 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001981
Kim B. Heino40102372008-02-29 12:26:21 -08001982 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001983 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001984
1985 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001986 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001987 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1988 ret = -EFAULT;
1989 break;
1990
1991 case TUNSETSNDBUF:
1992 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1993 ret = -EFAULT;
1994 break;
1995 }
1996
Jason Wangc8d68e62012-10-31 19:46:00 +00001997 tun->sndbuf = sndbuf;
1998 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001999 break;
2000
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002001 case TUNGETVNETHDRSZ:
2002 vnet_hdr_sz = tun->vnet_hdr_sz;
2003 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2004 ret = -EFAULT;
2005 break;
2006
2007 case TUNSETVNETHDRSZ:
2008 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2009 ret = -EFAULT;
2010 break;
2011 }
2012 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2013 ret = -EINVAL;
2014 break;
2015 }
2016
2017 tun->vnet_hdr_sz = vnet_hdr_sz;
2018 break;
2019
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002020 case TUNATTACHFILTER:
2021 /* Can be set only for TAPs */
2022 ret = -EINVAL;
2023 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2024 break;
2025 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002026 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002027 break;
2028
Jason Wangc8d68e62012-10-31 19:46:00 +00002029 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002030 break;
2031
2032 case TUNDETACHFILTER:
2033 /* Can be set only for TAPs */
2034 ret = -EINVAL;
2035 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2036 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002037 ret = 0;
2038 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002039 break;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002042 ret = -EINVAL;
2043 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Herbert Xu876bfd42009-08-06 14:22:44 +00002046unlock:
2047 rtnl_unlock();
2048 if (tun)
2049 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Arnd Bergmann50857e22009-11-06 22:52:32 -08002053static long tun_chr_ioctl(struct file *file,
2054 unsigned int cmd, unsigned long arg)
2055{
2056 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2057}
2058
2059#ifdef CONFIG_COMPAT
2060static long tun_chr_compat_ioctl(struct file *file,
2061 unsigned int cmd, unsigned long arg)
2062{
2063 switch (cmd) {
2064 case TUNSETIFF:
2065 case TUNGETIFF:
2066 case TUNSETTXFILTER:
2067 case TUNGETSNDBUF:
2068 case TUNSETSNDBUF:
2069 case SIOCGIFHWADDR:
2070 case SIOCSIFHWADDR:
2071 arg = (unsigned long)compat_ptr(arg);
2072 break;
2073 default:
2074 arg = (compat_ulong_t)arg;
2075 break;
2076 }
2077
2078 /*
2079 * compat_ifreq is shorter than ifreq, so we must not access beyond
2080 * the end of that structure. All fields that are used in this
2081 * driver are compatible though, we don't need to convert the
2082 * contents.
2083 */
2084 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2085}
2086#endif /* CONFIG_COMPAT */
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static int tun_chr_fasync(int fd, struct file *file, int on)
2089{
Jason Wang54f968d2012-10-31 19:45:57 +00002090 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int ret;
2092
Jason Wang54f968d2012-10-31 19:45:57 +00002093 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002094 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002097 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002099 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00002100 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002101 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002102 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002103 ret = 0;
2104out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
2108static int tun_chr_open(struct inode *inode, struct file * file)
2109{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002110 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002111
Joe Perches6b8a66e2011-03-02 07:18:10 +00002112 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002113
Jason Wang54f968d2012-10-31 19:45:57 +00002114 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2115 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002116 if (!tfile)
2117 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00002118 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002119 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002120 tfile->flags = 0;
2121
2122 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
2123 init_waitqueue_head(&tfile->wq.wait);
2124
2125 tfile->socket.file = file;
2126 tfile->socket.ops = &tun_socket_ops;
2127
2128 sock_init_data(&tfile->socket, &tfile->sk);
2129 sk_change_net(&tfile->sk, tfile->net);
2130
2131 tfile->sk.sk_write_space = tun_sock_write_space;
2132 tfile->sk.sk_sndbuf = INT_MAX;
2133
Eric W. Biederman631ab462009-01-20 11:00:40 +00002134 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002135 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002136 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140
2141static int tun_chr_close(struct inode *inode, struct file *file)
2142{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002143 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002144 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Jason Wangc8d68e62012-10-31 19:46:00 +00002146 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002147 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 return 0;
2150}
2151
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002152static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002153 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002155 .read = do_sync_read,
2156 .aio_read = tun_chr_aio_read,
2157 .write = do_sync_write,
2158 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002160 .unlocked_ioctl = tun_chr_ioctl,
2161#ifdef CONFIG_COMPAT
2162 .compat_ioctl = tun_chr_compat_ioctl,
2163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 .open = tun_chr_open,
2165 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002166 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167};
2168
2169static struct miscdevice tun_miscdev = {
2170 .minor = TUN_MINOR,
2171 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002172 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174};
2175
2176/* ethtool interface */
2177
2178static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2179{
2180 cmd->supported = 0;
2181 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002182 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 cmd->duplex = DUPLEX_FULL;
2184 cmd->port = PORT_TP;
2185 cmd->phy_address = 0;
2186 cmd->transceiver = XCVR_INTERNAL;
2187 cmd->autoneg = AUTONEG_DISABLE;
2188 cmd->maxtxpkt = 0;
2189 cmd->maxrxpkt = 0;
2190 return 0;
2191}
2192
2193static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2194{
2195 struct tun_struct *tun = netdev_priv(dev);
2196
Rick Jones33a5ba12011-11-15 14:59:53 +00002197 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2198 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 switch (tun->flags & TUN_TYPE_MASK) {
2201 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002202 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 break;
2204 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002205 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207 }
2208}
2209
2210static u32 tun_get_msglevel(struct net_device *dev)
2211{
2212#ifdef TUN_DEBUG
2213 struct tun_struct *tun = netdev_priv(dev);
2214 return tun->debug;
2215#else
2216 return -EOPNOTSUPP;
2217#endif
2218}
2219
2220static void tun_set_msglevel(struct net_device *dev, u32 value)
2221{
2222#ifdef TUN_DEBUG
2223 struct tun_struct *tun = netdev_priv(dev);
2224 tun->debug = value;
2225#endif
2226}
2227
Jeff Garzik7282d492006-09-13 14:30:00 -04002228static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 .get_settings = tun_get_settings,
2230 .get_drvinfo = tun_get_drvinfo,
2231 .get_msglevel = tun_get_msglevel,
2232 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002233 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234};
2235
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237static int __init tun_init(void)
2238{
2239 int ret = 0;
2240
Joe Perches6b8a66e2011-03-02 07:18:10 +00002241 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2242 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002244 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002245 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002246 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002247 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002248 }
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002251 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002252 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002253 goto err_misc;
2254 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002255 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002256err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002257 rtnl_link_unregister(&tun_link_ops);
2258err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return ret;
2260}
2261
2262static void tun_cleanup(void)
2263{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002264 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002265 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002268/* Get an underlying socket object from tun file. Returns error unless file is
2269 * attached to a device. The returned object works like a packet socket, it
2270 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2271 * holding a reference to the file for as long as the socket is in use. */
2272struct socket *tun_get_socket(struct file *file)
2273{
Jason Wang6e914fc2012-10-31 19:45:58 +00002274 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002275 if (file->f_op != &tun_fops)
2276 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002277 tfile = file->private_data;
2278 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002279 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002280 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002281}
2282EXPORT_SYMBOL_GPL(tun_get_socket);
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284module_init(tun_init);
2285module_exit(tun_cleanup);
2286MODULE_DESCRIPTION(DRV_DESCRIPTION);
2287MODULE_AUTHOR(DRV_COPYRIGHT);
2288MODULE_LICENSE("GPL");
2289MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002290MODULE_ALIAS("devname:net/tun");