blob: b4e4ca01facc50360f7e076a3198b987b2b6d83c [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>
Jason Wang6680ec62013-07-25 13:00:33 +080063#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070065#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070066#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000067#include <linux/rcupdate.h>
Ben Hutchings5188cd42014-10-30 18:27:17 +000068#include <net/ipv6.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070069#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070070#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080071#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080072#include <net/sock.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090073#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
76
Rusty Russell14daa022008-04-12 18:48:58 -070077/* Uncomment to enable debugging */
78/* #define TUN_DEBUG 1 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef TUN_DEBUG
81static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070082
Joe Perches6b8a66e2011-03-02 07:18:10 +000083#define tun_debug(level, tun, fmt, args...) \
84do { \
85 if (tun->debug) \
86 netdev_printk(level, tun->dev, fmt, ##args); \
87} while (0)
88#define DBG1(level, fmt, args...) \
89do { \
90 if (debug == 2) \
91 printk(level fmt, ##args); \
92} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070093#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000094#define tun_debug(level, tun, fmt, args...) \
95do { \
96 if (0) \
97 netdev_printk(level, tun->dev, fmt, ##args); \
98} while (0)
99#define DBG1(level, fmt, args...) \
100do { \
101 if (0) \
102 printk(level fmt, ##args); \
103} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200106/* TUN device flags */
107
108/* IFF_ATTACH_QUEUE is never stored in device flags,
109 * overload it to mean fasync when stored there.
110 */
111#define TUN_FASYNC IFF_ATTACH_QUEUE
112#define TUN_NO_PI IFF_NO_PI
113/* This flag has no real effect */
114#define TUN_ONE_QUEUE IFF_ONE_QUEUE
115#define TUN_PERSIST IFF_PERSIST
116#define TUN_VNET_HDR IFF_VNET_HDR
117#define TUN_TAP_MQ IFF_MULTI_QUEUE
118
119#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
120 IFF_MULTI_QUEUE)
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000121#define GOODCOPY_LEN 128
122
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700123#define FLT_EXACT_COUNT 8
124struct tap_filter {
125 unsigned int count; /* Number of addrs. Zero means disabled */
126 u32 mask[2]; /* Mask of the hashed addrs */
127 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
128};
129
stephen hemminger92d4ea62013-12-05 20:42:58 -0800130/* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
Jason Wangedfb6a12013-01-23 03:59:12 +0000131 * the netdevice to be fit in one page. So we can make sure the success of
132 * memory allocation. TODO: increase the limit. */
133#define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
Jason Wangb8732fb2013-01-23 03:59:13 +0000134#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000135
Jason Wang96442e422012-10-31 19:46:02 +0000136#define TUN_FLOW_EXPIRE (3 * HZ)
137
Jason Wang54f968d2012-10-31 19:45:57 +0000138/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800139 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000140 * to serve as one transmit queue for tuntap device. The sock_fprog and
141 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000142 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000143 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000144 *
145 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000146 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000147 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000148 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000149struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000150 struct sock sk;
151 struct socket socket;
152 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000153 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000154 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000155 struct fasync_struct *fasync;
156 /* only used for fasnyc */
157 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400158 union {
159 u16 queue_index;
160 unsigned int ifindex;
161 };
Jason Wang4008e972012-12-13 23:53:30 +0000162 struct list_head next;
163 struct tun_struct *detached;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000164};
165
Jason Wang96442e422012-10-31 19:46:02 +0000166struct tun_flow_entry {
167 struct hlist_node hash_link;
168 struct rcu_head rcu;
169 struct tun_struct *tun;
170
171 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800172 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000173 int queue_index;
174 unsigned long updated;
175};
176
177#define TUN_NUM_FLOW_ENTRIES 1024
178
Jason Wang54f968d2012-10-31 19:45:57 +0000179/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000180 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000181 * file were attached to a persist device.
182 */
Rusty Russell14daa022008-04-12 18:48:58 -0700183struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000184 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
185 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700186 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800187 kuid_t owner;
188 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700189
Rusty Russell14daa022008-04-12 18:48:58 -0700190 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000191 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000192#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
Ben Hutchings3d0ad092014-10-30 18:27:12 +0000193 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200194
195 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000196 int sndbuf;
197 struct tap_filter txflt;
198 struct sock_fprog fprog;
199 /* protected by rtnl lock */
200 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700201#ifdef TUN_DEBUG
202 int debug;
203#endif
Jason Wang96442e422012-10-31 19:46:02 +0000204 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000205 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
206 struct timer_list flow_gc_timer;
207 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000208 unsigned int numdisabled;
209 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000210 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000211 u32 flow_count;
Rusty Russell14daa022008-04-12 18:48:58 -0700212};
213
Jason Wang96442e422012-10-31 19:46:02 +0000214static inline u32 tun_hashfn(u32 rxhash)
215{
216 return rxhash & 0x3ff;
217}
218
219static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
220{
221 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000222
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000224 if (e->rxhash == rxhash)
225 return e;
226 }
227 return NULL;
228}
229
230static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
231 struct hlist_head *head,
232 u32 rxhash, u16 queue_index)
233{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000234 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
235
Jason Wang96442e422012-10-31 19:46:02 +0000236 if (e) {
237 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
238 rxhash, queue_index);
239 e->updated = jiffies;
240 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800241 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000242 e->queue_index = queue_index;
243 e->tun = tun;
244 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000245 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000246 }
247 return e;
248}
249
Jason Wang96442e422012-10-31 19:46:02 +0000250static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
251{
252 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
253 e->rxhash, e->queue_index);
Tom Herbert9bc88932013-12-22 18:54:32 +0800254 sock_rps_reset_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000255 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000256 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000257 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000258}
259
260static void tun_flow_flush(struct tun_struct *tun)
261{
262 int i;
263
264 spin_lock_bh(&tun->lock);
265 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
266 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800267 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000268
Sasha Levinb67bfe02013-02-27 17:06:00 -0800269 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000270 tun_flow_delete(tun, e);
271 }
272 spin_unlock_bh(&tun->lock);
273}
274
275static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
276{
277 int i;
278
279 spin_lock_bh(&tun->lock);
280 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
281 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800282 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000283
Sasha Levinb67bfe02013-02-27 17:06:00 -0800284 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000285 if (e->queue_index == queue_index)
286 tun_flow_delete(tun, e);
287 }
288 }
289 spin_unlock_bh(&tun->lock);
290}
291
292static void tun_flow_cleanup(unsigned long data)
293{
294 struct tun_struct *tun = (struct tun_struct *)data;
295 unsigned long delay = tun->ageing_time;
296 unsigned long next_timer = jiffies + delay;
297 unsigned long count = 0;
298 int i;
299
300 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
301
302 spin_lock_bh(&tun->lock);
303 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
304 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800305 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000306
Sasha Levinb67bfe02013-02-27 17:06:00 -0800307 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000308 unsigned long this_timer;
309 count++;
310 this_timer = e->updated + delay;
311 if (time_before_eq(this_timer, jiffies))
312 tun_flow_delete(tun, e);
313 else if (time_before(this_timer, next_timer))
314 next_timer = this_timer;
315 }
316 }
317
318 if (count)
319 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
320 spin_unlock_bh(&tun->lock);
321}
322
Eric Dumazet49974422012-12-12 19:22:57 +0000323static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000324 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000325{
326 struct hlist_head *head;
327 struct tun_flow_entry *e;
328 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000329 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000330
331 if (!rxhash)
332 return;
333 else
334 head = &tun->flows[tun_hashfn(rxhash)];
335
336 rcu_read_lock();
337
Jason Wang9e857222013-01-28 01:05:19 +0000338 /* We may get a very small possibility of OOO during switching, not
339 * worth to optimize.*/
340 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000341 goto unlock;
342
343 e = tun_flow_find(head, rxhash);
344 if (likely(e)) {
345 /* TODO: keep queueing to old queue until it's empty? */
346 e->queue_index = queue_index;
347 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800348 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000349 } else {
350 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000351 if (!tun_flow_find(head, rxhash) &&
352 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000353 tun_flow_create(tun, head, rxhash, queue_index);
354
355 if (!timer_pending(&tun->flow_gc_timer))
356 mod_timer(&tun->flow_gc_timer,
357 round_jiffies_up(jiffies + delay));
358 spin_unlock_bh(&tun->lock);
359 }
360
361unlock:
362 rcu_read_unlock();
363}
364
Tom Herbert9bc88932013-12-22 18:54:32 +0800365/**
366 * Save the hash received in the stack receive path and update the
367 * flow_hash table accordingly.
368 */
369static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
370{
371 if (unlikely(e->rps_rxhash != hash)) {
372 sock_rps_reset_flow_hash(e->rps_rxhash);
373 e->rps_rxhash = hash;
374 }
375}
376
Jason Wangc8d68e62012-10-31 19:46:00 +0000377/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800378 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000379 * the rxq based on the txq where the last packet of the flow comes. As
380 * the userspace application move between processors, we may get a
381 * different rxq no. here. If we could not get rxhash, then we would
382 * hope the rxq no. may help here.
383 */
Jason Wangf663dd92014-01-10 16:18:26 +0800384static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100385 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000386{
387 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000388 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000389 u32 txq = 0;
390 u32 numqueues = 0;
391
392 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800393 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000394
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800395 txq = skb_get_hash(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000396 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000397 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800398 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800399 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800400 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800401 } else
Jason Wang96442e422012-10-31 19:46:02 +0000402 /* use multiply and shift instead of expensive divide */
403 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000404 } else if (likely(skb_rx_queue_recorded(skb))) {
405 txq = skb_get_rx_queue(skb);
406 while (unlikely(txq >= numqueues))
407 txq -= numqueues;
408 }
409
410 rcu_read_unlock();
411 return txq;
412}
413
Jason Wangcde8b152012-10-31 19:46:01 +0000414static inline bool tun_not_capable(struct tun_struct *tun)
415{
416 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000417 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000418
419 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
420 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000421 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000422}
423
Jason Wangc8d68e62012-10-31 19:46:00 +0000424static void tun_set_real_num_queues(struct tun_struct *tun)
425{
426 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
427 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
428}
429
Jason Wang4008e972012-12-13 23:53:30 +0000430static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
431{
432 tfile->detached = tun;
433 list_add_tail(&tfile->next, &tun->disabled);
434 ++tun->numdisabled;
435}
436
Jason Wangd32649d2012-12-18 11:00:27 +0800437static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000438{
439 struct tun_struct *tun = tfile->detached;
440
441 tfile->detached = NULL;
442 list_del_init(&tfile->next);
443 --tun->numdisabled;
444 return tun;
445}
446
Jason Wang4bfb0512013-09-05 17:53:59 +0800447static void tun_queue_purge(struct tun_file *tfile)
448{
449 skb_queue_purge(&tfile->sk.sk_receive_queue);
450 skb_queue_purge(&tfile->sk.sk_error_queue);
451}
452
Jason Wangc8d68e62012-10-31 19:46:00 +0000453static void __tun_detach(struct tun_file *tfile, bool clean)
454{
455 struct tun_file *ntfile;
456 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000457
Jason Wangb8deabd2013-01-11 16:59:32 +0000458 tun = rtnl_dereference(tfile->tun);
459
Jason Wang9e857222013-01-28 01:05:19 +0000460 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000461 u16 index = tfile->queue_index;
462 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000463
464 rcu_assign_pointer(tun->tfiles[index],
465 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000466 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000467 ntfile->queue_index = index;
468
469 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000470 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530471 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000472 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000473 } else
Jason Wang4008e972012-12-13 23:53:30 +0000474 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000475
476 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000477 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000478 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800479 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000480 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000481 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000482 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000483 sock_put(&tfile->sk);
484 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000485
486 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000487 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
488 netif_carrier_off(tun->dev);
489
490 if (!(tun->flags & TUN_PERSIST) &&
491 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000492 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000493 }
Jason Wang4008e972012-12-13 23:53:30 +0000494
Jason Wangc8d68e62012-10-31 19:46:00 +0000495 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
496 &tfile->socket.flags));
497 sk_release_kernel(&tfile->sk);
498 }
499}
500
501static void tun_detach(struct tun_file *tfile, bool clean)
502{
503 rtnl_lock();
504 __tun_detach(tfile, clean);
505 rtnl_unlock();
506}
507
508static void tun_detach_all(struct net_device *dev)
509{
510 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000511 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000512 int i, n = tun->numqueues;
513
514 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000515 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000516 BUG_ON(!tfile);
Xi Wang9e641bd2014-05-16 15:11:48 -0700517 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530518 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000519 --tun->numqueues;
520 }
Jason Wang9e857222013-01-28 01:05:19 +0000521 list_for_each_entry(tfile, &tun->disabled, next) {
Xi Wang9e641bd2014-05-16 15:11:48 -0700522 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530523 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000524 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000525 BUG_ON(tun->numqueues != 0);
526
527 synchronize_net();
528 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000529 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000530 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800531 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000532 sock_put(&tfile->sk);
533 }
Jason Wang4008e972012-12-13 23:53:30 +0000534 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
535 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800536 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000537 sock_put(&tfile->sk);
538 }
539 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000540
541 if (tun->flags & TUN_PERSIST)
542 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000543}
544
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400545static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000546{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000547 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000548 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000549
Paul Moore5dbbaf22013-01-14 07:12:19 +0000550 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
551 if (err < 0)
552 goto out;
553
Eric W. Biederman38231b72009-01-20 11:02:28 +0000554 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000555 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000556 goto out;
557
558 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000559 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
560 goto out;
561
562 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000563 if (!tfile->detached &&
564 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000565 goto out;
566
567 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000568
stephen hemminger92d4ea62013-12-05 20:42:58 -0800569 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400570 if (!skip_filter && (tun->filter_attached == true)) {
Jason Wang54f968d2012-10-31 19:45:57 +0000571 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
572 if (!err)
573 goto out;
574 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000575 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000576 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000577 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000578 tun->numqueues++;
579
Jason Wang4008e972012-12-13 23:53:30 +0000580 if (tfile->detached)
581 tun_enable_queue(tfile);
582 else
583 sock_hold(&tfile->sk);
584
Jason Wangc8d68e62012-10-31 19:46:00 +0000585 tun_set_real_num_queues(tun);
586
Jason Wangc8d68e62012-10-31 19:46:00 +0000587 /* device is allowed to go away first, so no need to hold extra
588 * refcnt.
589 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000590
Eric W. Biederman38231b72009-01-20 11:02:28 +0000591out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000592 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000593}
594
Eric W. Biederman631ab462009-01-20 11:00:40 +0000595static struct tun_struct *__tun_get(struct tun_file *tfile)
596{
Jason Wang6e914fc2012-10-31 19:45:58 +0000597 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000598
Jason Wang6e914fc2012-10-31 19:45:58 +0000599 rcu_read_lock();
600 tun = rcu_dereference(tfile->tun);
601 if (tun)
602 dev_hold(tun->dev);
603 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000604
605 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000606}
607
608static struct tun_struct *tun_get(struct file *file)
609{
610 return __tun_get(file->private_data);
611}
612
613static void tun_put(struct tun_struct *tun)
614{
Jason Wang6e914fc2012-10-31 19:45:58 +0000615 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000616}
617
Joe Perches6b8a66e2011-03-02 07:18:10 +0000618/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700619static void addr_hash_set(u32 *mask, const u8 *addr)
620{
621 int n = ether_crc(ETH_ALEN, addr) >> 26;
622 mask[n >> 5] |= (1 << (n & 31));
623}
624
625static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
626{
627 int n = ether_crc(ETH_ALEN, addr) >> 26;
628 return mask[n >> 5] & (1 << (n & 31));
629}
630
631static int update_filter(struct tap_filter *filter, void __user *arg)
632{
633 struct { u8 u[ETH_ALEN]; } *addr;
634 struct tun_filter uf;
635 int err, alen, n, nexact;
636
637 if (copy_from_user(&uf, arg, sizeof(uf)))
638 return -EFAULT;
639
640 if (!uf.count) {
641 /* Disabled */
642 filter->count = 0;
643 return 0;
644 }
645
646 alen = ETH_ALEN * uf.count;
647 addr = kmalloc(alen, GFP_KERNEL);
648 if (!addr)
649 return -ENOMEM;
650
651 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
652 err = -EFAULT;
653 goto done;
654 }
655
656 /* The filter is updated without holding any locks. Which is
657 * perfectly safe. We disable it first and in the worst
658 * case we'll accept a few undesired packets. */
659 filter->count = 0;
660 wmb();
661
662 /* Use first set of addresses as an exact filter */
663 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
664 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
665
666 nexact = n;
667
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800668 /* Remaining multicast addresses are hashed,
669 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700670 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800671 for (; n < uf.count; n++) {
672 if (!is_multicast_ether_addr(addr[n].u)) {
673 err = 0; /* no filter */
674 goto done;
675 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700676 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800677 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700678
679 /* For ALLMULTI just set the mask to all ones.
680 * This overrides the mask populated above. */
681 if ((uf.flags & TUN_FLT_ALLMULTI))
682 memset(filter->mask, ~0, sizeof(filter->mask));
683
684 /* Now enable the filter */
685 wmb();
686 filter->count = nexact;
687
688 /* Return the number of exact filters */
689 err = nexact;
690
691done:
692 kfree(addr);
693 return err;
694}
695
696/* Returns: 0 - drop, !=0 - accept */
697static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
698{
699 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
700 * at this point. */
701 struct ethhdr *eh = (struct ethhdr *) skb->data;
702 int i;
703
704 /* Exact match */
705 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000706 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700707 return 1;
708
709 /* Inexact match (multicast only) */
710 if (is_multicast_ether_addr(eh->h_dest))
711 return addr_hash_test(filter->mask, eh->h_dest);
712
713 return 0;
714}
715
716/*
717 * Checks whether the packet is accepted or not.
718 * Returns: 0 - drop, !=0 - accept
719 */
720static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
721{
722 if (!filter->count)
723 return 1;
724
725 return run_filter(filter, skb);
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/* Network device part of the driver */
729
Jeff Garzik7282d492006-09-13 14:30:00 -0400730static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000732/* Net device detach from fd. */
733static void tun_net_uninit(struct net_device *dev)
734{
Jason Wangc8d68e62012-10-31 19:46:00 +0000735 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* Net device open. */
739static int tun_net_open(struct net_device *dev)
740{
Jason Wangc8d68e62012-10-31 19:46:00 +0000741 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
743}
744
745/* Net device close. */
746static int tun_net_close(struct net_device *dev)
747{
Jason Wangc8d68e62012-10-31 19:46:00 +0000748 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750}
751
752/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000753static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000756 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000757 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000758 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jason Wang6e914fc2012-10-31 19:45:58 +0000760 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000761 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000762 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000765 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto drop;
767
Dominic Curranfa358642014-01-22 03:03:23 +0000768 if (numqueues == 1) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800769 /* Select queue was not called for the skbuff, so we extract the
770 * RPS hash and save it into the flow_table here.
771 */
772 __u32 rxhash;
773
774 rxhash = skb_get_hash(skb);
775 if (rxhash) {
776 struct tun_flow_entry *e;
777 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
778 rxhash);
779 if (e)
780 tun_flow_save_rps_rxhash(e, rxhash);
781 }
782 }
783
Jason Wang6e914fc2012-10-31 19:45:58 +0000784 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
785
Jason Wangc8d68e62012-10-31 19:46:00 +0000786 BUG_ON(!tfile);
787
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700788 /* Drop if the filter does not like it.
789 * This is a noop if the filter is disabled.
790 * Filter can be enabled only for the TAP devices. */
791 if (!check_filter(&tun->txflt, skb))
792 goto drop;
793
Jason Wang54f968d2012-10-31 19:45:57 +0000794 if (tfile->socket.sk->sk_filter &&
795 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000796 goto drop;
797
Rami Rosen36fe8c092012-11-25 22:07:40 +0000798 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000799 * number of queues.
800 */
Dominic Curranfa358642014-01-22 03:03:23 +0000801 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
802 >= dev->tx_queue_len)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000803 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jason Wang7bf66302013-09-05 17:54:00 +0800805 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
806 goto drop;
807
Richard Cochraneda29772013-07-19 19:40:10 +0200808 if (skb->sk) {
809 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
810 sw_tx_timestamp(skb);
811 }
812
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000813 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800814 * for indefinite time.
815 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000816 skb_orphan(skb);
817
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000818 nf_reset(skb);
819
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700820 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000821 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000824 if (tfile->flags & TUN_FASYNC)
825 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700826 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000827
828 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000829 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700832 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000833 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000835 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000836 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700839static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700841 /*
842 * This callback is supposed to deal with mc filter in
843 * _rx_ path and has nothing to do with the _tx_ path.
844 * In rx path we always accept everything userspace gives us.
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Ed Swierk4885a502007-09-16 12:21:38 -0700848#define MIN_MTU 68
849#define MAX_MTU 65535
850
851static int
852tun_net_change_mtu(struct net_device *dev, int new_mtu)
853{
854 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
855 return -EINVAL;
856 dev->mtu = new_mtu;
857 return 0;
858}
859
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000860static netdev_features_t tun_net_fix_features(struct net_device *dev,
861 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000862{
863 struct tun_struct *tun = netdev_priv(dev);
864
865 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
866}
Neil Hormanbebd0972011-06-15 05:25:01 +0000867#ifdef CONFIG_NET_POLL_CONTROLLER
868static void tun_poll_controller(struct net_device *dev)
869{
870 /*
871 * Tun only receives frames when:
872 * 1) the char device endpoint gets data from user space
873 * 2) the tun socket gets a sendmsg call from user space
stephen hemminger92d4ea62013-12-05 20:42:58 -0800874 * Since both of those are synchronous operations, we are guaranteed
Neil Hormanbebd0972011-06-15 05:25:01 +0000875 * never to have pending data when we poll for it
stephen hemminger92d4ea62013-12-05 20:42:58 -0800876 * so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +0000877 * We need this though so netpoll recognizes us as an interface that
878 * supports polling, which enables bridge devices in virt setups to
879 * still use netconsole
880 */
881 return;
882}
883#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800884static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000885 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800886 .ndo_open = tun_net_open,
887 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800888 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800889 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000890 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000891 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000892#ifdef CONFIG_NET_POLL_CONTROLLER
893 .ndo_poll_controller = tun_poll_controller,
894#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800895};
896
897static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000898 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800899 .ndo_open = tun_net_open,
900 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800901 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800902 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000903 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000904 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800905 .ndo_set_mac_address = eth_mac_addr,
906 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000907 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000908#ifdef CONFIG_NET_POLL_CONTROLLER
909 .ndo_poll_controller = tun_poll_controller,
910#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800911};
912
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400913static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000914{
915 int i;
916
Jason Wang96442e422012-10-31 19:46:02 +0000917 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
918 INIT_HLIST_HEAD(&tun->flows[i]);
919
920 tun->ageing_time = TUN_FLOW_EXPIRE;
921 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
922 mod_timer(&tun->flow_gc_timer,
923 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000924}
925
926static void tun_flow_uninit(struct tun_struct *tun)
927{
928 del_timer_sync(&tun->flow_gc_timer);
929 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* Initialize net device. */
933static void tun_net_init(struct net_device *dev)
934{
935 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 switch (tun->flags & TUN_TYPE_MASK) {
938 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800939 dev->netdev_ops = &tun_netdev_ops;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Point-to-Point TUN Device */
942 dev->hard_header_len = 0;
943 dev->addr_len = 0;
944 dev->mtu = 1500;
945
946 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400947 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
949 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
950 break;
951
952 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800953 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700955 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000956 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000957 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000959 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
962 break;
963 }
964}
965
966/* Character device part */
967
968/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000969static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400970{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000971 struct tun_file *tfile = file->private_data;
972 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000973 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800974 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000977 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Jason Wang54f968d2012-10-31 19:45:57 +0000979 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000980
Joe Perches6b8a66e2011-03-02 07:18:10 +0000981 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Xi Wang9e641bd2014-05-16 15:11:48 -0700983 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400984
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000985 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 mask |= POLLIN | POLLRDNORM;
987
Herbert Xu33dccbb2009-02-05 21:25:32 -0800988 if (sock_writeable(sk) ||
989 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
990 sock_writeable(sk)))
991 mask |= POLLOUT | POLLWRNORM;
992
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000993 if (tun->dev->reg_state != NETREG_REGISTERED)
994 mask = POLLERR;
995
Eric W. Biederman631ab462009-01-20 11:00:40 +0000996 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return mask;
998}
999
Rusty Russellf42157c2008-08-15 15:15:10 -07001000/* prepad is the amount to reserve at front. len is length after that.
1001 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001002static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001003 size_t prepad, size_t len,
1004 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001005{
Jason Wang54f968d2012-10-31 19:45:57 +00001006 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001007 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001008 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001009
1010 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001011 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001012 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001013
Herbert Xu33dccbb2009-02-05 21:25:32 -08001014 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001015 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001016 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001017 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001018
1019 skb_reserve(skb, prepad);
1020 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001021 skb->data_len = len - linear;
1022 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001023
1024 return skb;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001028static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1029 void *msg_control, const struct iovec *iv,
1030 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Harvey Harrison09640e632009-02-01 00:45:17 -08001032 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct sk_buff *skb;
Jason Wang3dd5c332013-07-10 13:43:27 +08001034 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001035 struct virtio_net_hdr gso = { 0 };
Jason Wang96f8d9e2013-11-13 14:00:39 +08001036 int good_linear;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001037 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001038 int copylen;
1039 bool zerocopy = false;
1040 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001041 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (!(tun->flags & TUN_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001044 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001046 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001048 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001050 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Rusty Russellf43798c2008-07-03 03:48:02 -07001053 if (tun->flags & TUN_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001054 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001055 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001056 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001057
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001058 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001059 return -EFAULT;
1060
Herbert Xu49091222009-06-08 00:20:01 -07001061 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1062 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1063 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1064
Rusty Russellf43798c2008-07-03 03:48:02 -07001065 if (gso.hdr_len > len)
1066 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001067 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001068 }
1069
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001070 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +00001071 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001072 if (unlikely(len < ETH_HLEN ||
1073 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001074 return -EINVAL;
1075 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001076
Jason Wang96f8d9e2013-11-13 14:00:39 +08001077 good_linear = SKB_MAX_HEAD(align);
1078
Jason Wang88529172013-07-18 10:55:15 +08001079 if (msg_control) {
1080 /* There are 256 bytes to be copied in skb, so there is
1081 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001082 * The rest of the buffer is mapped from userspace.
1083 */
Jason Wang88529172013-07-18 10:55:15 +08001084 copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001085 if (copylen > good_linear)
1086 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001087 linear = copylen;
Jason Wang88529172013-07-18 10:55:15 +08001088 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1089 zerocopy = true;
1090 }
1091
1092 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001093 copylen = len;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001094 if (gso.hdr_len > good_linear)
1095 linear = good_linear;
1096 else
1097 linear = gso.hdr_len;
Jason Wang3dd5c332013-07-10 13:43:27 +08001098 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001099
Jason Wang3dd5c332013-07-10 13:43:27 +08001100 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001101 if (IS_ERR(skb)) {
1102 if (PTR_ERR(skb) != -EAGAIN)
1103 tun->dev->stats.rx_dropped++;
1104 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001107 if (zerocopy)
1108 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
Jason Wang88529172013-07-18 10:55:15 +08001109 else {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001110 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
Jason Wang88529172013-07-18 10:55:15 +08001111 if (!err && msg_control) {
1112 struct ubuf_info *uarg = msg_control;
1113 uarg->callback(uarg, false);
1114 }
1115 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001116
1117 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001118 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001119 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Rusty Russellf43798c2008-07-03 03:48:02 -07001123 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1124 if (!skb_partial_csum_set(skb, gso.csum_start,
1125 gso.csum_offset)) {
1126 tun->dev->stats.rx_frame_errors++;
1127 kfree_skb(skb);
1128 return -EINVAL;
1129 }
Michał Mirosław88255372011-04-19 06:13:10 +00001130 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 switch (tun->flags & TUN_TYPE_MASK) {
1133 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001134 if (tun->flags & TUN_NO_PI) {
1135 switch (skb->data[0] & 0xf0) {
1136 case 0x40:
1137 pi.proto = htons(ETH_P_IP);
1138 break;
1139 case 0x60:
1140 pi.proto = htons(ETH_P_IPV6);
1141 break;
1142 default:
1143 tun->dev->stats.rx_dropped++;
1144 kfree_skb(skb);
1145 return -EINVAL;
1146 }
1147 }
1148
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001149 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001151 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 case TUN_TAP_DEV:
1154 skb->protocol = eth_type_trans(skb, tun->dev);
1155 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Ben Hutchings5188cd42014-10-30 18:27:17 +00001158 skb_reset_network_header(skb);
1159
Rusty Russellf43798c2008-07-03 03:48:02 -07001160 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1161 pr_debug("GSO!\n");
1162 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1163 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001164 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001165 break;
1166 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001167 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001168 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001169 case VIRTIO_NET_HDR_GSO_UDP:
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001170 {
1171 static bool warned;
1172
1173 if (!warned) {
1174 warned = true;
1175 netdev_warn(tun->dev,
1176 "%s: using disabled UFO feature; please fix this program\n",
1177 current->comm);
1178 }
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001179 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Ben Hutchings5188cd42014-10-30 18:27:17 +00001180 if (skb->protocol == htons(ETH_P_IPV6))
1181 ipv6_proxy_select_ident(skb);
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001182 break;
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001183 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001184 default:
1185 tun->dev->stats.rx_frame_errors++;
1186 kfree_skb(skb);
1187 return -EINVAL;
1188 }
1189
1190 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001191 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001192
1193 skb_shinfo(skb)->gso_size = gso.gso_size;
1194 if (skb_shinfo(skb)->gso_size == 0) {
1195 tun->dev->stats.rx_frame_errors++;
1196 kfree_skb(skb);
1197 return -EINVAL;
1198 }
1199
1200 /* Header must be checked, and gso_segs computed. */
1201 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1202 skb_shinfo(skb)->gso_segs = 0;
1203 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001204
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001205 /* copy skb_ubuf_info for callback when skb has no error */
1206 if (zerocopy) {
1207 skb_shinfo(skb)->destructor_arg = msg_control;
1208 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001209 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001210 }
1211
Jason Wang40893fd2013-03-26 23:11:22 +00001212 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001213
Tom Herbert3958afa1b2013-12-15 22:12:06 -08001214 rxhash = skb_get_hash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001216
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001217 tun->dev->stats.rx_packets++;
1218 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jason Wang9e857222013-01-28 01:05:19 +00001220 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001221 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001224static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1225 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001227 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001228 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001229 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001230 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (!tun)
1233 return -EBADFD;
1234
Joe Perches6b8a66e2011-03-02 07:18:10 +00001235 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Jason Wang54f968d2012-10-31 19:45:57 +00001237 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1238 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001239
1240 tun_put(tun);
1241 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001245static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001246 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001247 struct sk_buff *skb,
1248 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct tun_pi pi = { 0, skb->protocol };
1251 ssize_t total = 0;
Jason Wange6fd07c2013-12-11 13:08:33 +08001252 int vlan_offset = 0, copied;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001253 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001254 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001255
1256 if (vlan_tx_tag_present(skb))
1257 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Herbert Xu2eb783c2014-11-03 04:30:14 +08001259 if (tun->flags & TUN_VNET_HDR)
1260 vnet_hdr_sz = tun->vnet_hdr_sz;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (!(tun->flags & TUN_NO_PI)) {
1263 if ((len -= sizeof(pi)) < 0)
1264 return -EINVAL;
1265
Herbert Xu2eb783c2014-11-03 04:30:14 +08001266 if (len < skb->len + vlan_hlen + vnet_hdr_sz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* Packet will be striped */
1268 pi.flags |= TUN_PKT_STRIP;
1269 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001270
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001271 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -EFAULT;
1273 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Herbert Xu2eb783c2014-11-03 04:30:14 +08001276 if (vnet_hdr_sz) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001277 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Herbert Xu2eb783c2014-11-03 04:30:14 +08001278 if ((len -= vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001279 return -EINVAL;
1280
1281 if (skb_is_gso(skb)) {
1282 struct skb_shared_info *sinfo = skb_shinfo(skb);
1283
1284 /* This is a hint as to how much should be linear. */
1285 gso.hdr_len = skb_headlen(skb);
1286 gso.gso_size = sinfo->gso_size;
1287 if (sinfo->gso_type & SKB_GSO_TCPV4)
1288 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1289 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1290 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001291 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001292 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001293 "0x%x, gso_size %d, hdr_len %d\n",
1294 sinfo->gso_type, gso.gso_size,
1295 gso.hdr_len);
1296 print_hex_dump(KERN_ERR, "tun: ",
1297 DUMP_PREFIX_NONE,
1298 16, 1, skb->head,
1299 min((int)gso.hdr_len, 64), true);
1300 WARN_ON_ONCE(1);
1301 return -EINVAL;
1302 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001303 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1304 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1305 } else
1306 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1307
1308 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1309 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001310 gso.csum_start = skb_checksum_start_offset(skb) +
1311 vlan_hlen;
Rusty Russellf43798c2008-07-03 03:48:02 -07001312 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001313 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1314 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001315 } /* else everything is zero */
1316
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001317 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1318 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001319 return -EFAULT;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001320 total += vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001321 }
1322
Jason Wange6fd07c2013-12-11 13:08:33 +08001323 copied = total;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001324 len = min_t(int, skb->len + vlan_hlen, len);
1325 total += skb->len + vlan_hlen;
1326 if (vlan_hlen) {
Jason Wang6680ec62013-07-25 13:00:33 +08001327 int copy, ret;
1328 struct {
1329 __be16 h_vlan_proto;
1330 __be16 h_vlan_TCI;
1331 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jason Wang6680ec62013-07-25 13:00:33 +08001333 veth.h_vlan_proto = skb->vlan_proto;
1334 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jason Wang6680ec62013-07-25 13:00:33 +08001336 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001337
1338 copy = min_t(int, vlan_offset, len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001339 ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001340 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001341 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001342 if (ret || !len)
1343 goto done;
1344
1345 copy = min_t(int, sizeof(veth), len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001346 ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001347 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001348 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001349 if (ret || !len)
1350 goto done;
1351 }
1352
Jason Wange6fd07c2013-12-11 13:08:33 +08001353 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
Jason Wang6680ec62013-07-25 13:00:33 +08001354
1355done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001356 tun->dev->stats.tx_packets++;
1357 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 return total;
1360}
1361
Jason Wang54f968d2012-10-31 19:45:57 +00001362static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001363 const struct iovec *iv, ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001366 ssize_t ret = 0;
Xi Wang9e641bd2014-05-16 15:11:48 -07001367 int peeked, err, off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Rami Rosen3872baf2012-11-25 22:07:41 +00001369 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Xi Wang9e641bd2014-05-16 15:11:48 -07001371 if (!len)
1372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Xi Wang9e641bd2014-05-16 15:11:48 -07001374 if (tun->dev->reg_state != NETREG_REGISTERED)
1375 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Xi Wang9e641bd2014-05-16 15:11:48 -07001377 /* Read frames from queue */
1378 skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
1379 &peeked, &off, &err);
1380 if (skb) {
Jason Wang54f968d2012-10-31 19:45:57 +00001381 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001382 kfree_skb(skb);
Xi Wang9e641bd2014-05-16 15:11:48 -07001383 } else
1384 ret = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001386 return ret;
1387}
1388
1389static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1390 unsigned long count, loff_t pos)
1391{
1392 struct file *file = iocb->ki_filp;
1393 struct tun_file *tfile = file->private_data;
1394 struct tun_struct *tun = __tun_get(tfile);
1395 ssize_t len, ret;
1396
1397 if (!tun)
1398 return -EBADFD;
1399 len = iov_length(iv, count);
1400 if (len < 0) {
1401 ret = -EINVAL;
1402 goto out;
1403 }
1404
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001405 ret = tun_do_read(tun, tfile, iv, len,
Jason Wang54f968d2012-10-31 19:45:57 +00001406 file->f_flags & O_NONBLOCK);
David S. Miller42404c02013-12-10 22:05:45 -05001407 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001408 if (ret > 0)
1409 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001410out:
1411 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return ret;
1413}
1414
Jason Wang96442e422012-10-31 19:46:02 +00001415static void tun_free_netdev(struct net_device *dev)
1416{
1417 struct tun_struct *tun = netdev_priv(dev);
1418
Jason Wang4008e972012-12-13 23:53:30 +00001419 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001420 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001421 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001422 free_netdev(dev);
1423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static void tun_setup(struct net_device *dev)
1426{
1427 struct tun_struct *tun = netdev_priv(dev);
1428
Eric W. Biederman0625c882012-02-07 16:48:55 -08001429 tun->owner = INVALID_UID;
1430 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001433 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001436/* Trivial set of netlink ops to allow deleting tun or tap
1437 * device with netlink.
1438 */
1439static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1440{
1441 return -EINVAL;
1442}
1443
1444static struct rtnl_link_ops tun_link_ops __read_mostly = {
1445 .kind = DRV_NAME,
1446 .priv_size = sizeof(struct tun_struct),
1447 .setup = tun_setup,
1448 .validate = tun_validate,
1449};
1450
Herbert Xu33dccbb2009-02-05 21:25:32 -08001451static void tun_sock_write_space(struct sock *sk)
1452{
Jason Wang54f968d2012-10-31 19:45:57 +00001453 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001454 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001455
1456 if (!sock_writeable(sk))
1457 return;
1458
Herbert Xu33dccbb2009-02-05 21:25:32 -08001459 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1460 return;
1461
Eric Dumazet43815482010-04-29 11:01:49 +00001462 wqueue = sk_sleep(sk);
1463 if (wqueue && waitqueue_active(wqueue))
1464 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001465 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001466
Jason Wang54f968d2012-10-31 19:45:57 +00001467 tfile = container_of(sk, struct tun_file, sk);
1468 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001469}
1470
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001471static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1472 struct msghdr *m, size_t total_len)
1473{
Jason Wang54f968d2012-10-31 19:45:57 +00001474 int ret;
1475 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1476 struct tun_struct *tun = __tun_get(tfile);
1477
1478 if (!tun)
1479 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001480 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1481 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1482 tun_put(tun);
1483 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001484}
1485
1486static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1487 struct msghdr *m, size_t total_len,
1488 int flags)
1489{
Jason Wang54f968d2012-10-31 19:45:57 +00001490 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1491 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001492 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001493
1494 if (!tun)
1495 return -EBADFD;
1496
Richard Cochraneda29772013-07-19 19:40:10 +02001497 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001498 ret = -EINVAL;
1499 goto out;
1500 }
Richard Cochraneda29772013-07-19 19:40:10 +02001501 if (flags & MSG_ERRQUEUE) {
1502 ret = sock_recv_errqueue(sock->sk, m, total_len,
1503 SOL_PACKET, TUN_TX_TIMESTAMP);
1504 goto out;
1505 }
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001506 ret = tun_do_read(tun, tfile, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001507 flags & MSG_DONTWAIT);
David S. Miller42404c02013-12-10 22:05:45 -05001508 if (ret > total_len) {
1509 m->msg_flags |= MSG_TRUNC;
1510 ret = flags & MSG_TRUNC ? ret : total_len;
1511 }
Gao feng3811ae72013-04-24 21:59:23 +00001512out:
Jason Wang54f968d2012-10-31 19:45:57 +00001513 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001514 return ret;
1515}
1516
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001517static int tun_release(struct socket *sock)
1518{
1519 if (sock->sk)
1520 sock_put(sock->sk);
1521 return 0;
1522}
1523
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001524/* Ops structure to mimic raw sockets with tun */
1525static const struct proto_ops tun_socket_ops = {
1526 .sendmsg = tun_sendmsg,
1527 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001528 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001529};
1530
Herbert Xu33dccbb2009-02-05 21:25:32 -08001531static struct proto tun_proto = {
1532 .name = "tun",
1533 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001534 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001535};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001536
David Woodhouse980c9e82009-05-09 22:54:21 -07001537static int tun_flags(struct tun_struct *tun)
1538{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001539 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07001540}
1541
1542static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1543 char *buf)
1544{
1545 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1546 return sprintf(buf, "0x%x\n", tun_flags(tun));
1547}
1548
1549static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1550 char *buf)
1551{
1552 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001553 return uid_valid(tun->owner)?
1554 sprintf(buf, "%u\n",
1555 from_kuid_munged(current_user_ns(), tun->owner)):
1556 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001557}
1558
1559static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1560 char *buf)
1561{
1562 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001563 return gid_valid(tun->group) ?
1564 sprintf(buf, "%u\n",
1565 from_kgid_munged(current_user_ns(), tun->group)):
1566 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001567}
1568
1569static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1570static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1571static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1572
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001573static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001576 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct net_device *dev;
1578 int err;
1579
Jason Wang7c0c3b12013-01-11 16:59:33 +00001580 if (tfile->detached)
1581 return -EINVAL;
1582
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001583 dev = __dev_get_by_name(net, ifr->ifr_name);
1584 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001585 if (ifr->ifr_flags & IFF_TUN_EXCL)
1586 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001587 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1588 tun = netdev_priv(dev);
1589 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1590 tun = netdev_priv(dev);
1591 else
1592 return -EINVAL;
1593
Jason Wang8e6d91a2013-05-28 18:32:11 +00001594 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
1595 !!(tun->flags & TUN_TAP_MQ))
1596 return -EINVAL;
1597
Jason Wangcde8b152012-10-31 19:46:01 +00001598 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001599 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001600 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001601 if (err < 0)
1602 return err;
1603
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001604 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001605 if (err < 0)
1606 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001607
1608 if (tun->flags & TUN_TAP_MQ &&
Jason Wange8dbad62013-04-22 20:40:39 +00001609 (tun->numqueues + tun->numdisabled > 1)) {
1610 /* One or more queue has already been attached, no need
1611 * to initialize the device again.
1612 */
1613 return 0;
1614 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 else {
1617 char *name;
1618 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001619 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1620 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001622 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001623 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001624 err = security_tun_dev_create();
1625 if (err < 0)
1626 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* Set dev type */
1629 if (ifr->ifr_flags & IFF_TUN) {
1630 /* TUN device */
1631 flags |= TUN_TUN_DEV;
1632 name = "tun%d";
1633 } else if (ifr->ifr_flags & IFF_TAP) {
1634 /* TAP device */
1635 flags |= TUN_TAP_DEV;
1636 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001637 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001638 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (*ifr->ifr_name)
1641 name = ifr->ifr_name;
1642
Jason Wangc8d68e62012-10-31 19:46:00 +00001643 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02001644 NET_NAME_UNKNOWN, tun_setup, queues,
1645 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!dev)
1648 return -ENOMEM;
1649
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001650 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001651 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001652 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 tun = netdev_priv(dev);
1655 tun->dev = dev;
1656 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001657 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001658 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Jason Wang54f968d2012-10-31 19:45:57 +00001660 tun->filter_attached = false;
1661 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001662
Jason Wang96442e422012-10-31 19:46:02 +00001663 spin_lock_init(&tun->lock);
1664
Paul Moore5dbbaf22013-01-14 07:12:19 +00001665 err = security_tun_dev_alloc_security(&tun->security);
1666 if (err < 0)
1667 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001670 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001671
Michał Mirosław88255372011-04-19 06:13:10 +00001672 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001673 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1674 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001675 dev->features = dev->hw_features;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09001676 dev->vlan_features = dev->features &
1677 ~(NETIF_F_HW_VLAN_CTAG_TX |
1678 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00001679
Jason Wang4008e972012-12-13 23:53:30 +00001680 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001681 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00001682 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001683 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 err = register_netdevice(tun->dev);
1686 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001687 goto err_detach;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001688
David Woodhouse980c9e82009-05-09 22:54:21 -07001689 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1690 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1691 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001692 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001695 netif_carrier_on(tun->dev);
1696
Joe Perches6b8a66e2011-03-02 07:18:10 +00001697 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001699 tun->flags = (tun->flags & ~TUN_FEATURES) |
1700 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00001701
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001702 /* Make sure persistent devices do not get stuck in
1703 * xoff state.
1704 */
1705 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001706 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 strcpy(ifr->ifr_name, tun->dev->name);
1709 return 0;
1710
Jason Wang662ca432013-09-11 18:09:48 +08001711err_detach:
1712 tun_detach_all(dev);
1713err_free_flow:
1714 tun_flow_uninit(tun);
1715 security_tun_dev_free_security(tun->security);
1716err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return err;
1719}
1720
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001721static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001722 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001723{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001724 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001725
1726 strcpy(ifr->ifr_name, tun->dev->name);
1727
David Woodhouse980c9e82009-05-09 22:54:21 -07001728 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001729
Mark McLoughline3b99552008-08-15 15:09:56 -07001730}
1731
Rusty Russell5228ddc2008-07-03 03:46:16 -07001732/* This is like a cut-down ethtool ops, except done via tun fd so no
1733 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001734static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001735{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001736 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001737
1738 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001739 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001740 arg &= ~TUN_F_CSUM;
1741
1742 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1743 if (arg & TUN_F_TSO_ECN) {
1744 features |= NETIF_F_TSO_ECN;
1745 arg &= ~TUN_F_TSO_ECN;
1746 }
1747 if (arg & TUN_F_TSO4)
1748 features |= NETIF_F_TSO;
1749 if (arg & TUN_F_TSO6)
1750 features |= NETIF_F_TSO6;
1751 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1752 }
1753 }
1754
1755 /* This gives the user a way to test for new features in future by
1756 * trying to set them. */
1757 if (arg)
1758 return -EINVAL;
1759
Michał Mirosław88255372011-04-19 06:13:10 +00001760 tun->set_features = features;
1761 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001762
1763 return 0;
1764}
1765
Jason Wangc8d68e62012-10-31 19:46:00 +00001766static void tun_detach_filter(struct tun_struct *tun, int n)
1767{
1768 int i;
1769 struct tun_file *tfile;
1770
1771 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001772 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001773 sk_detach_filter(tfile->socket.sk);
1774 }
1775
1776 tun->filter_attached = false;
1777}
1778
1779static int tun_attach_filter(struct tun_struct *tun)
1780{
1781 int i, ret = 0;
1782 struct tun_file *tfile;
1783
1784 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001785 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001786 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1787 if (ret) {
1788 tun_detach_filter(tun, i);
1789 return ret;
1790 }
1791 }
1792
1793 tun->filter_attached = true;
1794 return ret;
1795}
1796
1797static void tun_set_sndbuf(struct tun_struct *tun)
1798{
1799 struct tun_file *tfile;
1800 int i;
1801
1802 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001803 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001804 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1805 }
1806}
1807
Jason Wangcde8b152012-10-31 19:46:01 +00001808static int tun_set_queue(struct file *file, struct ifreq *ifr)
1809{
1810 struct tun_file *tfile = file->private_data;
1811 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001812 int ret = 0;
1813
1814 rtnl_lock();
1815
1816 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001817 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001818 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001819 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001820 goto unlock;
1821 }
1822 ret = security_tun_dev_attach_queue(tun->security);
1823 if (ret < 0)
1824 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001825 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00001826 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001827 tun = rtnl_dereference(tfile->tun);
Jason Wang9e857222013-01-28 01:05:19 +00001828 if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001829 ret = -EINVAL;
1830 else
1831 __tun_detach(tfile, false);
1832 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001833 ret = -EINVAL;
1834
Paul Moore5dbbaf22013-01-14 07:12:19 +00001835unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001836 rtnl_unlock();
1837 return ret;
1838}
1839
Arnd Bergmann50857e22009-11-06 22:52:32 -08001840static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1841 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001843 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001844 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 void __user* argp = (void __user*)arg;
1846 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001847 kuid_t owner;
1848 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001849 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001850 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001851 unsigned int ifindex;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001852 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Jason Wangcde8b152012-10-31 19:46:01 +00001854 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001855 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001857 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001858 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001859 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001860 if (cmd == TUNGETFEATURES) {
1861 /* Currently this just means: "what IFF flags are valid?".
1862 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001863 * TUNSETIFF.
1864 */
1865 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001866 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001867 } else if (cmd == TUNSETQUEUE)
1868 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001869
Jason Wangc8d68e62012-10-31 19:46:00 +00001870 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001871 rtnl_lock();
1872
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001873 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1876
Herbert Xu876bfd42009-08-06 14:22:44 +00001877 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Herbert Xu876bfd42009-08-06 14:22:44 +00001879 if (ret)
1880 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Arnd Bergmann50857e22009-11-06 22:52:32 -08001882 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001883 ret = -EFAULT;
1884 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001886 if (cmd == TUNSETIFINDEX) {
1887 ret = -EPERM;
1888 if (tun)
1889 goto unlock;
1890
1891 ret = -EFAULT;
1892 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1893 goto unlock;
1894
1895 ret = 0;
1896 tfile->ifindex = ifindex;
1897 goto unlock;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Herbert Xu876bfd42009-08-06 14:22:44 +00001900 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001902 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Jason Wang1e588332012-10-31 19:45:56 +00001904 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Eric W. Biederman631ab462009-01-20 11:00:40 +00001906 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001908 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001909 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001910
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001911 if (tfile->detached)
1912 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001913 if (!tfile->socket.sk->sk_filter)
1914 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001915
Arnd Bergmann50857e22009-11-06 22:52:32 -08001916 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001917 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001918 break;
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 case TUNSETNOCSUM:
1921 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Michał Mirosław88255372011-04-19 06:13:10 +00001923 /* [unimplemented] */
1924 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001925 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927
1928 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001929 /* Disable/Enable persist mode. Keep an extra reference to the
1930 * module to prevent the module being unprobed.
1931 */
Jason Wangdd38bd82013-01-11 16:59:34 +00001932 if (arg && !(tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001934 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001935 }
1936 if (!arg && (tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001938 module_put(THIS_MODULE);
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Joe Perches6b8a66e2011-03-02 07:18:10 +00001941 tun_debug(KERN_INFO, tun, "persist %s\n",
1942 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 break;
1944
1945 case TUNSETOWNER:
1946 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001947 owner = make_kuid(current_user_ns(), arg);
1948 if (!uid_valid(owner)) {
1949 ret = -EINVAL;
1950 break;
1951 }
1952 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001953 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001954 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956
Guido Guenther8c644622007-07-02 22:50:25 -07001957 case TUNSETGROUP:
1958 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001959 group = make_kgid(current_user_ns(), arg);
1960 if (!gid_valid(group)) {
1961 ret = -EINVAL;
1962 break;
1963 }
1964 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001965 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001966 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001967 break;
1968
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001969 case TUNSETLINK:
1970 /* Only allow setting the type when the interface is down */
1971 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001972 tun_debug(KERN_INFO, tun,
1973 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001974 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001975 } else {
1976 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001977 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1978 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001979 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001980 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001981 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#ifdef TUN_DEBUG
1984 case TUNSETDEBUG:
1985 tun->debug = arg;
1986 break;
1987#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001988 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001989 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001990 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001991
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001992 case TUNSETTXFILTER:
1993 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001994 ret = -EINVAL;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001995 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001996 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001997 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002001 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002002 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2003 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002004 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002005 ret = -EFAULT;
2006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002009 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002010 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2011 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002012
Kim B. Heino40102372008-02-29 12:26:21 -08002013 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002014 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002015
2016 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002017 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002018 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2019 ret = -EFAULT;
2020 break;
2021
2022 case TUNSETSNDBUF:
2023 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2024 ret = -EFAULT;
2025 break;
2026 }
2027
Jason Wangc8d68e62012-10-31 19:46:00 +00002028 tun->sndbuf = sndbuf;
2029 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002030 break;
2031
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002032 case TUNGETVNETHDRSZ:
2033 vnet_hdr_sz = tun->vnet_hdr_sz;
2034 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2035 ret = -EFAULT;
2036 break;
2037
2038 case TUNSETVNETHDRSZ:
2039 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2040 ret = -EFAULT;
2041 break;
2042 }
2043 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2044 ret = -EINVAL;
2045 break;
2046 }
2047
2048 tun->vnet_hdr_sz = vnet_hdr_sz;
2049 break;
2050
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002051 case TUNATTACHFILTER:
2052 /* Can be set only for TAPs */
2053 ret = -EINVAL;
2054 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2055 break;
2056 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002057 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002058 break;
2059
Jason Wangc8d68e62012-10-31 19:46:00 +00002060 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002061 break;
2062
2063 case TUNDETACHFILTER:
2064 /* Can be set only for TAPs */
2065 ret = -EINVAL;
2066 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2067 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002068 ret = 0;
2069 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002070 break;
2071
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002072 case TUNGETFILTER:
2073 ret = -EINVAL;
2074 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2075 break;
2076 ret = -EFAULT;
2077 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2078 break;
2079 ret = 0;
2080 break;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002083 ret = -EINVAL;
2084 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Herbert Xu876bfd42009-08-06 14:22:44 +00002087unlock:
2088 rtnl_unlock();
2089 if (tun)
2090 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Arnd Bergmann50857e22009-11-06 22:52:32 -08002094static long tun_chr_ioctl(struct file *file,
2095 unsigned int cmd, unsigned long arg)
2096{
2097 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2098}
2099
2100#ifdef CONFIG_COMPAT
2101static long tun_chr_compat_ioctl(struct file *file,
2102 unsigned int cmd, unsigned long arg)
2103{
2104 switch (cmd) {
2105 case TUNSETIFF:
2106 case TUNGETIFF:
2107 case TUNSETTXFILTER:
2108 case TUNGETSNDBUF:
2109 case TUNSETSNDBUF:
2110 case SIOCGIFHWADDR:
2111 case SIOCSIFHWADDR:
2112 arg = (unsigned long)compat_ptr(arg);
2113 break;
2114 default:
2115 arg = (compat_ulong_t)arg;
2116 break;
2117 }
2118
2119 /*
2120 * compat_ifreq is shorter than ifreq, so we must not access beyond
2121 * the end of that structure. All fields that are used in this
2122 * driver are compatible though, we don't need to convert the
2123 * contents.
2124 */
2125 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2126}
2127#endif /* CONFIG_COMPAT */
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129static int tun_chr_fasync(int fd, struct file *file, int on)
2130{
Jason Wang54f968d2012-10-31 19:45:57 +00002131 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int ret;
2133
Jason Wang54f968d2012-10-31 19:45:57 +00002134 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002135 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002138 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002139 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002140 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002141 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002142 ret = 0;
2143out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147static int tun_chr_open(struct inode *inode, struct file * file)
2148{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002149 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002150
Joe Perches6b8a66e2011-03-02 07:18:10 +00002151 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002152
Jason Wang54f968d2012-10-31 19:45:57 +00002153 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2154 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002155 if (!tfile)
2156 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302157 RCU_INIT_POINTER(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002158 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002159 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002160 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002161
Jason Wang54f968d2012-10-31 19:45:57 +00002162 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002163 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002164
2165 tfile->socket.file = file;
2166 tfile->socket.ops = &tun_socket_ops;
2167
2168 sock_init_data(&tfile->socket, &tfile->sk);
2169 sk_change_net(&tfile->sk, tfile->net);
2170
2171 tfile->sk.sk_write_space = tun_sock_write_space;
2172 tfile->sk.sk_sndbuf = INT_MAX;
2173
Eric W. Biederman631ab462009-01-20 11:00:40 +00002174 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002175 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002176 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002177
Jason Wang19a6afb2013-06-08 14:17:41 +08002178 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181}
2182
2183static int tun_chr_close(struct inode *inode, struct file *file)
2184{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002185 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002186 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jason Wangc8d68e62012-10-31 19:46:00 +00002188 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002189 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 return 0;
2192}
2193
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002194#ifdef CONFIG_PROC_FS
2195static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
2196{
2197 struct tun_struct *tun;
2198 struct ifreq ifr;
2199
2200 memset(&ifr, 0, sizeof(ifr));
2201
2202 rtnl_lock();
2203 tun = tun_get(f);
2204 if (tun)
2205 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2206 rtnl_unlock();
2207
2208 if (tun)
2209 tun_put(tun);
2210
2211 return seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
2212}
2213#endif
2214
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002215static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002216 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002218 .read = do_sync_read,
2219 .aio_read = tun_chr_aio_read,
2220 .write = do_sync_write,
2221 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002223 .unlocked_ioctl = tun_chr_ioctl,
2224#ifdef CONFIG_COMPAT
2225 .compat_ioctl = tun_chr_compat_ioctl,
2226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 .open = tun_chr_open,
2228 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002229 .fasync = tun_chr_fasync,
2230#ifdef CONFIG_PROC_FS
2231 .show_fdinfo = tun_chr_show_fdinfo,
2232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233};
2234
2235static struct miscdevice tun_miscdev = {
2236 .minor = TUN_MINOR,
2237 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002238 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240};
2241
2242/* ethtool interface */
2243
2244static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2245{
2246 cmd->supported = 0;
2247 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002248 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 cmd->duplex = DUPLEX_FULL;
2250 cmd->port = PORT_TP;
2251 cmd->phy_address = 0;
2252 cmd->transceiver = XCVR_INTERNAL;
2253 cmd->autoneg = AUTONEG_DISABLE;
2254 cmd->maxtxpkt = 0;
2255 cmd->maxrxpkt = 0;
2256 return 0;
2257}
2258
2259static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2260{
2261 struct tun_struct *tun = netdev_priv(dev);
2262
Rick Jones33a5ba12011-11-15 14:59:53 +00002263 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2264 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 switch (tun->flags & TUN_TYPE_MASK) {
2267 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002268 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 break;
2270 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002271 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
2273 }
2274}
2275
2276static u32 tun_get_msglevel(struct net_device *dev)
2277{
2278#ifdef TUN_DEBUG
2279 struct tun_struct *tun = netdev_priv(dev);
2280 return tun->debug;
2281#else
2282 return -EOPNOTSUPP;
2283#endif
2284}
2285
2286static void tun_set_msglevel(struct net_device *dev, u32 value)
2287{
2288#ifdef TUN_DEBUG
2289 struct tun_struct *tun = netdev_priv(dev);
2290 tun->debug = value;
2291#endif
2292}
2293
Jeff Garzik7282d492006-09-13 14:30:00 -04002294static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 .get_settings = tun_get_settings,
2296 .get_drvinfo = tun_get_drvinfo,
2297 .get_msglevel = tun_get_msglevel,
2298 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002299 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002300 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301};
2302
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static int __init tun_init(void)
2305{
2306 int ret = 0;
2307
Joe Perches6b8a66e2011-03-02 07:18:10 +00002308 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2309 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002311 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002312 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002313 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002314 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002315 }
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002318 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002319 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002320 goto err_misc;
2321 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002322 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002323err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002324 rtnl_link_unregister(&tun_link_ops);
2325err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return ret;
2327}
2328
2329static void tun_cleanup(void)
2330{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002331 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002332 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002335/* Get an underlying socket object from tun file. Returns error unless file is
2336 * attached to a device. The returned object works like a packet socket, it
2337 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2338 * holding a reference to the file for as long as the socket is in use. */
2339struct socket *tun_get_socket(struct file *file)
2340{
Jason Wang6e914fc2012-10-31 19:45:58 +00002341 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002342 if (file->f_op != &tun_fops)
2343 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002344 tfile = file->private_data;
2345 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002346 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002347 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002348}
2349EXPORT_SYMBOL_GPL(tun_get_socket);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351module_init(tun_init);
2352module_exit(tun_cleanup);
2353MODULE_DESCRIPTION(DRV_DESCRIPTION);
2354MODULE_AUTHOR(DRV_COPYRIGHT);
2355MODULE_LICENSE("GPL");
2356MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002357MODULE_ALIAS("devname:net/tun");