blob: 79b6f9ecc12cc5a07341591b307e41a7155afefc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18/*
19 * Changes:
20 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070021 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000025 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
Joe Perches6b8a66e2011-03-02 07:18:10 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRV_NAME "tun"
40#define DRV_VERSION "1.6"
41#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/errno.h>
46#include <linux/kernel.h>
47#include <linux/major.h>
48#include <linux/slab.h>
49#include <linux/poll.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/skbuff.h>
53#include <linux/netdevice.h>
54#include <linux/etherdevice.h>
55#include <linux/miscdevice.h>
56#include <linux/ethtool.h>
57#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080058#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/if.h>
60#include <linux/if_arp.h>
61#include <linux/if_ether.h>
62#include <linux/if_tun.h>
63#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070064#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070065#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000066#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070067#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070068#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080069#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080070#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/uaccess.h>
73
Rusty Russell14daa022008-04-12 18:48:58 -070074/* Uncomment to enable debugging */
75/* #define TUN_DEBUG 1 */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef TUN_DEBUG
78static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070079
Joe Perches6b8a66e2011-03-02 07:18:10 +000080#define tun_debug(level, tun, fmt, args...) \
81do { \
82 if (tun->debug) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
84} while (0)
85#define DBG1(level, fmt, args...) \
86do { \
87 if (debug == 2) \
88 printk(level fmt, ##args); \
89} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070090#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000091#define tun_debug(level, tun, fmt, args...) \
92do { \
93 if (0) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
95} while (0)
96#define DBG1(level, fmt, args...) \
97do { \
98 if (0) \
99 printk(level fmt, ##args); \
100} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000103#define GOODCOPY_LEN 128
104
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700105#define FLT_EXACT_COUNT 8
106struct tap_filter {
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
110};
111
Jason Wangc8d68e62012-10-31 19:46:00 +0000112/* 1024 is probably a high enough limit: modern hypervisors seem to support on
113 * the order of 100-200 CPUs so this leaves us some breathing space if we want
114 * to match a queue per guest CPU.
115 */
116#define MAX_TAP_QUEUES 1024
117
Jason Wang54f968d2012-10-31 19:45:57 +0000118/* A tun_file connects an open character device to a tuntap netdevice. It
119 * also contains all socket related strctures (except sock_fprog and tap_filter)
120 * to serve as one transmit queue for tuntap device. The sock_fprog and
121 * tap_filter were kept in tun_struct since they were used for filtering for the
122 * netdevice not for a specific queue (at least I didn't see the reqirement for
123 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000124 *
125 * RCU usage:
126 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
127 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000128 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000129struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000130 struct sock sk;
131 struct socket socket;
132 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000133 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000134 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000135 struct fasync_struct *fasync;
136 /* only used for fasnyc */
137 unsigned int flags;
Jason Wangc8d68e62012-10-31 19:46:00 +0000138 u16 queue_index;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000139};
140
Jason Wang54f968d2012-10-31 19:45:57 +0000141/* Since the socket were moved to tun_file, to preserve the behavior of persist
142 * device, socket fileter, sndbuf and vnet header size were restore when the
143 * file were attached to a persist device.
144 */
Rusty Russell14daa022008-04-12 18:48:58 -0700145struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000146 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
147 unsigned int numqueues;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700148 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800149 kuid_t owner;
150 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700151
Rusty Russell14daa022008-04-12 18:48:58 -0700152 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000153 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000154#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
155 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200156
157 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000158 int sndbuf;
159 struct tap_filter txflt;
160 struct sock_fprog fprog;
161 /* protected by rtnl lock */
162 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700163#ifdef TUN_DEBUG
164 int debug;
165#endif
166};
167
Jason Wangc8d68e62012-10-31 19:46:00 +0000168/* We try to identify a flow through its rxhash first. The reason that
169 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
170 * the rxq based on the txq where the last packet of the flow comes. As
171 * the userspace application move between processors, we may get a
172 * different rxq no. here. If we could not get rxhash, then we would
173 * hope the rxq no. may help here.
174 */
175static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
176{
177 struct tun_struct *tun = netdev_priv(dev);
178 u32 txq = 0;
179 u32 numqueues = 0;
180
181 rcu_read_lock();
182 numqueues = tun->numqueues;
183
184 txq = skb_get_rxhash(skb);
185 if (txq) {
186 /* use multiply and shift instead of expensive divide */
187 txq = ((u64)txq * numqueues) >> 32;
188 } else if (likely(skb_rx_queue_recorded(skb))) {
189 txq = skb_get_rx_queue(skb);
190 while (unlikely(txq >= numqueues))
191 txq -= numqueues;
192 }
193
194 rcu_read_unlock();
195 return txq;
196}
197
Jason Wangcde8b152012-10-31 19:46:01 +0000198static inline bool tun_not_capable(struct tun_struct *tun)
199{
200 const struct cred *cred = current_cred();
201
202 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
203 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
204 !capable(CAP_NET_ADMIN);
205}
206
Jason Wangc8d68e62012-10-31 19:46:00 +0000207static void tun_set_real_num_queues(struct tun_struct *tun)
208{
209 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
210 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
211}
212
213static void __tun_detach(struct tun_file *tfile, bool clean)
214{
215 struct tun_file *ntfile;
216 struct tun_struct *tun;
217 struct net_device *dev;
218
219 tun = rcu_dereference_protected(tfile->tun,
220 lockdep_rtnl_is_held());
221 if (tun) {
222 u16 index = tfile->queue_index;
223 BUG_ON(index >= tun->numqueues);
224 dev = tun->dev;
225
226 rcu_assign_pointer(tun->tfiles[index],
227 tun->tfiles[tun->numqueues - 1]);
228 rcu_assign_pointer(tfile->tun, NULL);
229 ntfile = rcu_dereference_protected(tun->tfiles[index],
230 lockdep_rtnl_is_held());
231 ntfile->queue_index = index;
232
233 --tun->numqueues;
234 sock_put(&tfile->sk);
235
236 synchronize_net();
237 /* Drop read queue */
238 skb_queue_purge(&tfile->sk.sk_receive_queue);
239 tun_set_real_num_queues(tun);
240
241 if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
242 if (dev->reg_state == NETREG_REGISTERED)
243 unregister_netdevice(dev);
244 }
245
246 if (clean) {
247 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
248 &tfile->socket.flags));
249 sk_release_kernel(&tfile->sk);
250 }
251}
252
253static void tun_detach(struct tun_file *tfile, bool clean)
254{
255 rtnl_lock();
256 __tun_detach(tfile, clean);
257 rtnl_unlock();
258}
259
260static void tun_detach_all(struct net_device *dev)
261{
262 struct tun_struct *tun = netdev_priv(dev);
263 struct tun_file *tfile;
264 int i, n = tun->numqueues;
265
266 for (i = 0; i < n; i++) {
267 tfile = rcu_dereference_protected(tun->tfiles[i],
268 lockdep_rtnl_is_held());
269 BUG_ON(!tfile);
270 wake_up_all(&tfile->wq.wait);
271 rcu_assign_pointer(tfile->tun, NULL);
272 --tun->numqueues;
273 }
274 BUG_ON(tun->numqueues != 0);
275
276 synchronize_net();
277 for (i = 0; i < n; i++) {
278 tfile = rcu_dereference_protected(tun->tfiles[i],
279 lockdep_rtnl_is_held());
280 /* Drop read queue */
281 skb_queue_purge(&tfile->sk.sk_receive_queue);
282 sock_put(&tfile->sk);
283 }
284}
285
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000286static int tun_attach(struct tun_struct *tun, struct file *file)
287{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000288 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000289 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000290
Eric W. Biederman38231b72009-01-20 11:02:28 +0000291 err = -EINVAL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000292 if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
Eric W. Biederman38231b72009-01-20 11:02:28 +0000293 goto out;
294
295 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000296 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
297 goto out;
298
299 err = -E2BIG;
300 if (tun->numqueues == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000301 goto out;
302
303 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000304
Jason Wangc8d68e62012-10-31 19:46:00 +0000305 /* Re-attach the filter to presist device */
Jason Wang54f968d2012-10-31 19:45:57 +0000306 if (tun->filter_attached == true) {
307 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
308 if (!err)
309 goto out;
310 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000311 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000312 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000313 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wang54f968d2012-10-31 19:45:57 +0000314 sock_hold(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000315 tun->numqueues++;
316
317 tun_set_real_num_queues(tun);
318
319 if (tun->numqueues == 1)
320 netif_carrier_on(tun->dev);
321
322 /* device is allowed to go away first, so no need to hold extra
323 * refcnt.
324 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000325
Eric W. Biederman38231b72009-01-20 11:02:28 +0000326out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000327 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000328}
329
Eric W. Biederman631ab462009-01-20 11:00:40 +0000330static struct tun_struct *__tun_get(struct tun_file *tfile)
331{
Jason Wang6e914fc2012-10-31 19:45:58 +0000332 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000333
Jason Wang6e914fc2012-10-31 19:45:58 +0000334 rcu_read_lock();
335 tun = rcu_dereference(tfile->tun);
336 if (tun)
337 dev_hold(tun->dev);
338 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000339
340 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000341}
342
343static struct tun_struct *tun_get(struct file *file)
344{
345 return __tun_get(file->private_data);
346}
347
348static void tun_put(struct tun_struct *tun)
349{
Jason Wang6e914fc2012-10-31 19:45:58 +0000350 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000351}
352
Joe Perches6b8a66e2011-03-02 07:18:10 +0000353/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700354static void addr_hash_set(u32 *mask, const u8 *addr)
355{
356 int n = ether_crc(ETH_ALEN, addr) >> 26;
357 mask[n >> 5] |= (1 << (n & 31));
358}
359
360static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
361{
362 int n = ether_crc(ETH_ALEN, addr) >> 26;
363 return mask[n >> 5] & (1 << (n & 31));
364}
365
366static int update_filter(struct tap_filter *filter, void __user *arg)
367{
368 struct { u8 u[ETH_ALEN]; } *addr;
369 struct tun_filter uf;
370 int err, alen, n, nexact;
371
372 if (copy_from_user(&uf, arg, sizeof(uf)))
373 return -EFAULT;
374
375 if (!uf.count) {
376 /* Disabled */
377 filter->count = 0;
378 return 0;
379 }
380
381 alen = ETH_ALEN * uf.count;
382 addr = kmalloc(alen, GFP_KERNEL);
383 if (!addr)
384 return -ENOMEM;
385
386 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
387 err = -EFAULT;
388 goto done;
389 }
390
391 /* The filter is updated without holding any locks. Which is
392 * perfectly safe. We disable it first and in the worst
393 * case we'll accept a few undesired packets. */
394 filter->count = 0;
395 wmb();
396
397 /* Use first set of addresses as an exact filter */
398 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
399 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
400
401 nexact = n;
402
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800403 /* Remaining multicast addresses are hashed,
404 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700405 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800406 for (; n < uf.count; n++) {
407 if (!is_multicast_ether_addr(addr[n].u)) {
408 err = 0; /* no filter */
409 goto done;
410 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700411 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800412 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700413
414 /* For ALLMULTI just set the mask to all ones.
415 * This overrides the mask populated above. */
416 if ((uf.flags & TUN_FLT_ALLMULTI))
417 memset(filter->mask, ~0, sizeof(filter->mask));
418
419 /* Now enable the filter */
420 wmb();
421 filter->count = nexact;
422
423 /* Return the number of exact filters */
424 err = nexact;
425
426done:
427 kfree(addr);
428 return err;
429}
430
431/* Returns: 0 - drop, !=0 - accept */
432static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
433{
434 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
435 * at this point. */
436 struct ethhdr *eh = (struct ethhdr *) skb->data;
437 int i;
438
439 /* Exact match */
440 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000441 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700442 return 1;
443
444 /* Inexact match (multicast only) */
445 if (is_multicast_ether_addr(eh->h_dest))
446 return addr_hash_test(filter->mask, eh->h_dest);
447
448 return 0;
449}
450
451/*
452 * Checks whether the packet is accepted or not.
453 * Returns: 0 - drop, !=0 - accept
454 */
455static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
456{
457 if (!filter->count)
458 return 1;
459
460 return run_filter(filter, skb);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/* Network device part of the driver */
464
Jeff Garzik7282d492006-09-13 14:30:00 -0400465static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000467/* Net device detach from fd. */
468static void tun_net_uninit(struct net_device *dev)
469{
Jason Wangc8d68e62012-10-31 19:46:00 +0000470 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/* Net device open. */
474static int tun_net_open(struct net_device *dev)
475{
Jason Wangc8d68e62012-10-31 19:46:00 +0000476 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 0;
478}
479
480/* Net device close. */
481static int tun_net_close(struct net_device *dev)
482{
Jason Wangc8d68e62012-10-31 19:46:00 +0000483 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
487/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000488static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000491 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000492 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jason Wang6e914fc2012-10-31 19:45:58 +0000494 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000495 tfile = rcu_dereference(tun->tfiles[txq]);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000498 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto drop;
500
Jason Wang6e914fc2012-10-31 19:45:58 +0000501 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
502
Jason Wangc8d68e62012-10-31 19:46:00 +0000503 BUG_ON(!tfile);
504
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700505 /* Drop if the filter does not like it.
506 * This is a noop if the filter is disabled.
507 * Filter can be enabled only for the TAP devices. */
508 if (!check_filter(&tun->txflt, skb))
509 goto drop;
510
Jason Wang54f968d2012-10-31 19:45:57 +0000511 if (tfile->socket.sk->sk_filter &&
512 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000513 goto drop;
514
Jason Wangc8d68e62012-10-31 19:46:00 +0000515 /* Limit the number of packets queued by divining txq length with the
516 * number of queues.
517 */
Jason Wang54f968d2012-10-31 19:45:57 +0000518 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Jason Wangc8d68e62012-10-31 19:46:00 +0000519 >= dev->tx_queue_len / tun->numqueues){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!(tun->flags & TUN_ONE_QUEUE)) {
521 /* Normal queueing mode. */
522 /* Packet scheduler handles dropping of further packets. */
Jason Wangc8d68e62012-10-31 19:46:00 +0000523 netif_stop_subqueue(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* We won't see all dropped packets individually, so overrun
526 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700527 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
529 /* Single queue mode.
530 * Driver handles dropping of all packets itself. */
531 goto drop;
532 }
533 }
534
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000535 /* Orphan the skb - required as we might hang on to it
536 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000537 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
538 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000539 skb_orphan(skb);
540
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700541 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000542 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000545 if (tfile->flags & TUN_FASYNC)
546 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
547 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000548 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000549
550 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000551 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700554 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000556 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000557 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700560static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700562 /*
563 * This callback is supposed to deal with mc filter in
564 * _rx_ path and has nothing to do with the _tx_ path.
565 * In rx path we always accept everything userspace gives us.
566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Ed Swierk4885a502007-09-16 12:21:38 -0700569#define MIN_MTU 68
570#define MAX_MTU 65535
571
572static int
573tun_net_change_mtu(struct net_device *dev, int new_mtu)
574{
575 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
576 return -EINVAL;
577 dev->mtu = new_mtu;
578 return 0;
579}
580
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000581static netdev_features_t tun_net_fix_features(struct net_device *dev,
582 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000583{
584 struct tun_struct *tun = netdev_priv(dev);
585
586 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
587}
Neil Hormanbebd0972011-06-15 05:25:01 +0000588#ifdef CONFIG_NET_POLL_CONTROLLER
589static void tun_poll_controller(struct net_device *dev)
590{
591 /*
592 * Tun only receives frames when:
593 * 1) the char device endpoint gets data from user space
594 * 2) the tun socket gets a sendmsg call from user space
595 * Since both of those are syncronous operations, we are guaranteed
596 * never to have pending data when we poll for it
597 * so theres nothing to do here but return.
598 * We need this though so netpoll recognizes us as an interface that
599 * supports polling, which enables bridge devices in virt setups to
600 * still use netconsole
601 */
602 return;
603}
604#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800605static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000606 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800607 .ndo_open = tun_net_open,
608 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800609 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800610 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000611 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000612 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000613#ifdef CONFIG_NET_POLL_CONTROLLER
614 .ndo_poll_controller = tun_poll_controller,
615#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800616};
617
618static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000619 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800620 .ndo_open = tun_net_open,
621 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800622 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800623 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000624 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000625 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800626 .ndo_set_mac_address = eth_mac_addr,
627 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000628 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000629#ifdef CONFIG_NET_POLL_CONTROLLER
630 .ndo_poll_controller = tun_poll_controller,
631#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800632};
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/* Initialize net device. */
635static void tun_net_init(struct net_device *dev)
636{
637 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 switch (tun->flags & TUN_TYPE_MASK) {
640 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800641 dev->netdev_ops = &tun_netdev_ops;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* Point-to-Point TUN Device */
644 dev->hard_header_len = 0;
645 dev->addr_len = 0;
646 dev->mtu = 1500;
647
648 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400649 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
651 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
652 break;
653
654 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800655 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700657 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000658 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000660 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
663 break;
664 }
665}
666
667/* Character device part */
668
669/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000670static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400671{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000672 struct tun_file *tfile = file->private_data;
673 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000674 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800675 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000678 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jason Wang54f968d2012-10-31 19:45:57 +0000680 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000681
Joe Perches6b8a66e2011-03-02 07:18:10 +0000682 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jason Wang54f968d2012-10-31 19:45:57 +0000684 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400685
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000686 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mask |= POLLIN | POLLRDNORM;
688
Herbert Xu33dccbb2009-02-05 21:25:32 -0800689 if (sock_writeable(sk) ||
690 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
691 sock_writeable(sk)))
692 mask |= POLLOUT | POLLWRNORM;
693
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000694 if (tun->dev->reg_state != NETREG_REGISTERED)
695 mask = POLLERR;
696
Eric W. Biederman631ab462009-01-20 11:00:40 +0000697 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return mask;
699}
700
Rusty Russellf42157c2008-08-15 15:15:10 -0700701/* prepad is the amount to reserve at front. len is length after that.
702 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000703static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000704 size_t prepad, size_t len,
705 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700706{
Jason Wang54f968d2012-10-31 19:45:57 +0000707 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700708 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800709 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700710
711 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700712 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800713 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700714
Herbert Xu33dccbb2009-02-05 21:25:32 -0800715 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
716 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700717 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800718 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700719
720 skb_reserve(skb, prepad);
721 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800722 skb->data_len = len - linear;
723 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700724
725 return skb;
726}
727
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000728/* set skb frags from iovec, this can move to core network code for reuse */
729static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
730 int offset, size_t count)
731{
732 int len = iov_length(from, count) - offset;
733 int copy = skb_headlen(skb);
734 int size, offset1 = 0;
735 int i = 0;
736
737 /* Skip over from offset */
738 while (count && (offset >= from->iov_len)) {
739 offset -= from->iov_len;
740 ++from;
741 --count;
742 }
743
744 /* copy up to skb headlen */
745 while (count && (copy > 0)) {
746 size = min_t(unsigned int, copy, from->iov_len - offset);
747 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
748 size))
749 return -EFAULT;
750 if (copy > size) {
751 ++from;
752 --count;
753 offset = 0;
754 } else
755 offset += size;
756 copy -= size;
757 offset1 += size;
758 }
759
760 if (len == offset1)
761 return 0;
762
763 while (count--) {
764 struct page *page[MAX_SKB_FRAGS];
765 int num_pages;
766 unsigned long base;
767 unsigned long truesize;
768
769 len = from->iov_len - offset;
770 if (!len) {
771 offset = 0;
772 ++from;
773 continue;
774 }
775 base = (unsigned long)from->iov_base + offset;
776 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
777 if (i + size > MAX_SKB_FRAGS)
778 return -EMSGSIZE;
779 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
780 if (num_pages != size) {
781 for (i = 0; i < num_pages; i++)
782 put_page(page[i]);
783 return -EFAULT;
784 }
785 truesize = size * PAGE_SIZE;
786 skb->data_len += len;
787 skb->len += len;
788 skb->truesize += truesize;
789 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
790 while (len) {
791 int off = base & ~PAGE_MASK;
792 int size = min_t(int, len, PAGE_SIZE - off);
793 __skb_fill_page_desc(skb, i, page[i], off, size);
794 skb_shinfo(skb)->nr_frags++;
795 /* increase sk_wmem_alloc */
796 base += size;
797 len -= size;
798 i++;
799 }
800 offset = 0;
801 ++from;
802 }
803 return 0;
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000807static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
808 void *msg_control, const struct iovec *iv,
809 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Harvey Harrison09640e62009-02-01 00:45:17 -0800811 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000813 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -0700814 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000815 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000816 int copylen;
817 bool zerocopy = false;
818 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000821 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -EINVAL;
823
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000824 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000826 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Rusty Russellf43798c2008-07-03 03:48:02 -0700829 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000830 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -0700831 return -EINVAL;
832
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000833 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700834 return -EFAULT;
835
Herbert Xu49091222009-06-08 00:20:01 -0700836 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
837 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
838 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
839
Rusty Russellf43798c2008-07-03 03:48:02 -0700840 if (gso.hdr_len > len)
841 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200842 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700843 }
844
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700845 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +0000846 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -0700847 if (unlikely(len < ETH_HLEN ||
848 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700849 return -EINVAL;
850 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400851
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000852 if (msg_control)
853 zerocopy = true;
854
855 if (zerocopy) {
856 /* Userspace may produce vectors with count greater than
857 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
858 * to let the rest of data to be fit in the frags.
859 */
860 if (count > MAX_SKB_FRAGS) {
861 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
862 if (copylen < offset)
863 copylen = 0;
864 else
865 copylen -= offset;
866 } else
867 copylen = 0;
868 /* There are 256 bytes to be copied in skb, so there is enough
869 * room for skb expand head in case it is used.
870 * The rest of the buffer is mapped from userspace.
871 */
872 if (copylen < gso.hdr_len)
873 copylen = gso.hdr_len;
874 if (!copylen)
875 copylen = GOODCOPY_LEN;
876 } else
877 copylen = len;
878
Jason Wang54f968d2012-10-31 19:45:57 +0000879 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800880 if (IS_ERR(skb)) {
881 if (PTR_ERR(skb) != -EAGAIN)
882 tun->dev->stats.rx_dropped++;
883 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000886 if (zerocopy)
887 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
888 else
889 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
890
891 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700892 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800893 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Rusty Russellf43798c2008-07-03 03:48:02 -0700897 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
898 if (!skb_partial_csum_set(skb, gso.csum_start,
899 gso.csum_offset)) {
900 tun->dev->stats.rx_frame_errors++;
901 kfree_skb(skb);
902 return -EINVAL;
903 }
Michał Mirosław88255372011-04-19 06:13:10 +0000904 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 switch (tun->flags & TUN_TYPE_MASK) {
907 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700908 if (tun->flags & TUN_NO_PI) {
909 switch (skb->data[0] & 0xf0) {
910 case 0x40:
911 pi.proto = htons(ETH_P_IP);
912 break;
913 case 0x60:
914 pi.proto = htons(ETH_P_IPV6);
915 break;
916 default:
917 tun->dev->stats.rx_dropped++;
918 kfree_skb(skb);
919 return -EINVAL;
920 }
921 }
922
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700923 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700925 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927 case TUN_TAP_DEV:
928 skb->protocol = eth_type_trans(skb, tun->dev);
929 break;
Joe Perches6403eab2011-06-03 11:51:20 +0000930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Rusty Russellf43798c2008-07-03 03:48:02 -0700932 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
933 pr_debug("GSO!\n");
934 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
935 case VIRTIO_NET_HDR_GSO_TCPV4:
936 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
937 break;
938 case VIRTIO_NET_HDR_GSO_TCPV6:
939 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
940 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000941 case VIRTIO_NET_HDR_GSO_UDP:
942 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
943 break;
Rusty Russellf43798c2008-07-03 03:48:02 -0700944 default:
945 tun->dev->stats.rx_frame_errors++;
946 kfree_skb(skb);
947 return -EINVAL;
948 }
949
950 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
951 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
952
953 skb_shinfo(skb)->gso_size = gso.gso_size;
954 if (skb_shinfo(skb)->gso_size == 0) {
955 tun->dev->stats.rx_frame_errors++;
956 kfree_skb(skb);
957 return -EINVAL;
958 }
959
960 /* Header must be checked, and gso_segs computed. */
961 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
962 skb_shinfo(skb)->gso_segs = 0;
963 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400964
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000965 /* copy skb_ubuf_info for callback when skb has no error */
966 if (zerocopy) {
967 skb_shinfo(skb)->destructor_arg = msg_control;
968 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
969 }
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400972
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700973 tun->dev->stats.rx_packets++;
974 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000976 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400977}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700979static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
980 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800982 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -0800983 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +0000984 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000985 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (!tun)
988 return -EBADFD;
989
Joe Perches6b8a66e2011-03-02 07:18:10 +0000990 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jason Wang54f968d2012-10-31 19:45:57 +0000992 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
993 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000994
995 tun_put(tun);
996 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001000static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001001 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001002 struct sk_buff *skb,
1003 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct tun_pi pi = { 0, skb->protocol };
1006 ssize_t total = 0;
1007
1008 if (!(tun->flags & TUN_NO_PI)) {
1009 if ((len -= sizeof(pi)) < 0)
1010 return -EINVAL;
1011
1012 if (len < skb->len) {
1013 /* Packet will be striped */
1014 pi.flags |= TUN_PKT_STRIP;
1015 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001016
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001017 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EFAULT;
1019 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Rusty Russellf43798c2008-07-03 03:48:02 -07001022 if (tun->flags & TUN_VNET_HDR) {
1023 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001024 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001025 return -EINVAL;
1026
1027 if (skb_is_gso(skb)) {
1028 struct skb_shared_info *sinfo = skb_shinfo(skb);
1029
1030 /* This is a hint as to how much should be linear. */
1031 gso.hdr_len = skb_headlen(skb);
1032 gso.gso_size = sinfo->gso_size;
1033 if (sinfo->gso_type & SKB_GSO_TCPV4)
1034 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1035 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1036 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001037 else if (sinfo->gso_type & SKB_GSO_UDP)
1038 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001039 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001040 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001041 "0x%x, gso_size %d, hdr_len %d\n",
1042 sinfo->gso_type, gso.gso_size,
1043 gso.hdr_len);
1044 print_hex_dump(KERN_ERR, "tun: ",
1045 DUMP_PREFIX_NONE,
1046 16, 1, skb->head,
1047 min((int)gso.hdr_len, 64), true);
1048 WARN_ON_ONCE(1);
1049 return -EINVAL;
1050 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001051 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1052 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1053 } else
1054 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1055
1056 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1057 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001058 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001059 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001060 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1061 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001062 } /* else everything is zero */
1063
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001064 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1065 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001066 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001067 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001068 }
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 len = min_t(int, skb->len, len);
1071
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001072 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001073 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001075 tun->dev->stats.tx_packets++;
1076 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 return total;
1079}
1080
Jason Wang54f968d2012-10-31 19:45:57 +00001081static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001082 struct kiocb *iocb, const struct iovec *iv,
1083 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 DECLARE_WAITQUEUE(wait, current);
1086 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001087 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Joe Perches6b8a66e2011-03-02 07:18:10 +00001089 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Amos Kong61a5ff12011-06-09 00:27:10 -07001091 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001092 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 current->state = TASK_INTERRUPTIBLE;
1095
1096 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001097 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001098 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ret = -EAGAIN;
1100 break;
1101 }
1102 if (signal_pending(current)) {
1103 ret = -ERESTARTSYS;
1104 break;
1105 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001106 if (tun->dev->reg_state != NETREG_REGISTERED) {
1107 ret = -EIO;
1108 break;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* Nothing to read, let's sleep */
1112 schedule();
1113 continue;
1114 }
Jason Wangc8d68e62012-10-31 19:46:00 +00001115 netif_wake_subqueue(tun->dev, tfile->queue_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jason Wang54f968d2012-10-31 19:45:57 +00001117 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001118 kfree_skb(skb);
1119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001123 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001124 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001126 return ret;
1127}
1128
1129static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1130 unsigned long count, loff_t pos)
1131{
1132 struct file *file = iocb->ki_filp;
1133 struct tun_file *tfile = file->private_data;
1134 struct tun_struct *tun = __tun_get(tfile);
1135 ssize_t len, ret;
1136
1137 if (!tun)
1138 return -EBADFD;
1139 len = iov_length(iv, count);
1140 if (len < 0) {
1141 ret = -EINVAL;
1142 goto out;
1143 }
1144
Jason Wang54f968d2012-10-31 19:45:57 +00001145 ret = tun_do_read(tun, tfile, iocb, iv, len,
1146 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001147 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001148out:
1149 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return ret;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static void tun_setup(struct net_device *dev)
1154{
1155 struct tun_struct *tun = netdev_priv(dev);
1156
Eric W. Biederman0625c882012-02-07 16:48:55 -08001157 tun->owner = INVALID_UID;
1158 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang54f968d2012-10-31 19:45:57 +00001161 dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001164/* Trivial set of netlink ops to allow deleting tun or tap
1165 * device with netlink.
1166 */
1167static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1168{
1169 return -EINVAL;
1170}
1171
1172static struct rtnl_link_ops tun_link_ops __read_mostly = {
1173 .kind = DRV_NAME,
1174 .priv_size = sizeof(struct tun_struct),
1175 .setup = tun_setup,
1176 .validate = tun_validate,
1177};
1178
Herbert Xu33dccbb2009-02-05 21:25:32 -08001179static void tun_sock_write_space(struct sock *sk)
1180{
Jason Wang54f968d2012-10-31 19:45:57 +00001181 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001182 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001183
1184 if (!sock_writeable(sk))
1185 return;
1186
Herbert Xu33dccbb2009-02-05 21:25:32 -08001187 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1188 return;
1189
Eric Dumazet43815482010-04-29 11:01:49 +00001190 wqueue = sk_sleep(sk);
1191 if (wqueue && waitqueue_active(wqueue))
1192 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001193 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001194
Jason Wang54f968d2012-10-31 19:45:57 +00001195 tfile = container_of(sk, struct tun_file, sk);
1196 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001197}
1198
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001199static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1200 struct msghdr *m, size_t total_len)
1201{
Jason Wang54f968d2012-10-31 19:45:57 +00001202 int ret;
1203 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1204 struct tun_struct *tun = __tun_get(tfile);
1205
1206 if (!tun)
1207 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001208 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1209 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1210 tun_put(tun);
1211 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001212}
1213
Jason Wang54f968d2012-10-31 19:45:57 +00001214
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001215static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1216 struct msghdr *m, size_t total_len,
1217 int flags)
1218{
Jason Wang54f968d2012-10-31 19:45:57 +00001219 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1220 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001221 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001222
1223 if (!tun)
1224 return -EBADFD;
1225
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001226 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1227 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001228 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001229 flags & MSG_DONTWAIT);
1230 if (ret > total_len) {
1231 m->msg_flags |= MSG_TRUNC;
1232 ret = flags & MSG_TRUNC ? ret : total_len;
1233 }
Jason Wang54f968d2012-10-31 19:45:57 +00001234 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001235 return ret;
1236}
1237
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001238static int tun_release(struct socket *sock)
1239{
1240 if (sock->sk)
1241 sock_put(sock->sk);
1242 return 0;
1243}
1244
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001245/* Ops structure to mimic raw sockets with tun */
1246static const struct proto_ops tun_socket_ops = {
1247 .sendmsg = tun_sendmsg,
1248 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001249 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001250};
1251
Herbert Xu33dccbb2009-02-05 21:25:32 -08001252static struct proto tun_proto = {
1253 .name = "tun",
1254 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001255 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001256};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001257
David Woodhouse980c9e82009-05-09 22:54:21 -07001258static int tun_flags(struct tun_struct *tun)
1259{
1260 int flags = 0;
1261
1262 if (tun->flags & TUN_TUN_DEV)
1263 flags |= IFF_TUN;
1264 else
1265 flags |= IFF_TAP;
1266
1267 if (tun->flags & TUN_NO_PI)
1268 flags |= IFF_NO_PI;
1269
1270 if (tun->flags & TUN_ONE_QUEUE)
1271 flags |= IFF_ONE_QUEUE;
1272
1273 if (tun->flags & TUN_VNET_HDR)
1274 flags |= IFF_VNET_HDR;
1275
Jason Wangc8d68e62012-10-31 19:46:00 +00001276 if (tun->flags & TUN_TAP_MQ)
1277 flags |= IFF_MULTI_QUEUE;
1278
David Woodhouse980c9e82009-05-09 22:54:21 -07001279 return flags;
1280}
1281
1282static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1283 char *buf)
1284{
1285 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1286 return sprintf(buf, "0x%x\n", tun_flags(tun));
1287}
1288
1289static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1290 char *buf)
1291{
1292 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001293 return uid_valid(tun->owner)?
1294 sprintf(buf, "%u\n",
1295 from_kuid_munged(current_user_ns(), tun->owner)):
1296 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001297}
1298
1299static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1300 char *buf)
1301{
1302 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001303 return gid_valid(tun->group) ?
1304 sprintf(buf, "%u\n",
1305 from_kgid_munged(current_user_ns(), tun->group)):
1306 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001307}
1308
1309static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1310static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1311static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1312
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001313static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001316 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct net_device *dev;
1318 int err;
1319
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001320 dev = __dev_get_by_name(net, ifr->ifr_name);
1321 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001322 if (ifr->ifr_flags & IFF_TUN_EXCL)
1323 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001324 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1325 tun = netdev_priv(dev);
1326 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1327 tun = netdev_priv(dev);
1328 else
1329 return -EINVAL;
1330
Jason Wangcde8b152012-10-31 19:46:01 +00001331 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001332 return -EPERM;
Jason Wang54f968d2012-10-31 19:45:57 +00001333 err = security_tun_dev_attach(tfile->socket.sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001334 if (err < 0)
1335 return err;
1336
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001337 err = tun_attach(tun, file);
1338 if (err < 0)
1339 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 else {
1342 char *name;
1343 unsigned long flags = 0;
1344
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001345 if (!capable(CAP_NET_ADMIN))
1346 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001347 err = security_tun_dev_create();
1348 if (err < 0)
1349 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* Set dev type */
1352 if (ifr->ifr_flags & IFF_TUN) {
1353 /* TUN device */
1354 flags |= TUN_TUN_DEV;
1355 name = "tun%d";
1356 } else if (ifr->ifr_flags & IFF_TAP) {
1357 /* TAP device */
1358 flags |= TUN_TAP_DEV;
1359 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001360 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001361 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (*ifr->ifr_name)
1364 name = ifr->ifr_name;
1365
Jason Wangc8d68e62012-10-31 19:46:00 +00001366 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1367 tun_setup,
1368 MAX_TAP_QUEUES, MAX_TAP_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (!dev)
1370 return -ENOMEM;
1371
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001372 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001373 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 tun = netdev_priv(dev);
1376 tun->dev = dev;
1377 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001378 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001379 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jason Wang54f968d2012-10-31 19:45:57 +00001381 tun->filter_attached = false;
1382 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001383
Jason Wang54f968d2012-10-31 19:45:57 +00001384 security_tun_dev_post_create(&tfile->sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 tun_net_init(dev);
1387
Michał Mirosław88255372011-04-19 06:13:10 +00001388 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1389 TUN_USER_FEATURES;
1390 dev->features = dev->hw_features;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 err = register_netdevice(tun->dev);
1393 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001394 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001395
David Woodhouse980c9e82009-05-09 22:54:21 -07001396 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1397 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1398 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001399 pr_err("Failed to create tun sysfs files\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001400
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001401 err = tun_attach(tun, file);
1402 if (err < 0)
Jason Wangc8d68e62012-10-31 19:46:00 +00001403 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
Joe Perches6b8a66e2011-03-02 07:18:10 +00001406 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (ifr->ifr_flags & IFF_NO_PI)
1409 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001410 else
1411 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1414 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001415 else
1416 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Rusty Russellf43798c2008-07-03 03:48:02 -07001418 if (ifr->ifr_flags & IFF_VNET_HDR)
1419 tun->flags |= TUN_VNET_HDR;
1420 else
1421 tun->flags &= ~TUN_VNET_HDR;
1422
Jason Wangc8d68e62012-10-31 19:46:00 +00001423 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1424 tun->flags |= TUN_TAP_MQ;
1425 else
1426 tun->flags &= ~TUN_TAP_MQ;
1427
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001428 /* Make sure persistent devices do not get stuck in
1429 * xoff state.
1430 */
1431 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001432 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 strcpy(ifr->ifr_name, tun->dev->name);
1435 return 0;
1436
1437 err_free_dev:
1438 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return err;
1440}
1441
Herbert Xu876bfd42009-08-06 14:22:44 +00001442static int tun_get_iff(struct net *net, struct tun_struct *tun,
1443 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001444{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001445 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001446
1447 strcpy(ifr->ifr_name, tun->dev->name);
1448
David Woodhouse980c9e82009-05-09 22:54:21 -07001449 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001450
1451 return 0;
1452}
1453
Rusty Russell5228ddc2008-07-03 03:46:16 -07001454/* This is like a cut-down ethtool ops, except done via tun fd so no
1455 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001456static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001457{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001458 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001459
1460 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001461 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001462 arg &= ~TUN_F_CSUM;
1463
1464 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1465 if (arg & TUN_F_TSO_ECN) {
1466 features |= NETIF_F_TSO_ECN;
1467 arg &= ~TUN_F_TSO_ECN;
1468 }
1469 if (arg & TUN_F_TSO4)
1470 features |= NETIF_F_TSO;
1471 if (arg & TUN_F_TSO6)
1472 features |= NETIF_F_TSO6;
1473 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1474 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001475
1476 if (arg & TUN_F_UFO) {
1477 features |= NETIF_F_UFO;
1478 arg &= ~TUN_F_UFO;
1479 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001480 }
1481
1482 /* This gives the user a way to test for new features in future by
1483 * trying to set them. */
1484 if (arg)
1485 return -EINVAL;
1486
Michał Mirosław88255372011-04-19 06:13:10 +00001487 tun->set_features = features;
1488 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001489
1490 return 0;
1491}
1492
Jason Wangc8d68e62012-10-31 19:46:00 +00001493static void tun_detach_filter(struct tun_struct *tun, int n)
1494{
1495 int i;
1496 struct tun_file *tfile;
1497
1498 for (i = 0; i < n; i++) {
1499 tfile = rcu_dereference_protected(tun->tfiles[i],
1500 lockdep_rtnl_is_held());
1501 sk_detach_filter(tfile->socket.sk);
1502 }
1503
1504 tun->filter_attached = false;
1505}
1506
1507static int tun_attach_filter(struct tun_struct *tun)
1508{
1509 int i, ret = 0;
1510 struct tun_file *tfile;
1511
1512 for (i = 0; i < tun->numqueues; i++) {
1513 tfile = rcu_dereference_protected(tun->tfiles[i],
1514 lockdep_rtnl_is_held());
1515 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1516 if (ret) {
1517 tun_detach_filter(tun, i);
1518 return ret;
1519 }
1520 }
1521
1522 tun->filter_attached = true;
1523 return ret;
1524}
1525
1526static void tun_set_sndbuf(struct tun_struct *tun)
1527{
1528 struct tun_file *tfile;
1529 int i;
1530
1531 for (i = 0; i < tun->numqueues; i++) {
1532 tfile = rcu_dereference_protected(tun->tfiles[i],
1533 lockdep_rtnl_is_held());
1534 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1535 }
1536}
1537
Jason Wangcde8b152012-10-31 19:46:01 +00001538static int tun_set_queue(struct file *file, struct ifreq *ifr)
1539{
1540 struct tun_file *tfile = file->private_data;
1541 struct tun_struct *tun;
1542 struct net_device *dev;
1543 int ret = 0;
1544
1545 rtnl_lock();
1546
1547 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
1548 dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
1549 if (!dev) {
1550 ret = -EINVAL;
1551 goto unlock;
1552 }
1553
1554 tun = netdev_priv(dev);
1555 if (dev->netdev_ops != &tap_netdev_ops &&
1556 dev->netdev_ops != &tun_netdev_ops)
1557 ret = -EINVAL;
1558 else if (tun_not_capable(tun))
1559 ret = -EPERM;
1560 else
1561 ret = tun_attach(tun, file);
1562 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
1563 __tun_detach(tfile, false);
1564 else
1565 ret = -EINVAL;
1566
1567unlock:
1568 rtnl_unlock();
1569 return ret;
1570}
1571
Arnd Bergmann50857e22009-11-06 22:52:32 -08001572static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1573 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001575 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001576 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 void __user* argp = (void __user*)arg;
1578 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001579 kuid_t owner;
1580 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001581 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001582 int vnet_hdr_sz;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001583 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jason Wangcde8b152012-10-31 19:46:01 +00001585 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001586 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001588 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001589 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001590 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001591 if (cmd == TUNGETFEATURES) {
1592 /* Currently this just means: "what IFF flags are valid?".
1593 * This is needed because we never checked for invalid flags on
1594 * TUNSETIFF. */
1595 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
Jason Wangcde8b152012-10-31 19:46:01 +00001596 IFF_VNET_HDR | IFF_MULTI_QUEUE,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001597 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001598 } else if (cmd == TUNSETQUEUE)
1599 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001600
Jason Wangc8d68e62012-10-31 19:46:00 +00001601 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001602 rtnl_lock();
1603
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001604 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1607
Herbert Xu876bfd42009-08-06 14:22:44 +00001608 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Herbert Xu876bfd42009-08-06 14:22:44 +00001610 if (ret)
1611 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Arnd Bergmann50857e22009-11-06 22:52:32 -08001613 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001614 ret = -EFAULT;
1615 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Herbert Xu876bfd42009-08-06 14:22:44 +00001618 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001620 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Jason Wang1e588332012-10-31 19:45:56 +00001622 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Eric W. Biederman631ab462009-01-20 11:00:40 +00001624 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001626 case TUNGETIFF:
Herbert Xu876bfd42009-08-06 14:22:44 +00001627 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001628 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001629 break;
Mark McLoughline3b99552008-08-15 15:09:56 -07001630
Arnd Bergmann50857e22009-11-06 22:52:32 -08001631 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001632 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001633 break;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 case TUNSETNOCSUM:
1636 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Michał Mirosław88255372011-04-19 06:13:10 +00001638 /* [unimplemented] */
1639 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001640 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642
1643 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001644 /* Disable/Enable persist mode. Keep an extra reference to the
1645 * module to prevent the module being unprobed.
1646 */
1647 if (arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001649 __module_get(THIS_MODULE);
1650 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001652 module_put(THIS_MODULE);
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Joe Perches6b8a66e2011-03-02 07:18:10 +00001655 tun_debug(KERN_INFO, tun, "persist %s\n",
1656 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
1658
1659 case TUNSETOWNER:
1660 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001661 owner = make_kuid(current_user_ns(), arg);
1662 if (!uid_valid(owner)) {
1663 ret = -EINVAL;
1664 break;
1665 }
1666 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001667 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001668 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
1670
Guido Guenther8c644622007-07-02 22:50:25 -07001671 case TUNSETGROUP:
1672 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001673 group = make_kgid(current_user_ns(), arg);
1674 if (!gid_valid(group)) {
1675 ret = -EINVAL;
1676 break;
1677 }
1678 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001679 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001680 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001681 break;
1682
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001683 case TUNSETLINK:
1684 /* Only allow setting the type when the interface is down */
1685 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001686 tun_debug(KERN_INFO, tun,
1687 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001688 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001689 } else {
1690 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001691 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1692 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001693 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001694 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001695 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifdef TUN_DEBUG
1698 case TUNSETDEBUG:
1699 tun->debug = arg;
1700 break;
1701#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001702 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001703 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001704 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001705
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001706 case TUNSETTXFILTER:
1707 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001708 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001709 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001710 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001711 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001715 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001716 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1717 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001718 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001719 ret = -EFAULT;
1720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001723 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001724 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1725 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001726
Kim B. Heino40102372008-02-29 12:26:21 -08001727 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001728 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001729
1730 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001731 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001732 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1733 ret = -EFAULT;
1734 break;
1735
1736 case TUNSETSNDBUF:
1737 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1738 ret = -EFAULT;
1739 break;
1740 }
1741
Jason Wangc8d68e62012-10-31 19:46:00 +00001742 tun->sndbuf = sndbuf;
1743 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001744 break;
1745
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001746 case TUNGETVNETHDRSZ:
1747 vnet_hdr_sz = tun->vnet_hdr_sz;
1748 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1749 ret = -EFAULT;
1750 break;
1751
1752 case TUNSETVNETHDRSZ:
1753 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1754 ret = -EFAULT;
1755 break;
1756 }
1757 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1758 ret = -EINVAL;
1759 break;
1760 }
1761
1762 tun->vnet_hdr_sz = vnet_hdr_sz;
1763 break;
1764
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001765 case TUNATTACHFILTER:
1766 /* Can be set only for TAPs */
1767 ret = -EINVAL;
1768 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1769 break;
1770 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00001771 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001772 break;
1773
Jason Wangc8d68e62012-10-31 19:46:00 +00001774 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001775 break;
1776
1777 case TUNDETACHFILTER:
1778 /* Can be set only for TAPs */
1779 ret = -EINVAL;
1780 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1781 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00001782 ret = 0;
1783 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001784 break;
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001787 ret = -EINVAL;
1788 break;
Joe Perchesee289b62010-05-17 22:47:34 -07001789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Herbert Xu876bfd42009-08-06 14:22:44 +00001791unlock:
1792 rtnl_unlock();
1793 if (tun)
1794 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Arnd Bergmann50857e22009-11-06 22:52:32 -08001798static long tun_chr_ioctl(struct file *file,
1799 unsigned int cmd, unsigned long arg)
1800{
1801 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1802}
1803
1804#ifdef CONFIG_COMPAT
1805static long tun_chr_compat_ioctl(struct file *file,
1806 unsigned int cmd, unsigned long arg)
1807{
1808 switch (cmd) {
1809 case TUNSETIFF:
1810 case TUNGETIFF:
1811 case TUNSETTXFILTER:
1812 case TUNGETSNDBUF:
1813 case TUNSETSNDBUF:
1814 case SIOCGIFHWADDR:
1815 case SIOCSIFHWADDR:
1816 arg = (unsigned long)compat_ptr(arg);
1817 break;
1818 default:
1819 arg = (compat_ulong_t)arg;
1820 break;
1821 }
1822
1823 /*
1824 * compat_ifreq is shorter than ifreq, so we must not access beyond
1825 * the end of that structure. All fields that are used in this
1826 * driver are compatible though, we don't need to convert the
1827 * contents.
1828 */
1829 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1830}
1831#endif /* CONFIG_COMPAT */
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833static int tun_chr_fasync(int fd, struct file *file, int on)
1834{
Jason Wang54f968d2012-10-31 19:45:57 +00001835 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int ret;
1837
Jason Wang54f968d2012-10-31 19:45:57 +00001838 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001839 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001842 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001844 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00001845 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001846 } else
Jason Wang54f968d2012-10-31 19:45:57 +00001847 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001848 ret = 0;
1849out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06001850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853static int tun_chr_open(struct inode *inode, struct file * file)
1854{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001855 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07001856
Joe Perches6b8a66e2011-03-02 07:18:10 +00001857 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001858
Jason Wang54f968d2012-10-31 19:45:57 +00001859 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1860 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001861 if (!tfile)
1862 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00001863 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001864 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00001865 tfile->flags = 0;
1866
1867 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1868 init_waitqueue_head(&tfile->wq.wait);
1869
1870 tfile->socket.file = file;
1871 tfile->socket.ops = &tun_socket_ops;
1872
1873 sock_init_data(&tfile->socket, &tfile->sk);
1874 sk_change_net(&tfile->sk, tfile->net);
1875
1876 tfile->sk.sk_write_space = tun_sock_write_space;
1877 tfile->sk.sk_sndbuf = INT_MAX;
1878
Eric W. Biederman631ab462009-01-20 11:00:40 +00001879 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00001880 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883}
1884
1885static int tun_chr_close(struct inode *inode, struct file *file)
1886{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001887 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00001888 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jason Wangc8d68e62012-10-31 19:46:00 +00001890 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00001891 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 return 0;
1894}
1895
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001896static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001897 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001899 .read = do_sync_read,
1900 .aio_read = tun_chr_aio_read,
1901 .write = do_sync_write,
1902 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08001904 .unlocked_ioctl = tun_chr_ioctl,
1905#ifdef CONFIG_COMPAT
1906 .compat_ioctl = tun_chr_compat_ioctl,
1907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 .open = tun_chr_open,
1909 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001910 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
1913static struct miscdevice tun_miscdev = {
1914 .minor = TUN_MINOR,
1915 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02001916 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918};
1919
1920/* ethtool interface */
1921
1922static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1923{
1924 cmd->supported = 0;
1925 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00001926 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 cmd->duplex = DUPLEX_FULL;
1928 cmd->port = PORT_TP;
1929 cmd->phy_address = 0;
1930 cmd->transceiver = XCVR_INTERNAL;
1931 cmd->autoneg = AUTONEG_DISABLE;
1932 cmd->maxtxpkt = 0;
1933 cmd->maxrxpkt = 0;
1934 return 0;
1935}
1936
1937static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1938{
1939 struct tun_struct *tun = netdev_priv(dev);
1940
Rick Jones33a5ba12011-11-15 14:59:53 +00001941 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1942 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 switch (tun->flags & TUN_TYPE_MASK) {
1945 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001946 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
1948 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001949 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951 }
1952}
1953
1954static u32 tun_get_msglevel(struct net_device *dev)
1955{
1956#ifdef TUN_DEBUG
1957 struct tun_struct *tun = netdev_priv(dev);
1958 return tun->debug;
1959#else
1960 return -EOPNOTSUPP;
1961#endif
1962}
1963
1964static void tun_set_msglevel(struct net_device *dev, u32 value)
1965{
1966#ifdef TUN_DEBUG
1967 struct tun_struct *tun = netdev_priv(dev);
1968 tun->debug = value;
1969#endif
1970}
1971
Jeff Garzik7282d492006-09-13 14:30:00 -04001972static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 .get_settings = tun_get_settings,
1974 .get_drvinfo = tun_get_drvinfo,
1975 .get_msglevel = tun_get_msglevel,
1976 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00001977 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978};
1979
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static int __init tun_init(void)
1982{
1983 int ret = 0;
1984
Joe Perches6b8a66e2011-03-02 07:18:10 +00001985 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1986 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001988 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001989 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001990 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001991 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001992 }
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001995 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001996 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001997 goto err_misc;
1998 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001999 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002000err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002001 rtnl_link_unregister(&tun_link_ops);
2002err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return ret;
2004}
2005
2006static void tun_cleanup(void)
2007{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002008 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002009 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002012/* Get an underlying socket object from tun file. Returns error unless file is
2013 * attached to a device. The returned object works like a packet socket, it
2014 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2015 * holding a reference to the file for as long as the socket is in use. */
2016struct socket *tun_get_socket(struct file *file)
2017{
Jason Wang6e914fc2012-10-31 19:45:58 +00002018 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002019 if (file->f_op != &tun_fops)
2020 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002021 tfile = file->private_data;
2022 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002023 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002024 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002025}
2026EXPORT_SYMBOL_GPL(tun_get_socket);
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028module_init(tun_init);
2029module_exit(tun_cleanup);
2030MODULE_DESCRIPTION(DRV_DESCRIPTION);
2031MODULE_AUTHOR(DRV_COPYRIGHT);
2032MODULE_LICENSE("GPL");
2033MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002034MODULE_ALIAS("devname:net/tun");