blob: ffdb84474c47a52ffa393966b5430fcf35fc7137 [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 Krasnyanskyf271b2cc2008-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 Krasnyanskyf271b2cc2008-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) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000442 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
443 netif_carrier_off(tun->dev);
444
445 if (!(tun->flags & TUN_PERSIST) &&
446 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000447 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000448 }
Jason Wang4008e972012-12-13 23:53:30 +0000449
Jason Wangc8d68e62012-10-31 19:46:00 +0000450 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
451 &tfile->socket.flags));
452 sk_release_kernel(&tfile->sk);
453 }
454}
455
456static void tun_detach(struct tun_file *tfile, bool clean)
457{
458 rtnl_lock();
459 __tun_detach(tfile, clean);
460 rtnl_unlock();
461}
462
463static void tun_detach_all(struct net_device *dev)
464{
465 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000466 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000467 int i, n = tun->numqueues;
468
469 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000470 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000471 BUG_ON(!tfile);
472 wake_up_all(&tfile->wq.wait);
473 rcu_assign_pointer(tfile->tun, NULL);
474 --tun->numqueues;
475 }
476 BUG_ON(tun->numqueues != 0);
477
478 synchronize_net();
479 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000480 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000481 /* Drop read queue */
482 skb_queue_purge(&tfile->sk.sk_receive_queue);
483 sock_put(&tfile->sk);
484 }
Jason Wang4008e972012-12-13 23:53:30 +0000485 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
486 tun_enable_queue(tfile);
487 skb_queue_purge(&tfile->sk.sk_receive_queue);
488 sock_put(&tfile->sk);
489 }
490 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000491
492 if (tun->flags & TUN_PERSIST)
493 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000494}
495
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000496static int tun_attach(struct tun_struct *tun, struct file *file)
497{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000498 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000499 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000500
Paul Moore5dbbaf22013-01-14 07:12:19 +0000501 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
502 if (err < 0)
503 goto out;
504
Eric W. Biederman38231b72009-01-20 11:02:28 +0000505 err = -EINVAL;
Jason Wangb8deabd2013-01-11 16:59:32 +0000506 if (rtnl_dereference(tfile->tun))
Eric W. Biederman38231b72009-01-20 11:02:28 +0000507 goto out;
508
509 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000510 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
511 goto out;
512
513 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000514 if (!tfile->detached &&
515 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000516 goto out;
517
518 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000519
Jason Wangc8d68e62012-10-31 19:46:00 +0000520 /* Re-attach the filter to presist device */
Jason Wang54f968d2012-10-31 19:45:57 +0000521 if (tun->filter_attached == true) {
522 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
523 if (!err)
524 goto out;
525 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000526 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000527 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000528 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000529 tun->numqueues++;
530
Jason Wang4008e972012-12-13 23:53:30 +0000531 if (tfile->detached)
532 tun_enable_queue(tfile);
533 else
534 sock_hold(&tfile->sk);
535
Jason Wangc8d68e62012-10-31 19:46:00 +0000536 tun_set_real_num_queues(tun);
537
Jason Wangc8d68e62012-10-31 19:46:00 +0000538 /* device is allowed to go away first, so no need to hold extra
539 * refcnt.
540 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000541
Eric W. Biederman38231b72009-01-20 11:02:28 +0000542out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000543 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000544}
545
Eric W. Biederman631ab462009-01-20 11:00:40 +0000546static struct tun_struct *__tun_get(struct tun_file *tfile)
547{
Jason Wang6e914fc2012-10-31 19:45:58 +0000548 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000549
Jason Wang6e914fc2012-10-31 19:45:58 +0000550 rcu_read_lock();
551 tun = rcu_dereference(tfile->tun);
552 if (tun)
553 dev_hold(tun->dev);
554 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000555
556 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000557}
558
559static struct tun_struct *tun_get(struct file *file)
560{
561 return __tun_get(file->private_data);
562}
563
564static void tun_put(struct tun_struct *tun)
565{
Jason Wang6e914fc2012-10-31 19:45:58 +0000566 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000567}
568
Joe Perches6b8a66e2011-03-02 07:18:10 +0000569/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700570static void addr_hash_set(u32 *mask, const u8 *addr)
571{
572 int n = ether_crc(ETH_ALEN, addr) >> 26;
573 mask[n >> 5] |= (1 << (n & 31));
574}
575
576static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
577{
578 int n = ether_crc(ETH_ALEN, addr) >> 26;
579 return mask[n >> 5] & (1 << (n & 31));
580}
581
582static int update_filter(struct tap_filter *filter, void __user *arg)
583{
584 struct { u8 u[ETH_ALEN]; } *addr;
585 struct tun_filter uf;
586 int err, alen, n, nexact;
587
588 if (copy_from_user(&uf, arg, sizeof(uf)))
589 return -EFAULT;
590
591 if (!uf.count) {
592 /* Disabled */
593 filter->count = 0;
594 return 0;
595 }
596
597 alen = ETH_ALEN * uf.count;
598 addr = kmalloc(alen, GFP_KERNEL);
599 if (!addr)
600 return -ENOMEM;
601
602 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
603 err = -EFAULT;
604 goto done;
605 }
606
607 /* The filter is updated without holding any locks. Which is
608 * perfectly safe. We disable it first and in the worst
609 * case we'll accept a few undesired packets. */
610 filter->count = 0;
611 wmb();
612
613 /* Use first set of addresses as an exact filter */
614 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
615 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
616
617 nexact = n;
618
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800619 /* Remaining multicast addresses are hashed,
620 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700621 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800622 for (; n < uf.count; n++) {
623 if (!is_multicast_ether_addr(addr[n].u)) {
624 err = 0; /* no filter */
625 goto done;
626 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700627 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800628 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700629
630 /* For ALLMULTI just set the mask to all ones.
631 * This overrides the mask populated above. */
632 if ((uf.flags & TUN_FLT_ALLMULTI))
633 memset(filter->mask, ~0, sizeof(filter->mask));
634
635 /* Now enable the filter */
636 wmb();
637 filter->count = nexact;
638
639 /* Return the number of exact filters */
640 err = nexact;
641
642done:
643 kfree(addr);
644 return err;
645}
646
647/* Returns: 0 - drop, !=0 - accept */
648static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
649{
650 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
651 * at this point. */
652 struct ethhdr *eh = (struct ethhdr *) skb->data;
653 int i;
654
655 /* Exact match */
656 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000657 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700658 return 1;
659
660 /* Inexact match (multicast only) */
661 if (is_multicast_ether_addr(eh->h_dest))
662 return addr_hash_test(filter->mask, eh->h_dest);
663
664 return 0;
665}
666
667/*
668 * Checks whether the packet is accepted or not.
669 * Returns: 0 - drop, !=0 - accept
670 */
671static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
672{
673 if (!filter->count)
674 return 1;
675
676 return run_filter(filter, skb);
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/* Network device part of the driver */
680
Jeff Garzik7282d492006-09-13 14:30:00 -0400681static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000683/* Net device detach from fd. */
684static void tun_net_uninit(struct net_device *dev)
685{
Jason Wangc8d68e62012-10-31 19:46:00 +0000686 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* Net device open. */
690static int tun_net_open(struct net_device *dev)
691{
Jason Wangc8d68e62012-10-31 19:46:00 +0000692 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694}
695
696/* Net device close. */
697static int tun_net_close(struct net_device *dev)
698{
Jason Wangc8d68e62012-10-31 19:46:00 +0000699 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
703/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000704static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000707 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000708 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jason Wang6e914fc2012-10-31 19:45:58 +0000710 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000711 tfile = rcu_dereference(tun->tfiles[txq]);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000714 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto drop;
716
Jason Wang6e914fc2012-10-31 19:45:58 +0000717 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
718
Jason Wangc8d68e62012-10-31 19:46:00 +0000719 BUG_ON(!tfile);
720
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700721 /* Drop if the filter does not like it.
722 * This is a noop if the filter is disabled.
723 * Filter can be enabled only for the TAP devices. */
724 if (!check_filter(&tun->txflt, skb))
725 goto drop;
726
Jason Wang54f968d2012-10-31 19:45:57 +0000727 if (tfile->socket.sk->sk_filter &&
728 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000729 goto drop;
730
Rami Rosen36fe8c092012-11-25 22:07:40 +0000731 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000732 * number of queues.
733 */
Jason Wang54f968d2012-10-31 19:45:57 +0000734 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000735 >= dev->tx_queue_len / tun->numqueues)
736 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000738 /* Orphan the skb - required as we might hang on to it
739 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000740 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
741 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000742 skb_orphan(skb);
743
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700744 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000745 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000748 if (tfile->flags & TUN_FASYNC)
749 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
750 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000751 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000752
753 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000754 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700757 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000758 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000760 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000761 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700764static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700766 /*
767 * This callback is supposed to deal with mc filter in
768 * _rx_ path and has nothing to do with the _tx_ path.
769 * In rx path we always accept everything userspace gives us.
770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Ed Swierk4885a502007-09-16 12:21:38 -0700773#define MIN_MTU 68
774#define MAX_MTU 65535
775
776static int
777tun_net_change_mtu(struct net_device *dev, int new_mtu)
778{
779 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
780 return -EINVAL;
781 dev->mtu = new_mtu;
782 return 0;
783}
784
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000785static netdev_features_t tun_net_fix_features(struct net_device *dev,
786 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000787{
788 struct tun_struct *tun = netdev_priv(dev);
789
790 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
791}
Neil Hormanbebd0972011-06-15 05:25:01 +0000792#ifdef CONFIG_NET_POLL_CONTROLLER
793static void tun_poll_controller(struct net_device *dev)
794{
795 /*
796 * Tun only receives frames when:
797 * 1) the char device endpoint gets data from user space
798 * 2) the tun socket gets a sendmsg call from user space
799 * Since both of those are syncronous operations, we are guaranteed
800 * never to have pending data when we poll for it
801 * so theres nothing to do here but return.
802 * We need this though so netpoll recognizes us as an interface that
803 * supports polling, which enables bridge devices in virt setups to
804 * still use netconsole
805 */
806 return;
807}
808#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800809static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000810 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800811 .ndo_open = tun_net_open,
812 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800813 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800814 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000815 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000816 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000817#ifdef CONFIG_NET_POLL_CONTROLLER
818 .ndo_poll_controller = tun_poll_controller,
819#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800820};
821
822static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000823 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800824 .ndo_open = tun_net_open,
825 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800826 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800827 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000828 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000829 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800830 .ndo_set_mac_address = eth_mac_addr,
831 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000832 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000833#ifdef CONFIG_NET_POLL_CONTROLLER
834 .ndo_poll_controller = tun_poll_controller,
835#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800836};
837
Jason Wang96442e422012-10-31 19:46:02 +0000838static int tun_flow_init(struct tun_struct *tun)
839{
840 int i;
841
Jason Wang96442e422012-10-31 19:46:02 +0000842 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
843 INIT_HLIST_HEAD(&tun->flows[i]);
844
845 tun->ageing_time = TUN_FLOW_EXPIRE;
846 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
847 mod_timer(&tun->flow_gc_timer,
848 round_jiffies_up(jiffies + tun->ageing_time));
849
850 return 0;
851}
852
853static void tun_flow_uninit(struct tun_struct *tun)
854{
855 del_timer_sync(&tun->flow_gc_timer);
856 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/* Initialize net device. */
860static void tun_net_init(struct net_device *dev)
861{
862 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 switch (tun->flags & TUN_TYPE_MASK) {
865 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800866 dev->netdev_ops = &tun_netdev_ops;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Point-to-Point TUN Device */
869 dev->hard_header_len = 0;
870 dev->addr_len = 0;
871 dev->mtu = 1500;
872
873 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400874 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
876 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
877 break;
878
879 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800880 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700882 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000883 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000884 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000886 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
889 break;
890 }
891}
892
893/* Character device part */
894
895/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000896static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400897{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000898 struct tun_file *tfile = file->private_data;
899 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000900 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800901 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000904 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Jason Wang54f968d2012-10-31 19:45:57 +0000906 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000907
Joe Perches6b8a66e2011-03-02 07:18:10 +0000908 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jason Wang54f968d2012-10-31 19:45:57 +0000910 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400911
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000912 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 mask |= POLLIN | POLLRDNORM;
914
Herbert Xu33dccbb2009-02-05 21:25:32 -0800915 if (sock_writeable(sk) ||
916 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
917 sock_writeable(sk)))
918 mask |= POLLOUT | POLLWRNORM;
919
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000920 if (tun->dev->reg_state != NETREG_REGISTERED)
921 mask = POLLERR;
922
Eric W. Biederman631ab462009-01-20 11:00:40 +0000923 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return mask;
925}
926
Rusty Russellf42157c2008-08-15 15:15:10 -0700927/* prepad is the amount to reserve at front. len is length after that.
928 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000929static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000930 size_t prepad, size_t len,
931 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700932{
Jason Wang54f968d2012-10-31 19:45:57 +0000933 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700934 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800935 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700936
937 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700938 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800939 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700940
Herbert Xu33dccbb2009-02-05 21:25:32 -0800941 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
942 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700943 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800944 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700945
946 skb_reserve(skb, prepad);
947 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800948 skb->data_len = len - linear;
949 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700950
951 return skb;
952}
953
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000954/* set skb frags from iovec, this can move to core network code for reuse */
955static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
956 int offset, size_t count)
957{
958 int len = iov_length(from, count) - offset;
959 int copy = skb_headlen(skb);
960 int size, offset1 = 0;
961 int i = 0;
962
963 /* Skip over from offset */
964 while (count && (offset >= from->iov_len)) {
965 offset -= from->iov_len;
966 ++from;
967 --count;
968 }
969
970 /* copy up to skb headlen */
971 while (count && (copy > 0)) {
972 size = min_t(unsigned int, copy, from->iov_len - offset);
973 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
974 size))
975 return -EFAULT;
976 if (copy > size) {
977 ++from;
978 --count;
979 offset = 0;
980 } else
981 offset += size;
982 copy -= size;
983 offset1 += size;
984 }
985
986 if (len == offset1)
987 return 0;
988
989 while (count--) {
990 struct page *page[MAX_SKB_FRAGS];
991 int num_pages;
992 unsigned long base;
993 unsigned long truesize;
994
995 len = from->iov_len - offset;
996 if (!len) {
997 offset = 0;
998 ++from;
999 continue;
1000 }
1001 base = (unsigned long)from->iov_base + offset;
1002 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
1003 if (i + size > MAX_SKB_FRAGS)
1004 return -EMSGSIZE;
1005 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
1006 if (num_pages != size) {
1007 for (i = 0; i < num_pages; i++)
1008 put_page(page[i]);
1009 return -EFAULT;
1010 }
1011 truesize = size * PAGE_SIZE;
1012 skb->data_len += len;
1013 skb->len += len;
1014 skb->truesize += truesize;
1015 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
1016 while (len) {
1017 int off = base & ~PAGE_MASK;
1018 int size = min_t(int, len, PAGE_SIZE - off);
1019 __skb_fill_page_desc(skb, i, page[i], off, size);
1020 skb_shinfo(skb)->nr_frags++;
1021 /* increase sk_wmem_alloc */
1022 base += size;
1023 len -= size;
1024 i++;
1025 }
1026 offset = 0;
1027 ++from;
1028 }
1029 return 0;
1030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001033static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1034 void *msg_control, const struct iovec *iv,
1035 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Harvey Harrison09640e62009-02-01 00:45:17 -08001037 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001039 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -07001040 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001041 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001042 int copylen;
1043 bool zerocopy = false;
1044 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001045 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001048 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EINVAL;
1050
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001051 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001053 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Rusty Russellf43798c2008-07-03 03:48:02 -07001056 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001057 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001058 return -EINVAL;
1059
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001060 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001061 return -EFAULT;
1062
Herbert Xu49091222009-06-08 00:20:01 -07001063 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1064 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1065 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1066
Rusty Russellf43798c2008-07-03 03:48:02 -07001067 if (gso.hdr_len > len)
1068 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001069 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001070 }
1071
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001072 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +00001073 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001074 if (unlikely(len < ETH_HLEN ||
1075 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001076 return -EINVAL;
1077 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001078
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001079 if (msg_control)
1080 zerocopy = true;
1081
1082 if (zerocopy) {
1083 /* Userspace may produce vectors with count greater than
1084 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
1085 * to let the rest of data to be fit in the frags.
1086 */
1087 if (count > MAX_SKB_FRAGS) {
1088 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
1089 if (copylen < offset)
1090 copylen = 0;
1091 else
1092 copylen -= offset;
1093 } else
1094 copylen = 0;
1095 /* There are 256 bytes to be copied in skb, so there is enough
1096 * room for skb expand head in case it is used.
1097 * The rest of the buffer is mapped from userspace.
1098 */
1099 if (copylen < gso.hdr_len)
1100 copylen = gso.hdr_len;
1101 if (!copylen)
1102 copylen = GOODCOPY_LEN;
1103 } else
1104 copylen = len;
1105
Jason Wang54f968d2012-10-31 19:45:57 +00001106 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001107 if (IS_ERR(skb)) {
1108 if (PTR_ERR(skb) != -EAGAIN)
1109 tun->dev->stats.rx_dropped++;
1110 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001113 if (zerocopy)
1114 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
1115 else
1116 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
1117
1118 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001119 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001120 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Rusty Russellf43798c2008-07-03 03:48:02 -07001124 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1125 if (!skb_partial_csum_set(skb, gso.csum_start,
1126 gso.csum_offset)) {
1127 tun->dev->stats.rx_frame_errors++;
1128 kfree_skb(skb);
1129 return -EINVAL;
1130 }
Michał Mirosław88255372011-04-19 06:13:10 +00001131 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 switch (tun->flags & TUN_TYPE_MASK) {
1134 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001135 if (tun->flags & TUN_NO_PI) {
1136 switch (skb->data[0] & 0xf0) {
1137 case 0x40:
1138 pi.proto = htons(ETH_P_IP);
1139 break;
1140 case 0x60:
1141 pi.proto = htons(ETH_P_IPV6);
1142 break;
1143 default:
1144 tun->dev->stats.rx_dropped++;
1145 kfree_skb(skb);
1146 return -EINVAL;
1147 }
1148 }
1149
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001150 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001152 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 case TUN_TAP_DEV:
1155 skb->protocol = eth_type_trans(skb, tun->dev);
1156 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Rusty Russellf43798c2008-07-03 03:48:02 -07001159 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1160 pr_debug("GSO!\n");
1161 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1162 case VIRTIO_NET_HDR_GSO_TCPV4:
1163 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1164 break;
1165 case VIRTIO_NET_HDR_GSO_TCPV6:
1166 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1167 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001168 case VIRTIO_NET_HDR_GSO_UDP:
1169 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1170 break;
Rusty Russellf43798c2008-07-03 03:48:02 -07001171 default:
1172 tun->dev->stats.rx_frame_errors++;
1173 kfree_skb(skb);
1174 return -EINVAL;
1175 }
1176
1177 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1178 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1179
1180 skb_shinfo(skb)->gso_size = gso.gso_size;
1181 if (skb_shinfo(skb)->gso_size == 0) {
1182 tun->dev->stats.rx_frame_errors++;
1183 kfree_skb(skb);
1184 return -EINVAL;
1185 }
1186
1187 /* Header must be checked, and gso_segs computed. */
1188 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1189 skb_shinfo(skb)->gso_segs = 0;
1190 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001191
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001192 /* copy skb_ubuf_info for callback when skb has no error */
1193 if (zerocopy) {
1194 skb_shinfo(skb)->destructor_arg = msg_control;
1195 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1196 }
1197
Eric Dumazet76fe4582012-12-17 04:39:20 +00001198 skb_reset_network_header(skb);
Eric Dumazet49974422012-12-12 19:22:57 +00001199 rxhash = skb_get_rxhash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001201
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001202 tun->dev->stats.rx_packets++;
1203 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Eric Dumazet49974422012-12-12 19:22:57 +00001205 tun_flow_update(tun, rxhash, tfile->queue_index);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001206 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001209static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1210 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001212 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001213 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001214 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001215 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (!tun)
1218 return -EBADFD;
1219
Joe Perches6b8a66e2011-03-02 07:18:10 +00001220 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jason Wang54f968d2012-10-31 19:45:57 +00001222 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1223 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001224
1225 tun_put(tun);
1226 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001230static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001231 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001232 struct sk_buff *skb,
1233 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct tun_pi pi = { 0, skb->protocol };
1236 ssize_t total = 0;
1237
1238 if (!(tun->flags & TUN_NO_PI)) {
1239 if ((len -= sizeof(pi)) < 0)
1240 return -EINVAL;
1241
1242 if (len < skb->len) {
1243 /* Packet will be striped */
1244 pi.flags |= TUN_PKT_STRIP;
1245 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001246
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001247 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EFAULT;
1249 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Rusty Russellf43798c2008-07-03 03:48:02 -07001252 if (tun->flags & TUN_VNET_HDR) {
1253 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001254 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001255 return -EINVAL;
1256
1257 if (skb_is_gso(skb)) {
1258 struct skb_shared_info *sinfo = skb_shinfo(skb);
1259
1260 /* This is a hint as to how much should be linear. */
1261 gso.hdr_len = skb_headlen(skb);
1262 gso.gso_size = sinfo->gso_size;
1263 if (sinfo->gso_type & SKB_GSO_TCPV4)
1264 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1265 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1266 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001267 else if (sinfo->gso_type & SKB_GSO_UDP)
1268 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001269 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001270 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001271 "0x%x, gso_size %d, hdr_len %d\n",
1272 sinfo->gso_type, gso.gso_size,
1273 gso.hdr_len);
1274 print_hex_dump(KERN_ERR, "tun: ",
1275 DUMP_PREFIX_NONE,
1276 16, 1, skb->head,
1277 min((int)gso.hdr_len, 64), true);
1278 WARN_ON_ONCE(1);
1279 return -EINVAL;
1280 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001281 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1282 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1283 } else
1284 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1285
1286 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1287 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001288 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001289 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001290 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1291 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001292 } /* else everything is zero */
1293
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001294 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1295 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001296 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001297 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 len = min_t(int, skb->len, len);
1301
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001302 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001303 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001305 tun->dev->stats.tx_packets++;
1306 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 return total;
1309}
1310
Jason Wang54f968d2012-10-31 19:45:57 +00001311static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001312 struct kiocb *iocb, const struct iovec *iv,
1313 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 DECLARE_WAITQUEUE(wait, current);
1316 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001317 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Rami Rosen3872baf2012-11-25 22:07:41 +00001319 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Amos Kong61a5ff12011-06-09 00:27:10 -07001321 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001322 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 current->state = TASK_INTERRUPTIBLE;
1325
1326 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001327 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001328 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ret = -EAGAIN;
1330 break;
1331 }
1332 if (signal_pending(current)) {
1333 ret = -ERESTARTSYS;
1334 break;
1335 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001336 if (tun->dev->reg_state != NETREG_REGISTERED) {
1337 ret = -EIO;
1338 break;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* Nothing to read, let's sleep */
1342 schedule();
1343 continue;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Jason Wang54f968d2012-10-31 19:45:57 +00001346 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001347 kfree_skb(skb);
1348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001352 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001353 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001355 return ret;
1356}
1357
1358static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1359 unsigned long count, loff_t pos)
1360{
1361 struct file *file = iocb->ki_filp;
1362 struct tun_file *tfile = file->private_data;
1363 struct tun_struct *tun = __tun_get(tfile);
1364 ssize_t len, ret;
1365
1366 if (!tun)
1367 return -EBADFD;
1368 len = iov_length(iv, count);
1369 if (len < 0) {
1370 ret = -EINVAL;
1371 goto out;
1372 }
1373
Jason Wang54f968d2012-10-31 19:45:57 +00001374 ret = tun_do_read(tun, tfile, iocb, iv, len,
1375 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001376 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001377out:
1378 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return ret;
1380}
1381
Jason Wang96442e422012-10-31 19:46:02 +00001382static void tun_free_netdev(struct net_device *dev)
1383{
1384 struct tun_struct *tun = netdev_priv(dev);
1385
Jason Wang4008e972012-12-13 23:53:30 +00001386 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001387 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001388 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001389 free_netdev(dev);
1390}
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392static void tun_setup(struct net_device *dev)
1393{
1394 struct tun_struct *tun = netdev_priv(dev);
1395
Eric W. Biederman0625c882012-02-07 16:48:55 -08001396 tun->owner = INVALID_UID;
1397 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001400 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001403/* Trivial set of netlink ops to allow deleting tun or tap
1404 * device with netlink.
1405 */
1406static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1407{
1408 return -EINVAL;
1409}
1410
1411static struct rtnl_link_ops tun_link_ops __read_mostly = {
1412 .kind = DRV_NAME,
1413 .priv_size = sizeof(struct tun_struct),
1414 .setup = tun_setup,
1415 .validate = tun_validate,
1416};
1417
Herbert Xu33dccbb2009-02-05 21:25:32 -08001418static void tun_sock_write_space(struct sock *sk)
1419{
Jason Wang54f968d2012-10-31 19:45:57 +00001420 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001421 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001422
1423 if (!sock_writeable(sk))
1424 return;
1425
Herbert Xu33dccbb2009-02-05 21:25:32 -08001426 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1427 return;
1428
Eric Dumazet43815482010-04-29 11:01:49 +00001429 wqueue = sk_sleep(sk);
1430 if (wqueue && waitqueue_active(wqueue))
1431 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001432 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001433
Jason Wang54f968d2012-10-31 19:45:57 +00001434 tfile = container_of(sk, struct tun_file, sk);
1435 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001436}
1437
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001438static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1439 struct msghdr *m, size_t total_len)
1440{
Jason Wang54f968d2012-10-31 19:45:57 +00001441 int ret;
1442 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1443 struct tun_struct *tun = __tun_get(tfile);
1444
1445 if (!tun)
1446 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001447 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1448 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1449 tun_put(tun);
1450 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001451}
1452
Jason Wang54f968d2012-10-31 19:45:57 +00001453
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001454static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1455 struct msghdr *m, size_t total_len,
1456 int flags)
1457{
Jason Wang54f968d2012-10-31 19:45:57 +00001458 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1459 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001460 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001461
1462 if (!tun)
1463 return -EBADFD;
1464
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001465 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1466 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001467 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001468 flags & MSG_DONTWAIT);
1469 if (ret > total_len) {
1470 m->msg_flags |= MSG_TRUNC;
1471 ret = flags & MSG_TRUNC ? ret : total_len;
1472 }
Jason Wang54f968d2012-10-31 19:45:57 +00001473 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001474 return ret;
1475}
1476
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001477static int tun_release(struct socket *sock)
1478{
1479 if (sock->sk)
1480 sock_put(sock->sk);
1481 return 0;
1482}
1483
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001484/* Ops structure to mimic raw sockets with tun */
1485static const struct proto_ops tun_socket_ops = {
1486 .sendmsg = tun_sendmsg,
1487 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001488 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001489};
1490
Herbert Xu33dccbb2009-02-05 21:25:32 -08001491static struct proto tun_proto = {
1492 .name = "tun",
1493 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001494 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001495};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001496
David Woodhouse980c9e82009-05-09 22:54:21 -07001497static int tun_flags(struct tun_struct *tun)
1498{
1499 int flags = 0;
1500
1501 if (tun->flags & TUN_TUN_DEV)
1502 flags |= IFF_TUN;
1503 else
1504 flags |= IFF_TAP;
1505
1506 if (tun->flags & TUN_NO_PI)
1507 flags |= IFF_NO_PI;
1508
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001509 /* This flag has no real effect. We track the value for backwards
1510 * compatibility.
1511 */
David Woodhouse980c9e82009-05-09 22:54:21 -07001512 if (tun->flags & TUN_ONE_QUEUE)
1513 flags |= IFF_ONE_QUEUE;
1514
1515 if (tun->flags & TUN_VNET_HDR)
1516 flags |= IFF_VNET_HDR;
1517
Jason Wangc8d68e62012-10-31 19:46:00 +00001518 if (tun->flags & TUN_TAP_MQ)
1519 flags |= IFF_MULTI_QUEUE;
1520
David Woodhouse980c9e82009-05-09 22:54:21 -07001521 return flags;
1522}
1523
1524static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1525 char *buf)
1526{
1527 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1528 return sprintf(buf, "0x%x\n", tun_flags(tun));
1529}
1530
1531static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1532 char *buf)
1533{
1534 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001535 return uid_valid(tun->owner)?
1536 sprintf(buf, "%u\n",
1537 from_kuid_munged(current_user_ns(), tun->owner)):
1538 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001539}
1540
1541static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1542 char *buf)
1543{
1544 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001545 return gid_valid(tun->group) ?
1546 sprintf(buf, "%u\n",
1547 from_kgid_munged(current_user_ns(), tun->group)):
1548 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001549}
1550
1551static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1552static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1553static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1554
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001555static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001558 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct net_device *dev;
1560 int err;
1561
Jason Wang7c0c3b12013-01-11 16:59:33 +00001562 if (tfile->detached)
1563 return -EINVAL;
1564
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001565 dev = __dev_get_by_name(net, ifr->ifr_name);
1566 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001567 if (ifr->ifr_flags & IFF_TUN_EXCL)
1568 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001569 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1570 tun = netdev_priv(dev);
1571 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1572 tun = netdev_priv(dev);
1573 else
1574 return -EINVAL;
1575
Jason Wangcde8b152012-10-31 19:46:01 +00001576 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001577 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001578 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001579 if (err < 0)
1580 return err;
1581
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001582 err = tun_attach(tun, file);
1583 if (err < 0)
1584 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001585
1586 if (tun->flags & TUN_TAP_MQ &&
1587 (tun->numqueues + tun->numdisabled > 1))
1588 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 else {
1591 char *name;
1592 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001593 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1594 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001596 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001597 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001598 err = security_tun_dev_create();
1599 if (err < 0)
1600 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* Set dev type */
1603 if (ifr->ifr_flags & IFF_TUN) {
1604 /* TUN device */
1605 flags |= TUN_TUN_DEV;
1606 name = "tun%d";
1607 } else if (ifr->ifr_flags & IFF_TAP) {
1608 /* TAP device */
1609 flags |= TUN_TAP_DEV;
1610 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001611 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001612 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (*ifr->ifr_name)
1615 name = ifr->ifr_name;
1616
Jason Wangc8d68e62012-10-31 19:46:00 +00001617 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Jason Wangedfb6a12013-01-23 03:59:12 +00001618 tun_setup, queues, queues);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (!dev)
1621 return -ENOMEM;
1622
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001623 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001624 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 tun = netdev_priv(dev);
1627 tun->dev = dev;
1628 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001629 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001630 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Jason Wang54f968d2012-10-31 19:45:57 +00001632 tun->filter_attached = false;
1633 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001634
Jason Wang96442e422012-10-31 19:46:02 +00001635 spin_lock_init(&tun->lock);
1636
Paul Moore5dbbaf22013-01-14 07:12:19 +00001637 err = security_tun_dev_alloc_security(&tun->security);
1638 if (err < 0)
1639 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 tun_net_init(dev);
1642
Paul Mooreb3943ae2012-12-06 05:48:38 +00001643 err = tun_flow_init(tun);
1644 if (err < 0)
Jason Wang96442e422012-10-31 19:46:02 +00001645 goto err_free_dev;
1646
Michał Mirosław88255372011-04-19 06:13:10 +00001647 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1648 TUN_USER_FEATURES;
1649 dev->features = dev->hw_features;
1650
Jason Wang4008e972012-12-13 23:53:30 +00001651 INIT_LIST_HEAD(&tun->disabled);
Jason Wangeb0fb362012-12-02 17:19:45 +00001652 err = tun_attach(tun, file);
1653 if (err < 0)
1654 goto err_free_dev;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 err = register_netdevice(tun->dev);
1657 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001658 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001659
David Woodhouse980c9e82009-05-09 22:54:21 -07001660 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1661 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1662 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001663 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001666 netif_carrier_on(tun->dev);
1667
Joe Perches6b8a66e2011-03-02 07:18:10 +00001668 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 if (ifr->ifr_flags & IFF_NO_PI)
1671 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001672 else
1673 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001675 /* This flag has no real effect. We track the value for backwards
1676 * compatibility.
1677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1679 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001680 else
1681 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Rusty Russellf43798c2008-07-03 03:48:02 -07001683 if (ifr->ifr_flags & IFF_VNET_HDR)
1684 tun->flags |= TUN_VNET_HDR;
1685 else
1686 tun->flags &= ~TUN_VNET_HDR;
1687
Jason Wangc8d68e62012-10-31 19:46:00 +00001688 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1689 tun->flags |= TUN_TAP_MQ;
1690 else
1691 tun->flags &= ~TUN_TAP_MQ;
1692
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001693 /* Make sure persistent devices do not get stuck in
1694 * xoff state.
1695 */
1696 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001697 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 strcpy(ifr->ifr_name, tun->dev->name);
1700 return 0;
1701
1702 err_free_dev:
1703 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return err;
1705}
1706
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001707static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001708 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001709{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001710 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001711
1712 strcpy(ifr->ifr_name, tun->dev->name);
1713
David Woodhouse980c9e82009-05-09 22:54:21 -07001714 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001715
Mark McLoughline3b99552008-08-15 15:09:56 -07001716}
1717
Rusty Russell5228ddc2008-07-03 03:46:16 -07001718/* This is like a cut-down ethtool ops, except done via tun fd so no
1719 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001720static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001721{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001722 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001723
1724 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001725 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001726 arg &= ~TUN_F_CSUM;
1727
1728 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1729 if (arg & TUN_F_TSO_ECN) {
1730 features |= NETIF_F_TSO_ECN;
1731 arg &= ~TUN_F_TSO_ECN;
1732 }
1733 if (arg & TUN_F_TSO4)
1734 features |= NETIF_F_TSO;
1735 if (arg & TUN_F_TSO6)
1736 features |= NETIF_F_TSO6;
1737 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1738 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001739
1740 if (arg & TUN_F_UFO) {
1741 features |= NETIF_F_UFO;
1742 arg &= ~TUN_F_UFO;
1743 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001744 }
1745
1746 /* This gives the user a way to test for new features in future by
1747 * trying to set them. */
1748 if (arg)
1749 return -EINVAL;
1750
Michał Mirosław88255372011-04-19 06:13:10 +00001751 tun->set_features = features;
1752 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001753
1754 return 0;
1755}
1756
Jason Wangc8d68e62012-10-31 19:46:00 +00001757static void tun_detach_filter(struct tun_struct *tun, int n)
1758{
1759 int i;
1760 struct tun_file *tfile;
1761
1762 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001763 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001764 sk_detach_filter(tfile->socket.sk);
1765 }
1766
1767 tun->filter_attached = false;
1768}
1769
1770static int tun_attach_filter(struct tun_struct *tun)
1771{
1772 int i, ret = 0;
1773 struct tun_file *tfile;
1774
1775 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001776 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001777 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1778 if (ret) {
1779 tun_detach_filter(tun, i);
1780 return ret;
1781 }
1782 }
1783
1784 tun->filter_attached = true;
1785 return ret;
1786}
1787
1788static void tun_set_sndbuf(struct tun_struct *tun)
1789{
1790 struct tun_file *tfile;
1791 int i;
1792
1793 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001794 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001795 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1796 }
1797}
1798
Jason Wangcde8b152012-10-31 19:46:01 +00001799static int tun_set_queue(struct file *file, struct ifreq *ifr)
1800{
1801 struct tun_file *tfile = file->private_data;
1802 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001803 int ret = 0;
1804
1805 rtnl_lock();
1806
1807 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001808 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001809 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001810 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001811 goto unlock;
1812 }
1813 ret = security_tun_dev_attach_queue(tun->security);
1814 if (ret < 0)
1815 goto unlock;
1816 ret = tun_attach(tun, file);
Jason Wang4008e972012-12-13 23:53:30 +00001817 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001818 tun = rtnl_dereference(tfile->tun);
Jason Wang4008e972012-12-13 23:53:30 +00001819 if (!tun || !(tun->flags & TUN_TAP_MQ))
1820 ret = -EINVAL;
1821 else
1822 __tun_detach(tfile, false);
1823 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001824 ret = -EINVAL;
1825
Paul Moore5dbbaf22013-01-14 07:12:19 +00001826unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001827 rtnl_unlock();
1828 return ret;
1829}
1830
Arnd Bergmann50857e22009-11-06 22:52:32 -08001831static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1832 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001834 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001835 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 void __user* argp = (void __user*)arg;
1837 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001838 kuid_t owner;
1839 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001840 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001841 int vnet_hdr_sz;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001842 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jason Wangcde8b152012-10-31 19:46:01 +00001844 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001845 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001847 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001848 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001849 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001850 if (cmd == TUNGETFEATURES) {
1851 /* Currently this just means: "what IFF flags are valid?".
1852 * This is needed because we never checked for invalid flags on
1853 * TUNSETIFF. */
1854 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
Jason Wangcde8b152012-10-31 19:46:01 +00001855 IFF_VNET_HDR | IFF_MULTI_QUEUE,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001856 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001857 } else if (cmd == TUNSETQUEUE)
1858 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001859
Jason Wangc8d68e62012-10-31 19:46:00 +00001860 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001861 rtnl_lock();
1862
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001863 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1866
Herbert Xu876bfd42009-08-06 14:22:44 +00001867 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Herbert Xu876bfd42009-08-06 14:22:44 +00001869 if (ret)
1870 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Arnd Bergmann50857e22009-11-06 22:52:32 -08001872 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001873 ret = -EFAULT;
1874 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876
Herbert Xu876bfd42009-08-06 14:22:44 +00001877 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001879 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jason Wang1e588332012-10-31 19:45:56 +00001881 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Eric W. Biederman631ab462009-01-20 11:00:40 +00001883 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001885 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001886 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001887
Arnd Bergmann50857e22009-11-06 22:52:32 -08001888 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001889 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001890 break;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 case TUNSETNOCSUM:
1893 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Michał Mirosław88255372011-04-19 06:13:10 +00001895 /* [unimplemented] */
1896 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001897 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899
1900 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001901 /* Disable/Enable persist mode. Keep an extra reference to the
1902 * module to prevent the module being unprobed.
1903 */
Jason Wangdd38bd82013-01-11 16:59:34 +00001904 if (arg && !(tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001906 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001907 }
1908 if (!arg && (tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001910 module_put(THIS_MODULE);
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Joe Perches6b8a66e2011-03-02 07:18:10 +00001913 tun_debug(KERN_INFO, tun, "persist %s\n",
1914 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
1916
1917 case TUNSETOWNER:
1918 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001919 owner = make_kuid(current_user_ns(), arg);
1920 if (!uid_valid(owner)) {
1921 ret = -EINVAL;
1922 break;
1923 }
1924 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001925 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001926 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928
Guido Guenther8c644622007-07-02 22:50:25 -07001929 case TUNSETGROUP:
1930 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001931 group = make_kgid(current_user_ns(), arg);
1932 if (!gid_valid(group)) {
1933 ret = -EINVAL;
1934 break;
1935 }
1936 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001937 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001938 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001939 break;
1940
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001941 case TUNSETLINK:
1942 /* Only allow setting the type when the interface is down */
1943 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001944 tun_debug(KERN_INFO, tun,
1945 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001946 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001947 } else {
1948 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001949 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1950 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001951 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001952 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001953 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955#ifdef TUN_DEBUG
1956 case TUNSETDEBUG:
1957 tun->debug = arg;
1958 break;
1959#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001960 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001961 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001962 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001963
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001964 case TUNSETTXFILTER:
1965 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001966 ret = -EINVAL;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001967 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001968 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001969 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001973 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001974 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1975 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001976 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001977 ret = -EFAULT;
1978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001981 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001982 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1983 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001984
Kim B. Heino40102372008-02-29 12:26:21 -08001985 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001986 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001987
1988 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001989 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001990 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1991 ret = -EFAULT;
1992 break;
1993
1994 case TUNSETSNDBUF:
1995 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1996 ret = -EFAULT;
1997 break;
1998 }
1999
Jason Wangc8d68e62012-10-31 19:46:00 +00002000 tun->sndbuf = sndbuf;
2001 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002002 break;
2003
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002004 case TUNGETVNETHDRSZ:
2005 vnet_hdr_sz = tun->vnet_hdr_sz;
2006 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2007 ret = -EFAULT;
2008 break;
2009
2010 case TUNSETVNETHDRSZ:
2011 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2012 ret = -EFAULT;
2013 break;
2014 }
2015 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2016 ret = -EINVAL;
2017 break;
2018 }
2019
2020 tun->vnet_hdr_sz = vnet_hdr_sz;
2021 break;
2022
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002023 case TUNATTACHFILTER:
2024 /* Can be set only for TAPs */
2025 ret = -EINVAL;
2026 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2027 break;
2028 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002029 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002030 break;
2031
Jason Wangc8d68e62012-10-31 19:46:00 +00002032 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002033 break;
2034
2035 case TUNDETACHFILTER:
2036 /* Can be set only for TAPs */
2037 ret = -EINVAL;
2038 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2039 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002040 ret = 0;
2041 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002042 break;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002045 ret = -EINVAL;
2046 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Herbert Xu876bfd42009-08-06 14:22:44 +00002049unlock:
2050 rtnl_unlock();
2051 if (tun)
2052 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Arnd Bergmann50857e22009-11-06 22:52:32 -08002056static long tun_chr_ioctl(struct file *file,
2057 unsigned int cmd, unsigned long arg)
2058{
2059 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2060}
2061
2062#ifdef CONFIG_COMPAT
2063static long tun_chr_compat_ioctl(struct file *file,
2064 unsigned int cmd, unsigned long arg)
2065{
2066 switch (cmd) {
2067 case TUNSETIFF:
2068 case TUNGETIFF:
2069 case TUNSETTXFILTER:
2070 case TUNGETSNDBUF:
2071 case TUNSETSNDBUF:
2072 case SIOCGIFHWADDR:
2073 case SIOCSIFHWADDR:
2074 arg = (unsigned long)compat_ptr(arg);
2075 break;
2076 default:
2077 arg = (compat_ulong_t)arg;
2078 break;
2079 }
2080
2081 /*
2082 * compat_ifreq is shorter than ifreq, so we must not access beyond
2083 * the end of that structure. All fields that are used in this
2084 * driver are compatible though, we don't need to convert the
2085 * contents.
2086 */
2087 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2088}
2089#endif /* CONFIG_COMPAT */
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091static int tun_chr_fasync(int fd, struct file *file, int on)
2092{
Jason Wang54f968d2012-10-31 19:45:57 +00002093 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int ret;
2095
Jason Wang54f968d2012-10-31 19:45:57 +00002096 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002097 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002100 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002102 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00002103 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002104 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002105 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002106 ret = 0;
2107out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111static int tun_chr_open(struct inode *inode, struct file * file)
2112{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002113 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002114
Joe Perches6b8a66e2011-03-02 07:18:10 +00002115 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002116
Jason Wang54f968d2012-10-31 19:45:57 +00002117 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2118 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002119 if (!tfile)
2120 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00002121 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002122 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002123 tfile->flags = 0;
2124
2125 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
2126 init_waitqueue_head(&tfile->wq.wait);
2127
2128 tfile->socket.file = file;
2129 tfile->socket.ops = &tun_socket_ops;
2130
2131 sock_init_data(&tfile->socket, &tfile->sk);
2132 sk_change_net(&tfile->sk, tfile->net);
2133
2134 tfile->sk.sk_write_space = tun_sock_write_space;
2135 tfile->sk.sk_sndbuf = INT_MAX;
2136
Eric W. Biederman631ab462009-01-20 11:00:40 +00002137 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002138 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002139 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142}
2143
2144static int tun_chr_close(struct inode *inode, struct file *file)
2145{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002146 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002147 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Jason Wangc8d68e62012-10-31 19:46:00 +00002149 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002150 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 return 0;
2153}
2154
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002155static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002156 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002158 .read = do_sync_read,
2159 .aio_read = tun_chr_aio_read,
2160 .write = do_sync_write,
2161 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002163 .unlocked_ioctl = tun_chr_ioctl,
2164#ifdef CONFIG_COMPAT
2165 .compat_ioctl = tun_chr_compat_ioctl,
2166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 .open = tun_chr_open,
2168 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002169 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170};
2171
2172static struct miscdevice tun_miscdev = {
2173 .minor = TUN_MINOR,
2174 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002175 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
2179/* ethtool interface */
2180
2181static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2182{
2183 cmd->supported = 0;
2184 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002185 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 cmd->duplex = DUPLEX_FULL;
2187 cmd->port = PORT_TP;
2188 cmd->phy_address = 0;
2189 cmd->transceiver = XCVR_INTERNAL;
2190 cmd->autoneg = AUTONEG_DISABLE;
2191 cmd->maxtxpkt = 0;
2192 cmd->maxrxpkt = 0;
2193 return 0;
2194}
2195
2196static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2197{
2198 struct tun_struct *tun = netdev_priv(dev);
2199
Rick Jones33a5ba12011-11-15 14:59:53 +00002200 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2201 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 switch (tun->flags & TUN_TYPE_MASK) {
2204 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002205 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002208 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 break;
2210 }
2211}
2212
2213static u32 tun_get_msglevel(struct net_device *dev)
2214{
2215#ifdef TUN_DEBUG
2216 struct tun_struct *tun = netdev_priv(dev);
2217 return tun->debug;
2218#else
2219 return -EOPNOTSUPP;
2220#endif
2221}
2222
2223static void tun_set_msglevel(struct net_device *dev, u32 value)
2224{
2225#ifdef TUN_DEBUG
2226 struct tun_struct *tun = netdev_priv(dev);
2227 tun->debug = value;
2228#endif
2229}
2230
Jeff Garzik7282d492006-09-13 14:30:00 -04002231static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 .get_settings = tun_get_settings,
2233 .get_drvinfo = tun_get_drvinfo,
2234 .get_msglevel = tun_get_msglevel,
2235 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002236 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237};
2238
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240static int __init tun_init(void)
2241{
2242 int ret = 0;
2243
Joe Perches6b8a66e2011-03-02 07:18:10 +00002244 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2245 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002247 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002248 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002249 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002250 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002251 }
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002254 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002255 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002256 goto err_misc;
2257 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002258 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002259err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002260 rtnl_link_unregister(&tun_link_ops);
2261err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return ret;
2263}
2264
2265static void tun_cleanup(void)
2266{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002267 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002268 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002271/* Get an underlying socket object from tun file. Returns error unless file is
2272 * attached to a device. The returned object works like a packet socket, it
2273 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2274 * holding a reference to the file for as long as the socket is in use. */
2275struct socket *tun_get_socket(struct file *file)
2276{
Jason Wang6e914fc2012-10-31 19:45:58 +00002277 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002278 if (file->f_op != &tun_fops)
2279 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002280 tfile = file->private_data;
2281 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002282 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002283 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002284}
2285EXPORT_SYMBOL_GPL(tun_get_socket);
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287module_init(tun_init);
2288module_exit(tun_cleanup);
2289MODULE_DESCRIPTION(DRV_DESCRIPTION);
2290MODULE_AUTHOR(DRV_COPYRIGHT);
2291MODULE_LICENSE("GPL");
2292MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002293MODULE_ALIAS("devname:net/tun");