blob: 55f3a3e667a9af0291da7c7d4e46c12e84d7ce6a [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>
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -070025 * Use random_ether_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
37#define DRV_NAME "tun"
38#define DRV_VERSION "1.6"
39#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
40#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
43#include <linux/errno.h>
44#include <linux/kernel.h>
45#include <linux/major.h>
46#include <linux/slab.h>
47#include <linux/poll.h>
48#include <linux/fcntl.h>
49#include <linux/init.h>
50#include <linux/skbuff.h>
51#include <linux/netdevice.h>
52#include <linux/etherdevice.h>
53#include <linux/miscdevice.h>
54#include <linux/ethtool.h>
55#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080056#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/if.h>
58#include <linux/if_arp.h>
59#include <linux/if_ether.h>
60#include <linux/if_tun.h>
61#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070062#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070063#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000064#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070065#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070066#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080067#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080068#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <asm/system.h>
71#include <asm/uaccess.h>
72
Rusty Russell14daa022008-04-12 18:48:58 -070073/* Uncomment to enable debugging */
74/* #define TUN_DEBUG 1 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#ifdef TUN_DEBUG
77static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070078
79#define DBG if(tun->debug)printk
80#define DBG1 if(debug==2)printk
81#else
82#define DBG( a... )
83#define DBG1( a... )
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -070086#define FLT_EXACT_COUNT 8
87struct tap_filter {
88 unsigned int count; /* Number of addrs. Zero means disabled */
89 u32 mask[2]; /* Mask of the hashed addrs */
90 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
91};
92
Eric W. Biederman631ab462009-01-20 11:00:40 +000093struct tun_file {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +000094 atomic_t count;
Eric W. Biederman631ab462009-01-20 11:00:40 +000095 struct tun_struct *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +000096 struct net *net;
Eric W. Biederman631ab462009-01-20 11:00:40 +000097};
98
Herbert Xu33dccbb2009-02-05 21:25:32 -080099struct tun_sock;
100
Rusty Russell14daa022008-04-12 18:48:58 -0700101struct tun_struct {
Eric W. Biederman631ab462009-01-20 11:00:40 +0000102 struct tun_file *tfile;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700103 unsigned int flags;
Rusty Russell14daa022008-04-12 18:48:58 -0700104 uid_t owner;
105 gid_t group;
106
Rusty Russell14daa022008-04-12 18:48:58 -0700107 struct net_device *dev;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700108 struct fasync_struct *fasync;
Rusty Russell14daa022008-04-12 18:48:58 -0700109
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700110 struct tap_filter txflt;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800111 struct socket socket;
Eric Dumazet43815482010-04-29 11:01:49 +0000112 struct socket_wq wq;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200113
114 int vnet_hdr_sz;
115
Rusty Russell14daa022008-04-12 18:48:58 -0700116#ifdef TUN_DEBUG
117 int debug;
118#endif
119};
120
Herbert Xu33dccbb2009-02-05 21:25:32 -0800121struct tun_sock {
122 struct sock sk;
123 struct tun_struct *tun;
124};
125
126static inline struct tun_sock *tun_sk(struct sock *sk)
127{
128 return container_of(sk, struct tun_sock, sk);
129}
130
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000131static int tun_attach(struct tun_struct *tun, struct file *file)
132{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000133 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000134 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000135
136 ASSERT_RTNL();
137
Eric W. Biederman38231b72009-01-20 11:02:28 +0000138 netif_tx_lock_bh(tun->dev);
139
140 err = -EINVAL;
141 if (tfile->tun)
142 goto out;
143
144 err = -EBUSY;
145 if (tun->tfile)
146 goto out;
147
148 err = 0;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000149 tfile->tun = tun;
150 tun->tfile = tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000151 tun->socket.file = file;
Nolan Leakebee31362010-07-27 13:53:43 +0000152 netif_carrier_on(tun->dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000153 dev_hold(tun->dev);
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000154 sock_hold(tun->socket.sk);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000155 atomic_inc(&tfile->count);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000156
Eric W. Biederman38231b72009-01-20 11:02:28 +0000157out:
158 netif_tx_unlock_bh(tun->dev);
159 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000160}
161
Eric W. Biederman631ab462009-01-20 11:00:40 +0000162static void __tun_detach(struct tun_struct *tun)
163{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000164 /* Detach from net device */
Eric W. Biederman38231b72009-01-20 11:02:28 +0000165 netif_tx_lock_bh(tun->dev);
Nolan Leakebee31362010-07-27 13:53:43 +0000166 netif_carrier_off(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000167 tun->tfile = NULL;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000168 tun->socket.file = NULL;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000169 netif_tx_unlock_bh(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000170
171 /* Drop read queue */
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000172 skb_queue_purge(&tun->socket.sk->sk_receive_queue);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000173
174 /* Drop the extra count on the net device */
175 dev_put(tun->dev);
176}
177
178static void tun_detach(struct tun_struct *tun)
179{
180 rtnl_lock();
181 __tun_detach(tun);
182 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +0000183}
184
185static struct tun_struct *__tun_get(struct tun_file *tfile)
186{
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000187 struct tun_struct *tun = NULL;
188
189 if (atomic_inc_not_zero(&tfile->count))
190 tun = tfile->tun;
191
192 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000193}
194
195static struct tun_struct *tun_get(struct file *file)
196{
197 return __tun_get(file->private_data);
198}
199
200static void tun_put(struct tun_struct *tun)
201{
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000202 struct tun_file *tfile = tun->tfile;
203
204 if (atomic_dec_and_test(&tfile->count))
205 tun_detach(tfile->tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000206}
207
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700208/* TAP filterting */
209static void addr_hash_set(u32 *mask, const u8 *addr)
210{
211 int n = ether_crc(ETH_ALEN, addr) >> 26;
212 mask[n >> 5] |= (1 << (n & 31));
213}
214
215static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
216{
217 int n = ether_crc(ETH_ALEN, addr) >> 26;
218 return mask[n >> 5] & (1 << (n & 31));
219}
220
221static int update_filter(struct tap_filter *filter, void __user *arg)
222{
223 struct { u8 u[ETH_ALEN]; } *addr;
224 struct tun_filter uf;
225 int err, alen, n, nexact;
226
227 if (copy_from_user(&uf, arg, sizeof(uf)))
228 return -EFAULT;
229
230 if (!uf.count) {
231 /* Disabled */
232 filter->count = 0;
233 return 0;
234 }
235
236 alen = ETH_ALEN * uf.count;
237 addr = kmalloc(alen, GFP_KERNEL);
238 if (!addr)
239 return -ENOMEM;
240
241 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
242 err = -EFAULT;
243 goto done;
244 }
245
246 /* The filter is updated without holding any locks. Which is
247 * perfectly safe. We disable it first and in the worst
248 * case we'll accept a few undesired packets. */
249 filter->count = 0;
250 wmb();
251
252 /* Use first set of addresses as an exact filter */
253 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
254 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
255
256 nexact = n;
257
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800258 /* Remaining multicast addresses are hashed,
259 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700260 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800261 for (; n < uf.count; n++) {
262 if (!is_multicast_ether_addr(addr[n].u)) {
263 err = 0; /* no filter */
264 goto done;
265 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700266 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800267 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700268
269 /* For ALLMULTI just set the mask to all ones.
270 * This overrides the mask populated above. */
271 if ((uf.flags & TUN_FLT_ALLMULTI))
272 memset(filter->mask, ~0, sizeof(filter->mask));
273
274 /* Now enable the filter */
275 wmb();
276 filter->count = nexact;
277
278 /* Return the number of exact filters */
279 err = nexact;
280
281done:
282 kfree(addr);
283 return err;
284}
285
286/* Returns: 0 - drop, !=0 - accept */
287static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
288{
289 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
290 * at this point. */
291 struct ethhdr *eh = (struct ethhdr *) skb->data;
292 int i;
293
294 /* Exact match */
295 for (i = 0; i < filter->count; i++)
296 if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
297 return 1;
298
299 /* Inexact match (multicast only) */
300 if (is_multicast_ether_addr(eh->h_dest))
301 return addr_hash_test(filter->mask, eh->h_dest);
302
303 return 0;
304}
305
306/*
307 * Checks whether the packet is accepted or not.
308 * Returns: 0 - drop, !=0 - accept
309 */
310static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
311{
312 if (!filter->count)
313 return 1;
314
315 return run_filter(filter, skb);
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* Network device part of the driver */
319
Jeff Garzik7282d492006-09-13 14:30:00 -0400320static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000322/* Net device detach from fd. */
323static void tun_net_uninit(struct net_device *dev)
324{
325 struct tun_struct *tun = netdev_priv(dev);
326 struct tun_file *tfile = tun->tfile;
327
328 /* Inform the methods they need to stop using the dev.
329 */
330 if (tfile) {
Eric Dumazet43815482010-04-29 11:01:49 +0000331 wake_up_all(&tun->wq.wait);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000332 if (atomic_dec_and_test(&tfile->count))
333 __tun_detach(tun);
334 }
335}
336
Herbert Xu9c3fea62009-04-18 14:15:52 +0000337static void tun_free_netdev(struct net_device *dev)
338{
339 struct tun_struct *tun = netdev_priv(dev);
340
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000341 sock_put(tun->socket.sk);
Herbert Xu9c3fea62009-04-18 14:15:52 +0000342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* Net device open. */
345static int tun_net_open(struct net_device *dev)
346{
347 netif_start_queue(dev);
348 return 0;
349}
350
351/* Net device close. */
352static int tun_net_close(struct net_device *dev)
353{
354 netif_stop_queue(dev);
355 return 0;
356}
357
358/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000359static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct tun_struct *tun = netdev_priv(dev);
362
363 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
364
365 /* Drop packet if interface is not attached */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000366 if (!tun->tfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto drop;
368
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700369 /* Drop if the filter does not like it.
370 * This is a noop if the filter is disabled.
371 * Filter can be enabled only for the TAP devices. */
372 if (!check_filter(&tun->txflt, skb))
373 goto drop;
374
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000375 if (tun->socket.sk->sk_filter &&
376 sk_filter(tun->socket.sk, skb))
377 goto drop;
378
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000379 if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!(tun->flags & TUN_ONE_QUEUE)) {
381 /* Normal queueing mode. */
382 /* Packet scheduler handles dropping of further packets. */
383 netif_stop_queue(dev);
384
385 /* We won't see all dropped packets individually, so overrun
386 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700387 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else {
389 /* Single queue mode.
390 * Driver handles dropping of all packets itself. */
391 goto drop;
392 }
393 }
394
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000395 /* Orphan the skb - required as we might hang on to it
396 * for indefinite time. */
397 skb_orphan(skb);
398
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700399 /* Enqueue packet */
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000400 skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* Notify and wake up reader process */
403 if (tun->flags & TUN_FASYNC)
404 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
Eric Dumazet43815482010-04-29 11:01:49 +0000405 wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000406 POLLRDNORM | POLLRDBAND);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000407 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700410 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000412 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700415static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700417 /*
418 * This callback is supposed to deal with mc filter in
419 * _rx_ path and has nothing to do with the _tx_ path.
420 * In rx path we always accept everything userspace gives us.
421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Ed Swierk4885a502007-09-16 12:21:38 -0700424#define MIN_MTU 68
425#define MAX_MTU 65535
426
427static int
428tun_net_change_mtu(struct net_device *dev, int new_mtu)
429{
430 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
431 return -EINVAL;
432 dev->mtu = new_mtu;
433 return 0;
434}
435
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800436static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000437 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800438 .ndo_open = tun_net_open,
439 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800440 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800441 .ndo_change_mtu = tun_net_change_mtu,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800442};
443
444static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000445 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800446 .ndo_open = tun_net_open,
447 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800448 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800449 .ndo_change_mtu = tun_net_change_mtu,
450 .ndo_set_multicast_list = tun_net_mclist,
451 .ndo_set_mac_address = eth_mac_addr,
452 .ndo_validate_addr = eth_validate_addr,
453};
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/* Initialize net device. */
456static void tun_net_init(struct net_device *dev)
457{
458 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 switch (tun->flags & TUN_TYPE_MASK) {
461 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800462 dev->netdev_ops = &tun_netdev_ops;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Point-to-Point TUN Device */
465 dev->hard_header_len = 0;
466 dev->addr_len = 0;
467 dev->mtu = 1500;
468
469 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400470 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
472 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
473 break;
474
475 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800476 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700478 ether_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700480 random_ether_addr(dev->dev_addr);
Brian Braunstein36226a82007-04-26 01:00:55 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
483 break;
484 }
485}
486
487/* Character device part */
488
489/* Poll */
490static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400491{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000492 struct tun_file *tfile = file->private_data;
493 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000494 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800495 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000498 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000500 sk = tun->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
503
Eric Dumazet43815482010-04-29 11:01:49 +0000504 poll_wait(file, &tun->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400505
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000506 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 mask |= POLLIN | POLLRDNORM;
508
Herbert Xu33dccbb2009-02-05 21:25:32 -0800509 if (sock_writeable(sk) ||
510 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
511 sock_writeable(sk)))
512 mask |= POLLOUT | POLLWRNORM;
513
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000514 if (tun->dev->reg_state != NETREG_REGISTERED)
515 mask = POLLERR;
516
Eric W. Biederman631ab462009-01-20 11:00:40 +0000517 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return mask;
519}
520
Rusty Russellf42157c2008-08-15 15:15:10 -0700521/* prepad is the amount to reserve at front. len is length after that.
522 * linear is a hint as to how much to copy (usually headers). */
Herbert Xu33dccbb2009-02-05 21:25:32 -0800523static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
524 size_t prepad, size_t len,
525 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700526{
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000527 struct sock *sk = tun->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700528 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800529 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700530
Herbert Xu82862742010-05-24 00:14:10 -0700531 sock_update_classid(sk);
532
Rusty Russellf42157c2008-08-15 15:15:10 -0700533 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700534 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800535 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700536
Herbert Xu33dccbb2009-02-05 21:25:32 -0800537 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
538 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700539 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800540 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700541
542 skb_reserve(skb, prepad);
543 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800544 skb->data_len = len - linear;
545 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700546
547 return skb;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/* Get packet from user space buffer */
Herbert Xu33dccbb2009-02-05 21:25:32 -0800551static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000552 const struct iovec *iv, size_t count,
Herbert Xu33dccbb2009-02-05 21:25:32 -0800553 int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Harvey Harrison09640e62009-02-01 00:45:17 -0800555 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct sk_buff *skb;
557 size_t len = count, align = 0;
Rusty Russellf43798c2008-07-03 03:48:02 -0700558 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000559 int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!(tun->flags & TUN_NO_PI)) {
562 if ((len -= sizeof(pi)) > count)
563 return -EINVAL;
564
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000565 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000567 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Rusty Russellf43798c2008-07-03 03:48:02 -0700570 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200571 if ((len -= tun->vnet_hdr_sz) > count)
Rusty Russellf43798c2008-07-03 03:48:02 -0700572 return -EINVAL;
573
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000574 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700575 return -EFAULT;
576
Herbert Xu49091222009-06-08 00:20:01 -0700577 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
578 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
579 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
580
Rusty Russellf43798c2008-07-03 03:48:02 -0700581 if (gso.hdr_len > len)
582 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200583 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700584 }
585
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700586 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 align = NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -0700588 if (unlikely(len < ETH_HLEN ||
589 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700590 return -EINVAL;
591 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400592
Herbert Xu33dccbb2009-02-05 21:25:32 -0800593 skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
594 if (IS_ERR(skb)) {
595 if (PTR_ERR(skb) != -EAGAIN)
596 tun->dev->stats.rx_dropped++;
597 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000600 if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700601 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800602 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Rusty Russellf43798c2008-07-03 03:48:02 -0700606 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
607 if (!skb_partial_csum_set(skb, gso.csum_start,
608 gso.csum_offset)) {
609 tun->dev->stats.rx_frame_errors++;
610 kfree_skb(skb);
611 return -EINVAL;
612 }
613 } else if (tun->flags & TUN_NOCHECKSUM)
614 skb->ip_summed = CHECKSUM_UNNECESSARY;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 switch (tun->flags & TUN_TYPE_MASK) {
617 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700618 if (tun->flags & TUN_NO_PI) {
619 switch (skb->data[0] & 0xf0) {
620 case 0x40:
621 pi.proto = htons(ETH_P_IP);
622 break;
623 case 0x60:
624 pi.proto = htons(ETH_P_IPV6);
625 break;
626 default:
627 tun->dev->stats.rx_dropped++;
628 kfree_skb(skb);
629 return -EINVAL;
630 }
631 }
632
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700633 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700635 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 case TUN_TAP_DEV:
638 skb->protocol = eth_type_trans(skb, tun->dev);
639 break;
640 };
641
Rusty Russellf43798c2008-07-03 03:48:02 -0700642 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
643 pr_debug("GSO!\n");
644 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
645 case VIRTIO_NET_HDR_GSO_TCPV4:
646 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
647 break;
648 case VIRTIO_NET_HDR_GSO_TCPV6:
649 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
650 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000651 case VIRTIO_NET_HDR_GSO_UDP:
652 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
653 break;
Rusty Russellf43798c2008-07-03 03:48:02 -0700654 default:
655 tun->dev->stats.rx_frame_errors++;
656 kfree_skb(skb);
657 return -EINVAL;
658 }
659
660 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
661 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
662
663 skb_shinfo(skb)->gso_size = gso.gso_size;
664 if (skb_shinfo(skb)->gso_size == 0) {
665 tun->dev->stats.rx_frame_errors++;
666 kfree_skb(skb);
667 return -EINVAL;
668 }
669
670 /* Header must be checked, and gso_segs computed. */
671 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
672 skb_shinfo(skb)->gso_segs = 0;
673 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400676
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700677 tun->dev->stats.rx_packets++;
678 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 return count;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700683static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
684 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800686 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -0800687 struct tun_struct *tun = tun_get(file);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000688 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (!tun)
691 return -EBADFD;
692
693 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
694
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000695 result = tun_get_user(tun, iv, iov_length(iv, count),
Herbert Xu33dccbb2009-02-05 21:25:32 -0800696 file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000697
698 tun_put(tun);
699 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* Put packet to the user space buffer */
703static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
704 struct sk_buff *skb,
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000705 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct tun_pi pi = { 0, skb->protocol };
708 ssize_t total = 0;
709
710 if (!(tun->flags & TUN_NO_PI)) {
711 if ((len -= sizeof(pi)) < 0)
712 return -EINVAL;
713
714 if (len < skb->len) {
715 /* Packet will be striped */
716 pi.flags |= TUN_PKT_STRIP;
717 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400718
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000719 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -EFAULT;
721 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Rusty Russellf43798c2008-07-03 03:48:02 -0700724 if (tun->flags & TUN_VNET_HDR) {
725 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200726 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -0700727 return -EINVAL;
728
729 if (skb_is_gso(skb)) {
730 struct skb_shared_info *sinfo = skb_shinfo(skb);
731
732 /* This is a hint as to how much should be linear. */
733 gso.hdr_len = skb_headlen(skb);
734 gso.gso_size = sinfo->gso_size;
735 if (sinfo->gso_type & SKB_GSO_TCPV4)
736 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
737 else if (sinfo->gso_type & SKB_GSO_TCPV6)
738 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000739 else if (sinfo->gso_type & SKB_GSO_UDP)
740 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +0000741 else {
742 printk(KERN_ERR "tun: unexpected GSO type: "
743 "0x%x, gso_size %d, hdr_len %d\n",
744 sinfo->gso_type, gso.gso_size,
745 gso.hdr_len);
746 print_hex_dump(KERN_ERR, "tun: ",
747 DUMP_PREFIX_NONE,
748 16, 1, skb->head,
749 min((int)gso.hdr_len, 64), true);
750 WARN_ON_ONCE(1);
751 return -EINVAL;
752 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700753 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
754 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
755 } else
756 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
757
758 if (skb->ip_summed == CHECKSUM_PARTIAL) {
759 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
760 gso.csum_start = skb->csum_start - skb_headroom(skb);
761 gso.csum_offset = skb->csum_offset;
762 } /* else everything is zero */
763
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000764 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
765 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -0700766 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200767 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 len = min_t(int, skb->len, len);
771
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000772 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000773 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700775 tun->dev->stats.tx_packets++;
776 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return total;
779}
780
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000781static ssize_t tun_do_read(struct tun_struct *tun,
782 struct kiocb *iocb, const struct iovec *iv,
783 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 DECLARE_WAITQUEUE(wait, current);
786 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000787 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
790
Eric Dumazet43815482010-04-29 11:01:49 +0000791 add_wait_queue(&tun->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 current->state = TASK_INTERRUPTIBLE;
794
795 /* Read frames from the queue */
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000796 if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000797 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ret = -EAGAIN;
799 break;
800 }
801 if (signal_pending(current)) {
802 ret = -ERESTARTSYS;
803 break;
804 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000805 if (tun->dev->reg_state != NETREG_REGISTERED) {
806 ret = -EIO;
807 break;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* Nothing to read, let's sleep */
811 schedule();
812 continue;
813 }
814 netif_wake_queue(tun->dev);
815
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000816 ret = tun_put_user(tun, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700817 kfree_skb(skb);
818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 current->state = TASK_RUNNING;
Eric Dumazet43815482010-04-29 11:01:49 +0000822 remove_wait_queue(&tun->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000824 return ret;
825}
826
827static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
828 unsigned long count, loff_t pos)
829{
830 struct file *file = iocb->ki_filp;
831 struct tun_file *tfile = file->private_data;
832 struct tun_struct *tun = __tun_get(tfile);
833 ssize_t len, ret;
834
835 if (!tun)
836 return -EBADFD;
837 len = iov_length(iv, count);
838 if (len < 0) {
839 ret = -EINVAL;
840 goto out;
841 }
842
843 ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
844 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000845out:
846 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return ret;
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static void tun_setup(struct net_device *dev)
851{
852 struct tun_struct *tun = netdev_priv(dev);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 tun->owner = -1;
Guido Guenther8c644622007-07-02 22:50:25 -0700855 tun->group = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 dev->ethtool_ops = &tun_ethtool_ops;
Herbert Xu9c3fea62009-04-18 14:15:52 +0000858 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -0800861/* Trivial set of netlink ops to allow deleting tun or tap
862 * device with netlink.
863 */
864static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
865{
866 return -EINVAL;
867}
868
869static struct rtnl_link_ops tun_link_ops __read_mostly = {
870 .kind = DRV_NAME,
871 .priv_size = sizeof(struct tun_struct),
872 .setup = tun_setup,
873 .validate = tun_validate,
874};
875
Herbert Xu33dccbb2009-02-05 21:25:32 -0800876static void tun_sock_write_space(struct sock *sk)
877{
878 struct tun_struct *tun;
Eric Dumazet43815482010-04-29 11:01:49 +0000879 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800880
881 if (!sock_writeable(sk))
882 return;
883
Herbert Xu33dccbb2009-02-05 21:25:32 -0800884 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
885 return;
886
Eric Dumazet43815482010-04-29 11:01:49 +0000887 wqueue = sk_sleep(sk);
888 if (wqueue && waitqueue_active(wqueue))
889 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000890 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -0700891
Vitaliy Gusev80924e52009-12-25 07:17:43 +0000892 tun = tun_sk(sk)->tun;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800893 kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
894}
895
896static void tun_sock_destruct(struct sock *sk)
897{
Vitaliy Gusev80924e52009-12-25 07:17:43 +0000898 free_netdev(tun_sk(sk)->tun->dev);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800899}
900
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000901static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
902 struct msghdr *m, size_t total_len)
903{
904 struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
905 return tun_get_user(tun, m->msg_iov, total_len,
906 m->msg_flags & MSG_DONTWAIT);
907}
908
909static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
910 struct msghdr *m, size_t total_len,
911 int flags)
912{
913 struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
914 int ret;
915 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
916 return -EINVAL;
917 ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
918 flags & MSG_DONTWAIT);
919 if (ret > total_len) {
920 m->msg_flags |= MSG_TRUNC;
921 ret = flags & MSG_TRUNC ? ret : total_len;
922 }
923 return ret;
924}
925
926/* Ops structure to mimic raw sockets with tun */
927static const struct proto_ops tun_socket_ops = {
928 .sendmsg = tun_sendmsg,
929 .recvmsg = tun_recvmsg,
930};
931
Herbert Xu33dccbb2009-02-05 21:25:32 -0800932static struct proto tun_proto = {
933 .name = "tun",
934 .owner = THIS_MODULE,
935 .obj_size = sizeof(struct tun_sock),
936};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -0800937
David Woodhouse980c9e82009-05-09 22:54:21 -0700938static int tun_flags(struct tun_struct *tun)
939{
940 int flags = 0;
941
942 if (tun->flags & TUN_TUN_DEV)
943 flags |= IFF_TUN;
944 else
945 flags |= IFF_TAP;
946
947 if (tun->flags & TUN_NO_PI)
948 flags |= IFF_NO_PI;
949
950 if (tun->flags & TUN_ONE_QUEUE)
951 flags |= IFF_ONE_QUEUE;
952
953 if (tun->flags & TUN_VNET_HDR)
954 flags |= IFF_VNET_HDR;
955
956 return flags;
957}
958
959static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
960 char *buf)
961{
962 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
963 return sprintf(buf, "0x%x\n", tun_flags(tun));
964}
965
966static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
967 char *buf)
968{
969 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
970 return sprintf(buf, "%d\n", tun->owner);
971}
972
973static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
974 char *buf)
975{
976 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
977 return sprintf(buf, "%d\n", tun->group);
978}
979
980static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
981static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
982static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
983
Pavel Emelyanovd647a592008-04-16 00:41:16 -0700984static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800986 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct tun_struct *tun;
988 struct net_device *dev;
989 int err;
990
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +0000991 dev = __dev_get_by_name(net, ifr->ifr_name);
992 if (dev) {
Paul Moore2b980db2009-08-28 18:12:43 -0400993 const struct cred *cred = current_cred();
994
David Woodhousef85ba782009-04-27 03:23:54 -0700995 if (ifr->ifr_flags & IFF_TUN_EXCL)
996 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +0000997 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
998 tun = netdev_priv(dev);
999 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1000 tun = netdev_priv(dev);
1001 else
1002 return -EINVAL;
1003
Paul Moore2b980db2009-08-28 18:12:43 -04001004 if (((tun->owner != -1 && cred->euid != tun->owner) ||
1005 (tun->group != -1 && !in_egroup_p(tun->group))) &&
1006 !capable(CAP_NET_ADMIN))
1007 return -EPERM;
Linus Torvaldsd7e96602009-09-14 10:37:28 -07001008 err = security_tun_dev_attach(tun->socket.sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001009 if (err < 0)
1010 return err;
1011
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001012 err = tun_attach(tun, file);
1013 if (err < 0)
1014 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 else {
1017 char *name;
1018 unsigned long flags = 0;
1019
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001020 if (!capable(CAP_NET_ADMIN))
1021 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001022 err = security_tun_dev_create();
1023 if (err < 0)
1024 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Set dev type */
1027 if (ifr->ifr_flags & IFF_TUN) {
1028 /* TUN device */
1029 flags |= TUN_TUN_DEV;
1030 name = "tun%d";
1031 } else if (ifr->ifr_flags & IFF_TAP) {
1032 /* TAP device */
1033 flags |= TUN_TAP_DEV;
1034 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001035 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001036 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (*ifr->ifr_name)
1039 name = ifr->ifr_name;
1040
1041 dev = alloc_netdev(sizeof(struct tun_struct), name,
1042 tun_setup);
1043 if (!dev)
1044 return -ENOMEM;
1045
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001046 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001047 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 tun = netdev_priv(dev);
1050 tun->dev = dev;
1051 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001052 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001053 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Herbert Xu33dccbb2009-02-05 21:25:32 -08001055 err = -ENOMEM;
1056 sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
1057 if (!sk)
1058 goto err_free_dev;
1059
Eric Dumazet43815482010-04-29 11:01:49 +00001060 tun->socket.wq = &tun->wq;
1061 init_waitqueue_head(&tun->wq.wait);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001062 tun->socket.ops = &tun_socket_ops;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001063 sock_init_data(&tun->socket, sk);
1064 sk->sk_write_space = tun_sock_write_space;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001065 sk->sk_sndbuf = INT_MAX;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001066
Vitaliy Gusev80924e52009-12-25 07:17:43 +00001067 tun_sk(sk)->tun = tun;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001068
Paul Moore2b980db2009-08-28 18:12:43 -04001069 security_tun_dev_post_create(sk);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 tun_net_init(dev);
1072
1073 if (strchr(dev->name, '%')) {
1074 err = dev_alloc_name(dev, dev->name);
1075 if (err < 0)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001076 goto err_free_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
1079 err = register_netdevice(tun->dev);
1080 if (err < 0)
Herbert Xu9c3fea62009-04-18 14:15:52 +00001081 goto err_free_sk;
1082
David Woodhouse980c9e82009-05-09 22:54:21 -07001083 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1084 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1085 device_create_file(&tun->dev->dev, &dev_attr_group))
1086 printk(KERN_ERR "Failed to create tun sysfs files\n");
1087
Herbert Xu9c3fea62009-04-18 14:15:52 +00001088 sk->sk_destruct = tun_sock_destruct;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001089
1090 err = tun_attach(tun, file);
1091 if (err < 0)
Herbert Xu9c3fea62009-04-18 14:15:52 +00001092 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
1095 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
1096
1097 if (ifr->ifr_flags & IFF_NO_PI)
1098 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001099 else
1100 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1103 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001104 else
1105 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Rusty Russellf43798c2008-07-03 03:48:02 -07001107 if (ifr->ifr_flags & IFF_VNET_HDR)
1108 tun->flags |= TUN_VNET_HDR;
1109 else
1110 tun->flags &= ~TUN_VNET_HDR;
1111
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001112 /* Make sure persistent devices do not get stuck in
1113 * xoff state.
1114 */
1115 if (netif_running(tun->dev))
1116 netif_wake_queue(tun->dev);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 strcpy(ifr->ifr_name, tun->dev->name);
1119 return 0;
1120
Herbert Xu33dccbb2009-02-05 21:25:32 -08001121 err_free_sk:
1122 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 err_free_dev:
1124 free_netdev(dev);
1125 failed:
1126 return err;
1127}
1128
Herbert Xu876bfd42009-08-06 14:22:44 +00001129static int tun_get_iff(struct net *net, struct tun_struct *tun,
1130 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001131{
Mark McLoughline3b99552008-08-15 15:09:56 -07001132 DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
1133
1134 strcpy(ifr->ifr_name, tun->dev->name);
1135
David Woodhouse980c9e82009-05-09 22:54:21 -07001136 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001137
1138 return 0;
1139}
1140
Rusty Russell5228ddc2008-07-03 03:46:16 -07001141/* This is like a cut-down ethtool ops, except done via tun fd so no
1142 * privs required. */
1143static int set_offload(struct net_device *dev, unsigned long arg)
1144{
1145 unsigned int old_features, features;
1146
1147 old_features = dev->features;
1148 /* Unset features, set them as we chew on the arg. */
1149 features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001150 |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6
1151 |NETIF_F_UFO));
Rusty Russell5228ddc2008-07-03 03:46:16 -07001152
1153 if (arg & TUN_F_CSUM) {
1154 features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1155 arg &= ~TUN_F_CSUM;
1156
1157 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1158 if (arg & TUN_F_TSO_ECN) {
1159 features |= NETIF_F_TSO_ECN;
1160 arg &= ~TUN_F_TSO_ECN;
1161 }
1162 if (arg & TUN_F_TSO4)
1163 features |= NETIF_F_TSO;
1164 if (arg & TUN_F_TSO6)
1165 features |= NETIF_F_TSO6;
1166 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1167 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001168
1169 if (arg & TUN_F_UFO) {
1170 features |= NETIF_F_UFO;
1171 arg &= ~TUN_F_UFO;
1172 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001173 }
1174
1175 /* This gives the user a way to test for new features in future by
1176 * trying to set them. */
1177 if (arg)
1178 return -EINVAL;
1179
1180 dev->features = features;
1181 if (old_features != dev->features)
1182 netdev_features_change(dev);
1183
1184 return 0;
1185}
1186
Arnd Bergmann50857e22009-11-06 22:52:32 -08001187static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1188 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001190 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001191 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 void __user* argp = (void __user*)arg;
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001193 struct sock_fprog fprog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct ifreq ifr;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001195 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001196 int vnet_hdr_sz;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001197 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
Arnd Bergmann50857e22009-11-06 22:52:32 -08001200 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return -EFAULT;
1202
Eric W. Biederman631ab462009-01-20 11:00:40 +00001203 if (cmd == TUNGETFEATURES) {
1204 /* Currently this just means: "what IFF flags are valid?".
1205 * This is needed because we never checked for invalid flags on
1206 * TUNSETIFF. */
1207 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1208 IFF_VNET_HDR,
1209 (unsigned int __user*)argp);
1210 }
1211
Herbert Xu876bfd42009-08-06 14:22:44 +00001212 rtnl_lock();
1213
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001214 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1217
Herbert Xu876bfd42009-08-06 14:22:44 +00001218 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Herbert Xu876bfd42009-08-06 14:22:44 +00001220 if (ret)
1221 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Arnd Bergmann50857e22009-11-06 22:52:32 -08001223 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001224 ret = -EFAULT;
1225 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Herbert Xu876bfd42009-08-06 14:22:44 +00001228 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001230 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
1233
Eric W. Biederman631ab462009-01-20 11:00:40 +00001234 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001236 case TUNGETIFF:
Herbert Xu876bfd42009-08-06 14:22:44 +00001237 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001238 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001239 break;
Mark McLoughline3b99552008-08-15 15:09:56 -07001240
Arnd Bergmann50857e22009-11-06 22:52:32 -08001241 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001242 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001243 break;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case TUNSETNOCSUM:
1246 /* Disable/Enable checksum */
1247 if (arg)
1248 tun->flags |= TUN_NOCHECKSUM;
1249 else
1250 tun->flags &= ~TUN_NOCHECKSUM;
1251
1252 DBG(KERN_INFO "%s: checksum %s\n",
1253 tun->dev->name, arg ? "disabled" : "enabled");
1254 break;
1255
1256 case TUNSETPERSIST:
1257 /* Disable/Enable persist mode */
1258 if (arg)
1259 tun->flags |= TUN_PERSIST;
1260 else
1261 tun->flags &= ~TUN_PERSIST;
1262
1263 DBG(KERN_INFO "%s: persist %s\n",
Toyo Abec6e991d2007-12-24 21:29:35 -08001264 tun->dev->name, arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266
1267 case TUNSETOWNER:
1268 /* Set owner of the device */
1269 tun->owner = (uid_t) arg;
1270
1271 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
1272 break;
1273
Guido Guenther8c644622007-07-02 22:50:25 -07001274 case TUNSETGROUP:
1275 /* Set group of the device */
1276 tun->group= (gid_t) arg;
1277
1278 DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
1279 break;
1280
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001281 case TUNSETLINK:
1282 /* Only allow setting the type when the interface is down */
1283 if (tun->dev->flags & IFF_UP) {
1284 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1285 tun->dev->name);
David S. Miller48abfe02008-04-23 19:37:58 -07001286 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001287 } else {
1288 tun->dev->type = (int) arg;
1289 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001290 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001291 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001292 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294#ifdef TUN_DEBUG
1295 case TUNSETDEBUG:
1296 tun->debug = arg;
1297 break;
1298#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001299 case TUNSETOFFLOAD:
Rusty Russell5228ddc2008-07-03 03:46:16 -07001300 ret = set_offload(tun->dev, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001301 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001302
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001303 case TUNSETTXFILTER:
1304 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001305 ret = -EINVAL;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001306 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001307 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001308 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 case SIOCGIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001312 /* Get hw addres */
1313 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1314 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001315 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001316 ret = -EFAULT;
1317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001320 /* Set hw address */
Johannes Berge1749612008-10-27 15:59:26 -07001321 DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1322 tun->dev->name, ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001323
Kim B. Heino40102372008-02-29 12:26:21 -08001324 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001325 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001326
1327 case TUNGETSNDBUF:
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +00001328 sndbuf = tun->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001329 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1330 ret = -EFAULT;
1331 break;
1332
1333 case TUNSETSNDBUF:
1334 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1335 ret = -EFAULT;
1336 break;
1337 }
1338
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +00001339 tun->socket.sk->sk_sndbuf = sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001340 break;
1341
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001342 case TUNGETVNETHDRSZ:
1343 vnet_hdr_sz = tun->vnet_hdr_sz;
1344 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1345 ret = -EFAULT;
1346 break;
1347
1348 case TUNSETVNETHDRSZ:
1349 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1350 ret = -EFAULT;
1351 break;
1352 }
1353 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1354 ret = -EINVAL;
1355 break;
1356 }
1357
1358 tun->vnet_hdr_sz = vnet_hdr_sz;
1359 break;
1360
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001361 case TUNATTACHFILTER:
1362 /* Can be set only for TAPs */
1363 ret = -EINVAL;
1364 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1365 break;
1366 ret = -EFAULT;
1367 if (copy_from_user(&fprog, argp, sizeof(fprog)))
1368 break;
1369
1370 ret = sk_attach_filter(&fprog, tun->socket.sk);
1371 break;
1372
1373 case TUNDETACHFILTER:
1374 /* Can be set only for TAPs */
1375 ret = -EINVAL;
1376 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1377 break;
1378 ret = sk_detach_filter(tun->socket.sk);
1379 break;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001382 ret = -EINVAL;
1383 break;
Joe Perchesee289b62010-05-17 22:47:34 -07001384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Herbert Xu876bfd42009-08-06 14:22:44 +00001386unlock:
1387 rtnl_unlock();
1388 if (tun)
1389 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001390 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Arnd Bergmann50857e22009-11-06 22:52:32 -08001393static long tun_chr_ioctl(struct file *file,
1394 unsigned int cmd, unsigned long arg)
1395{
1396 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1397}
1398
1399#ifdef CONFIG_COMPAT
1400static long tun_chr_compat_ioctl(struct file *file,
1401 unsigned int cmd, unsigned long arg)
1402{
1403 switch (cmd) {
1404 case TUNSETIFF:
1405 case TUNGETIFF:
1406 case TUNSETTXFILTER:
1407 case TUNGETSNDBUF:
1408 case TUNSETSNDBUF:
1409 case SIOCGIFHWADDR:
1410 case SIOCSIFHWADDR:
1411 arg = (unsigned long)compat_ptr(arg);
1412 break;
1413 default:
1414 arg = (compat_ulong_t)arg;
1415 break;
1416 }
1417
1418 /*
1419 * compat_ifreq is shorter than ifreq, so we must not access beyond
1420 * the end of that structure. All fields that are used in this
1421 * driver are compatible though, we don't need to convert the
1422 * contents.
1423 */
1424 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1425}
1426#endif /* CONFIG_COMPAT */
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static int tun_chr_fasync(int fd, struct file *file, int on)
1429{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001430 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int ret;
1432
1433 if (!tun)
1434 return -EBADFD;
1435
1436 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
1437
1438 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001439 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001442 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 tun->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001446 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 tun->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001448 ret = 0;
1449out:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001450 tun_put(tun);
Jonathan Corbet9d319522008-06-19 15:50:37 -06001451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static int tun_chr_open(struct inode *inode, struct file * file)
1455{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001456 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 DBG1(KERN_INFO "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001459
1460 tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1461 if (!tfile)
1462 return -ENOMEM;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001463 atomic_set(&tfile->count, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001464 tfile->tun = NULL;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001465 tfile->net = get_net(current->nsproxy->net_ns);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001466 file->private_data = tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
1469
1470static int tun_chr_close(struct inode *inode, struct file *file)
1471{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001472 struct tun_file *tfile = file->private_data;
Eric W. Biedermanf0a4d0e2009-06-08 00:44:31 -07001473 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Eric W. Biedermanf0a4d0e2009-06-08 00:44:31 -07001475 tun = __tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001476 if (tun) {
Herbert Xud23e4362009-07-02 23:03:55 +00001477 struct net_device *dev = tun->dev;
1478
1479 DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric W. Biederman631ab462009-01-20 11:00:40 +00001481 __tun_detach(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001483 /* If desirable, unregister the netdevice. */
Herbert Xud23e4362009-07-02 23:03:55 +00001484 if (!(tun->flags & TUN_PERSIST)) {
1485 rtnl_lock();
1486 if (dev->reg_state == NETREG_REGISTERED)
1487 unregister_netdevice(dev);
1488 rtnl_unlock();
1489 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Herbert Xu9c3fea62009-04-18 14:15:52 +00001492 tun = tfile->tun;
1493 if (tun)
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +00001494 sock_put(tun->socket.sk);
Herbert Xu9c3fea62009-04-18 14:15:52 +00001495
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001496 put_net(tfile->net);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001497 kfree(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 return 0;
1500}
1501
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001502static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001503 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001505 .read = do_sync_read,
1506 .aio_read = tun_chr_aio_read,
1507 .write = do_sync_write,
1508 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08001510 .unlocked_ioctl = tun_chr_ioctl,
1511#ifdef CONFIG_COMPAT
1512 .compat_ioctl = tun_chr_compat_ioctl,
1513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 .open = tun_chr_open,
1515 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001516 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517};
1518
1519static struct miscdevice tun_miscdev = {
1520 .minor = TUN_MINOR,
1521 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02001522 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524};
1525
1526/* ethtool interface */
1527
1528static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1529{
1530 cmd->supported = 0;
1531 cmd->advertising = 0;
1532 cmd->speed = SPEED_10;
1533 cmd->duplex = DUPLEX_FULL;
1534 cmd->port = PORT_TP;
1535 cmd->phy_address = 0;
1536 cmd->transceiver = XCVR_INTERNAL;
1537 cmd->autoneg = AUTONEG_DISABLE;
1538 cmd->maxtxpkt = 0;
1539 cmd->maxrxpkt = 0;
1540 return 0;
1541}
1542
1543static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1544{
1545 struct tun_struct *tun = netdev_priv(dev);
1546
1547 strcpy(info->driver, DRV_NAME);
1548 strcpy(info->version, DRV_VERSION);
1549 strcpy(info->fw_version, "N/A");
1550
1551 switch (tun->flags & TUN_TYPE_MASK) {
1552 case TUN_TUN_DEV:
1553 strcpy(info->bus_info, "tun");
1554 break;
1555 case TUN_TAP_DEV:
1556 strcpy(info->bus_info, "tap");
1557 break;
1558 }
1559}
1560
1561static u32 tun_get_msglevel(struct net_device *dev)
1562{
1563#ifdef TUN_DEBUG
1564 struct tun_struct *tun = netdev_priv(dev);
1565 return tun->debug;
1566#else
1567 return -EOPNOTSUPP;
1568#endif
1569}
1570
1571static void tun_set_msglevel(struct net_device *dev, u32 value)
1572{
1573#ifdef TUN_DEBUG
1574 struct tun_struct *tun = netdev_priv(dev);
1575 tun->debug = value;
1576#endif
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static u32 tun_get_rx_csum(struct net_device *dev)
1580{
1581 struct tun_struct *tun = netdev_priv(dev);
1582 return (tun->flags & TUN_NOCHECKSUM) == 0;
1583}
1584
1585static int tun_set_rx_csum(struct net_device *dev, u32 data)
1586{
1587 struct tun_struct *tun = netdev_priv(dev);
1588 if (data)
1589 tun->flags &= ~TUN_NOCHECKSUM;
1590 else
1591 tun->flags |= TUN_NOCHECKSUM;
1592 return 0;
1593}
1594
Jeff Garzik7282d492006-09-13 14:30:00 -04001595static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 .get_settings = tun_get_settings,
1597 .get_drvinfo = tun_get_drvinfo,
1598 .get_msglevel = tun_get_msglevel,
1599 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00001600 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 .get_rx_csum = tun_get_rx_csum,
1602 .set_rx_csum = tun_set_rx_csum
1603};
1604
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static int __init tun_init(void)
1607{
1608 int ret = 0;
1609
1610 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1611 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
1612
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001613 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001614 if (ret) {
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001615 printk(KERN_ERR "tun: Can't register link_ops\n");
1616 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001620 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001622 goto err_misc;
1623 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001624 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001625err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001626 rtnl_link_unregister(&tun_link_ops);
1627err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return ret;
1629}
1630
1631static void tun_cleanup(void)
1632{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001633 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001634 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001637/* Get an underlying socket object from tun file. Returns error unless file is
1638 * attached to a device. The returned object works like a packet socket, it
1639 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1640 * holding a reference to the file for as long as the socket is in use. */
1641struct socket *tun_get_socket(struct file *file)
1642{
1643 struct tun_struct *tun;
1644 if (file->f_op != &tun_fops)
1645 return ERR_PTR(-EINVAL);
1646 tun = tun_get(file);
1647 if (!tun)
1648 return ERR_PTR(-EBADFD);
1649 tun_put(tun);
1650 return &tun->socket;
1651}
1652EXPORT_SYMBOL_GPL(tun_get_socket);
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654module_init(tun_init);
1655module_exit(tun_cleanup);
1656MODULE_DESCRIPTION(DRV_DESCRIPTION);
1657MODULE_AUTHOR(DRV_COPYRIGHT);
1658MODULE_LICENSE("GPL");
1659MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02001660MODULE_ALIAS("devname:net/tun");