blob: dfbf586597711a0c5cc1e6f6fa5b24c2b7208963 [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 Krasnyanskyf271b2c2008-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>
Arnd Bergmannfd3e05b2008-05-20 19:16:24 +020047#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/poll.h>
49#include <linux/fcntl.h>
50#include <linux/init.h>
51#include <linux/skbuff.h>
52#include <linux/netdevice.h>
53#include <linux/etherdevice.h>
54#include <linux/miscdevice.h>
55#include <linux/ethtool.h>
56#include <linux/rtnetlink.h>
57#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>
Eric W. Biederman881d9662007-09-17 11:56:21 -070064#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070065#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <asm/system.h>
68#include <asm/uaccess.h>
69
Rusty Russell14daa022008-04-12 18:48:58 -070070/* Uncomment to enable debugging */
71/* #define TUN_DEBUG 1 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef TUN_DEBUG
74static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070075
76#define DBG if(tun->debug)printk
77#define DBG1 if(debug==2)printk
78#else
79#define DBG( a... )
80#define DBG1( a... )
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -070083#define FLT_EXACT_COUNT 8
84struct tap_filter {
85 unsigned int count; /* Number of addrs. Zero means disabled */
86 u32 mask[2]; /* Mask of the hashed addrs */
87 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
88};
89
Eric W. Biederman631ab462009-01-20 11:00:40 +000090struct tun_file {
91 struct tun_struct *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +000092 struct net *net;
Eric W. Biederman631ab462009-01-20 11:00:40 +000093};
94
Rusty Russell14daa022008-04-12 18:48:58 -070095struct tun_struct {
Eric W. Biederman631ab462009-01-20 11:00:40 +000096 struct tun_file *tfile;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -070097 unsigned int flags;
Rusty Russell14daa022008-04-12 18:48:58 -070098 uid_t owner;
99 gid_t group;
100
101 wait_queue_head_t read_wait;
102 struct sk_buff_head readq;
103
104 struct net_device *dev;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700105 struct fasync_struct *fasync;
Rusty Russell14daa022008-04-12 18:48:58 -0700106
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700107 struct tap_filter txflt;
Rusty Russell14daa022008-04-12 18:48:58 -0700108
109#ifdef TUN_DEBUG
110 int debug;
111#endif
112};
113
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000114static int tun_attach(struct tun_struct *tun, struct file *file)
115{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000116 struct tun_file *tfile = file->private_data;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000117 const struct cred *cred = current_cred();
118
119 ASSERT_RTNL();
120
Eric W. Biederman631ab462009-01-20 11:00:40 +0000121 if (tfile->tun)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000122 return -EINVAL;
123
Eric W. Biederman631ab462009-01-20 11:00:40 +0000124 if (tun->tfile)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000125 return -EBUSY;
126
127 /* Check permissions */
128 if (((tun->owner != -1 && cred->euid != tun->owner) ||
129 (tun->group != -1 && cred->egid != tun->group)) &&
130 !capable(CAP_NET_ADMIN))
131 return -EPERM;
132
Eric W. Biederman631ab462009-01-20 11:00:40 +0000133 tfile->tun = tun;
134 tun->tfile = tfile;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000135
136 return 0;
137}
138
Eric W. Biederman631ab462009-01-20 11:00:40 +0000139static void __tun_detach(struct tun_struct *tun)
140{
141 struct tun_file *tfile = tun->tfile;
142
143 /* Detach from net device */
144 tfile->tun = NULL;
145 tun->tfile = NULL;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000146
147 /* Drop read queue */
148 skb_queue_purge(&tun->readq);
149}
150
151static struct tun_struct *__tun_get(struct tun_file *tfile)
152{
153 return tfile->tun;
154}
155
156static struct tun_struct *tun_get(struct file *file)
157{
158 return __tun_get(file->private_data);
159}
160
161static void tun_put(struct tun_struct *tun)
162{
163 /* Noop for now */
164}
165
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700166/* TAP filterting */
167static void addr_hash_set(u32 *mask, const u8 *addr)
168{
169 int n = ether_crc(ETH_ALEN, addr) >> 26;
170 mask[n >> 5] |= (1 << (n & 31));
171}
172
173static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
174{
175 int n = ether_crc(ETH_ALEN, addr) >> 26;
176 return mask[n >> 5] & (1 << (n & 31));
177}
178
179static int update_filter(struct tap_filter *filter, void __user *arg)
180{
181 struct { u8 u[ETH_ALEN]; } *addr;
182 struct tun_filter uf;
183 int err, alen, n, nexact;
184
185 if (copy_from_user(&uf, arg, sizeof(uf)))
186 return -EFAULT;
187
188 if (!uf.count) {
189 /* Disabled */
190 filter->count = 0;
191 return 0;
192 }
193
194 alen = ETH_ALEN * uf.count;
195 addr = kmalloc(alen, GFP_KERNEL);
196 if (!addr)
197 return -ENOMEM;
198
199 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
200 err = -EFAULT;
201 goto done;
202 }
203
204 /* The filter is updated without holding any locks. Which is
205 * perfectly safe. We disable it first and in the worst
206 * case we'll accept a few undesired packets. */
207 filter->count = 0;
208 wmb();
209
210 /* Use first set of addresses as an exact filter */
211 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
212 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
213
214 nexact = n;
215
216 /* The rest is hashed */
217 memset(filter->mask, 0, sizeof(filter->mask));
218 for (; n < uf.count; n++)
219 addr_hash_set(filter->mask, addr[n].u);
220
221 /* For ALLMULTI just set the mask to all ones.
222 * This overrides the mask populated above. */
223 if ((uf.flags & TUN_FLT_ALLMULTI))
224 memset(filter->mask, ~0, sizeof(filter->mask));
225
226 /* Now enable the filter */
227 wmb();
228 filter->count = nexact;
229
230 /* Return the number of exact filters */
231 err = nexact;
232
233done:
234 kfree(addr);
235 return err;
236}
237
238/* Returns: 0 - drop, !=0 - accept */
239static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
240{
241 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
242 * at this point. */
243 struct ethhdr *eh = (struct ethhdr *) skb->data;
244 int i;
245
246 /* Exact match */
247 for (i = 0; i < filter->count; i++)
248 if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
249 return 1;
250
251 /* Inexact match (multicast only) */
252 if (is_multicast_ether_addr(eh->h_dest))
253 return addr_hash_test(filter->mask, eh->h_dest);
254
255 return 0;
256}
257
258/*
259 * Checks whether the packet is accepted or not.
260 * Returns: 0 - drop, !=0 - accept
261 */
262static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
263{
264 if (!filter->count)
265 return 1;
266
267 return run_filter(filter, skb);
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/* Network device part of the driver */
271
Jeff Garzik7282d492006-09-13 14:30:00 -0400272static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/* Net device open. */
275static int tun_net_open(struct net_device *dev)
276{
277 netif_start_queue(dev);
278 return 0;
279}
280
281/* Net device close. */
282static int tun_net_close(struct net_device *dev)
283{
284 netif_stop_queue(dev);
285 return 0;
286}
287
288/* Net device start xmit */
289static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
290{
291 struct tun_struct *tun = netdev_priv(dev);
292
293 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
294
295 /* Drop packet if interface is not attached */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000296 if (!tun->tfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto drop;
298
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700299 /* Drop if the filter does not like it.
300 * This is a noop if the filter is disabled.
301 * Filter can be enabled only for the TAP devices. */
302 if (!check_filter(&tun->txflt, skb))
303 goto drop;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
306 if (!(tun->flags & TUN_ONE_QUEUE)) {
307 /* Normal queueing mode. */
308 /* Packet scheduler handles dropping of further packets. */
309 netif_stop_queue(dev);
310
311 /* We won't see all dropped packets individually, so overrun
312 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700313 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 } else {
315 /* Single queue mode.
316 * Driver handles dropping of all packets itself. */
317 goto drop;
318 }
319 }
320
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700321 /* Enqueue packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 skb_queue_tail(&tun->readq, skb);
323 dev->trans_start = jiffies;
324
325 /* Notify and wake up reader process */
326 if (tun->flags & TUN_FASYNC)
327 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
328 wake_up_interruptible(&tun->read_wait);
329 return 0;
330
331drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700332 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 kfree_skb(skb);
334 return 0;
335}
336
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700337static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700339 /*
340 * This callback is supposed to deal with mc filter in
341 * _rx_ path and has nothing to do with the _tx_ path.
342 * In rx path we always accept everything userspace gives us.
343 */
344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Ed Swierk4885a502007-09-16 12:21:38 -0700347#define MIN_MTU 68
348#define MAX_MTU 65535
349
350static int
351tun_net_change_mtu(struct net_device *dev, int new_mtu)
352{
353 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
354 return -EINVAL;
355 dev->mtu = new_mtu;
356 return 0;
357}
358
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800359static const struct net_device_ops tun_netdev_ops = {
360 .ndo_open = tun_net_open,
361 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800362 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800363 .ndo_change_mtu = tun_net_change_mtu,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800364};
365
366static const struct net_device_ops tap_netdev_ops = {
367 .ndo_open = tun_net_open,
368 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800369 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800370 .ndo_change_mtu = tun_net_change_mtu,
371 .ndo_set_multicast_list = tun_net_mclist,
372 .ndo_set_mac_address = eth_mac_addr,
373 .ndo_validate_addr = eth_validate_addr,
374};
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* Initialize net device. */
377static void tun_net_init(struct net_device *dev)
378{
379 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 switch (tun->flags & TUN_TYPE_MASK) {
382 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800383 dev->netdev_ops = &tun_netdev_ops;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Point-to-Point TUN Device */
386 dev->hard_header_len = 0;
387 dev->addr_len = 0;
388 dev->mtu = 1500;
389
390 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400391 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
393 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
394 break;
395
396 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800397 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700399 ether_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700401 random_ether_addr(dev->dev_addr);
Brian Braunstein36226a82007-04-26 01:00:55 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
404 break;
405 }
406}
407
408/* Character device part */
409
410/* Poll */
411static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400412{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000413 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned int mask = POLLOUT | POLLWRNORM;
415
416 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000417 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
420
421 poll_wait(file, &tun->read_wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400422
David S. Millerb03efcf2005-07-08 14:57:23 -0700423 if (!skb_queue_empty(&tun->readq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 mask |= POLLIN | POLLRDNORM;
425
Eric W. Biederman631ab462009-01-20 11:00:40 +0000426 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return mask;
428}
429
Rusty Russellf42157c2008-08-15 15:15:10 -0700430/* prepad is the amount to reserve at front. len is length after that.
431 * linear is a hint as to how much to copy (usually headers). */
432static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
433 gfp_t gfp)
434{
435 struct sk_buff *skb;
436 unsigned int i;
437
438 skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
439 if (skb) {
440 skb_reserve(skb, prepad);
441 skb_put(skb, len);
442 return skb;
443 }
444
445 /* Under a page? Don't bother with paged skb. */
446 if (prepad + len < PAGE_SIZE)
447 return NULL;
448
449 /* Start with a normal skb, and add pages. */
450 skb = alloc_skb(prepad + linear, gfp);
451 if (!skb)
452 return NULL;
453
454 skb_reserve(skb, prepad);
455 skb_put(skb, linear);
456
457 len -= linear;
458
459 for (i = 0; i < MAX_SKB_FRAGS; i++) {
460 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
461
462 f->page = alloc_page(gfp|__GFP_ZERO);
463 if (!f->page)
464 break;
465
466 f->page_offset = 0;
467 f->size = PAGE_SIZE;
468
469 skb->data_len += PAGE_SIZE;
470 skb->len += PAGE_SIZE;
471 skb->truesize += PAGE_SIZE;
472 skb_shinfo(skb)->nr_frags++;
473
474 if (len < PAGE_SIZE) {
475 len = 0;
476 break;
477 }
478 len -= PAGE_SIZE;
479 }
480
481 /* Too large, or alloc fail? */
482 if (unlikely(len)) {
483 kfree_skb(skb);
484 skb = NULL;
485 }
486
487 return skb;
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/* Get packet from user space buffer */
491static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
492{
493 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
494 struct sk_buff *skb;
495 size_t len = count, align = 0;
Rusty Russellf43798c2008-07-03 03:48:02 -0700496 struct virtio_net_hdr gso = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (!(tun->flags & TUN_NO_PI)) {
499 if ((len -= sizeof(pi)) > count)
500 return -EINVAL;
501
502 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
503 return -EFAULT;
504 }
505
Rusty Russellf43798c2008-07-03 03:48:02 -0700506 if (tun->flags & TUN_VNET_HDR) {
507 if ((len -= sizeof(gso)) > count)
508 return -EINVAL;
509
510 if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
511 return -EFAULT;
512
513 if (gso.hdr_len > len)
514 return -EINVAL;
515 }
516
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700517 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 align = NET_IP_ALIGN;
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700519 if (unlikely(len < ETH_HLEN))
520 return -EINVAL;
521 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400522
Rusty Russellf42157c2008-08-15 15:15:10 -0700523 if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700524 tun->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -ENOMEM;
526 }
527
Rusty Russellf42157c2008-08-15 15:15:10 -0700528 if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700529 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800530 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Rusty Russellf43798c2008-07-03 03:48:02 -0700534 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
535 if (!skb_partial_csum_set(skb, gso.csum_start,
536 gso.csum_offset)) {
537 tun->dev->stats.rx_frame_errors++;
538 kfree_skb(skb);
539 return -EINVAL;
540 }
541 } else if (tun->flags & TUN_NOCHECKSUM)
542 skb->ip_summed = CHECKSUM_UNNECESSARY;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (tun->flags & TUN_TYPE_MASK) {
545 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700546 if (tun->flags & TUN_NO_PI) {
547 switch (skb->data[0] & 0xf0) {
548 case 0x40:
549 pi.proto = htons(ETH_P_IP);
550 break;
551 case 0x60:
552 pi.proto = htons(ETH_P_IPV6);
553 break;
554 default:
555 tun->dev->stats.rx_dropped++;
556 kfree_skb(skb);
557 return -EINVAL;
558 }
559 }
560
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700561 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700563 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case TUN_TAP_DEV:
566 skb->protocol = eth_type_trans(skb, tun->dev);
567 break;
568 };
569
Rusty Russellf43798c2008-07-03 03:48:02 -0700570 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
571 pr_debug("GSO!\n");
572 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
573 case VIRTIO_NET_HDR_GSO_TCPV4:
574 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
575 break;
576 case VIRTIO_NET_HDR_GSO_TCPV6:
577 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
578 break;
579 default:
580 tun->dev->stats.rx_frame_errors++;
581 kfree_skb(skb);
582 return -EINVAL;
583 }
584
585 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
586 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
587
588 skb_shinfo(skb)->gso_size = gso.gso_size;
589 if (skb_shinfo(skb)->gso_size == 0) {
590 tun->dev->stats.rx_frame_errors++;
591 kfree_skb(skb);
592 return -EINVAL;
593 }
594
595 /* Header must be checked, and gso_segs computed. */
596 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
597 skb_shinfo(skb)->gso_segs = 0;
598 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400601
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700602 tun->dev->stats.rx_packets++;
603 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 return count;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400606}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700608static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
609 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000611 struct tun_struct *tun = tun_get(iocb->ki_filp);
612 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (!tun)
615 return -EBADFD;
616
617 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
618
Eric W. Biederman631ab462009-01-20 11:00:40 +0000619 result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
620
621 tun_put(tun);
622 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/* Put packet to the user space buffer */
626static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
627 struct sk_buff *skb,
628 struct iovec *iv, int len)
629{
630 struct tun_pi pi = { 0, skb->protocol };
631 ssize_t total = 0;
632
633 if (!(tun->flags & TUN_NO_PI)) {
634 if ((len -= sizeof(pi)) < 0)
635 return -EINVAL;
636
637 if (len < skb->len) {
638 /* Packet will be striped */
639 pi.flags |= TUN_PKT_STRIP;
640 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
643 return -EFAULT;
644 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Rusty Russellf43798c2008-07-03 03:48:02 -0700647 if (tun->flags & TUN_VNET_HDR) {
648 struct virtio_net_hdr gso = { 0 }; /* no info leak */
649 if ((len -= sizeof(gso)) < 0)
650 return -EINVAL;
651
652 if (skb_is_gso(skb)) {
653 struct skb_shared_info *sinfo = skb_shinfo(skb);
654
655 /* This is a hint as to how much should be linear. */
656 gso.hdr_len = skb_headlen(skb);
657 gso.gso_size = sinfo->gso_size;
658 if (sinfo->gso_type & SKB_GSO_TCPV4)
659 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
660 else if (sinfo->gso_type & SKB_GSO_TCPV6)
661 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
662 else
663 BUG();
664 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
665 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
666 } else
667 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
668
669 if (skb->ip_summed == CHECKSUM_PARTIAL) {
670 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
671 gso.csum_start = skb->csum_start - skb_headroom(skb);
672 gso.csum_offset = skb->csum_offset;
673 } /* else everything is zero */
674
675 if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
676 return -EFAULT;
677 total += sizeof(gso);
678 }
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 len = min_t(int, skb->len, len);
681
682 skb_copy_datagram_iovec(skb, 0, iv, len);
683 total += len;
684
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700685 tun->dev->stats.tx_packets++;
686 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 return total;
689}
690
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700691static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
692 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700694 struct file *file = iocb->ki_filp;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000695 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 DECLARE_WAITQUEUE(wait, current);
697 struct sk_buff *skb;
698 ssize_t len, ret = 0;
699
700 if (!tun)
701 return -EBADFD;
702
703 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
704
Akinobu Mita52427c92007-11-19 22:46:51 -0800705 len = iov_length(iv, count);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000706 if (len < 0) {
707 ret = -EINVAL;
708 goto out;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 add_wait_queue(&tun->read_wait, &wait);
712 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 current->state = TASK_INTERRUPTIBLE;
714
715 /* Read frames from the queue */
716 if (!(skb=skb_dequeue(&tun->readq))) {
717 if (file->f_flags & O_NONBLOCK) {
718 ret = -EAGAIN;
719 break;
720 }
721 if (signal_pending(current)) {
722 ret = -ERESTARTSYS;
723 break;
724 }
725
726 /* Nothing to read, let's sleep */
727 schedule();
728 continue;
729 }
730 netif_wake_queue(tun->dev);
731
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700732 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
733 kfree_skb(skb);
734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
737 current->state = TASK_RUNNING;
738 remove_wait_queue(&tun->read_wait, &wait);
739
Eric W. Biederman631ab462009-01-20 11:00:40 +0000740out:
741 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return ret;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static void tun_setup(struct net_device *dev)
746{
747 struct tun_struct *tun = netdev_priv(dev);
748
749 skb_queue_head_init(&tun->readq);
750 init_waitqueue_head(&tun->read_wait);
751
752 tun->owner = -1;
Guido Guenther8c644622007-07-02 22:50:25 -0700753 tun->group = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 dev->ethtool_ops = &tun_ethtool_ops;
756 dev->destructor = free_netdev;
Pavel Emelyanovfc54c652008-04-16 00:41:53 -0700757 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Pavel Emelyanovd647a592008-04-16 00:41:16 -0700760static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct tun_struct *tun;
763 struct net_device *dev;
764 int err;
765
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +0000766 dev = __dev_get_by_name(net, ifr->ifr_name);
767 if (dev) {
768 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
769 tun = netdev_priv(dev);
770 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
771 tun = netdev_priv(dev);
772 else
773 return -EINVAL;
774
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000775 err = tun_attach(tun, file);
776 if (err < 0)
777 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 else {
780 char *name;
781 unsigned long flags = 0;
782
783 err = -EINVAL;
784
David Woodhouseca6bb5d2006-06-22 16:07:52 -0700785 if (!capable(CAP_NET_ADMIN))
786 return -EPERM;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Set dev type */
789 if (ifr->ifr_flags & IFF_TUN) {
790 /* TUN device */
791 flags |= TUN_TUN_DEV;
792 name = "tun%d";
793 } else if (ifr->ifr_flags & IFF_TAP) {
794 /* TAP device */
795 flags |= TUN_TAP_DEV;
796 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto failed;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (*ifr->ifr_name)
801 name = ifr->ifr_name;
802
803 dev = alloc_netdev(sizeof(struct tun_struct), name,
804 tun_setup);
805 if (!dev)
806 return -ENOMEM;
807
Pavel Emelyanovfc54c652008-04-16 00:41:53 -0700808 dev_net_set(dev, net);
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 tun = netdev_priv(dev);
811 tun->dev = dev;
812 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700813 tun->txflt.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 tun_net_init(dev);
816
817 if (strchr(dev->name, '%')) {
818 err = dev_alloc_name(dev, dev->name);
819 if (err < 0)
820 goto err_free_dev;
821 }
822
823 err = register_netdevice(tun->dev);
824 if (err < 0)
825 goto err_free_dev;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000826
827 err = tun_attach(tun, file);
828 if (err < 0)
829 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
833
834 if (ifr->ifr_flags & IFF_NO_PI)
835 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800836 else
837 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (ifr->ifr_flags & IFF_ONE_QUEUE)
840 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800841 else
842 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Rusty Russellf43798c2008-07-03 03:48:02 -0700844 if (ifr->ifr_flags & IFF_VNET_HDR)
845 tun->flags |= TUN_VNET_HDR;
846 else
847 tun->flags &= ~TUN_VNET_HDR;
848
Max Krasnyanskye35259a2008-07-10 16:59:11 -0700849 /* Make sure persistent devices do not get stuck in
850 * xoff state.
851 */
852 if (netif_running(tun->dev))
853 netif_wake_queue(tun->dev);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 strcpy(ifr->ifr_name, tun->dev->name);
856 return 0;
857
858 err_free_dev:
859 free_netdev(dev);
860 failed:
861 return err;
862}
863
Mark McLoughline3b99552008-08-15 15:09:56 -0700864static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
865{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000866 struct tun_struct *tun = tun_get(file);
Mark McLoughline3b99552008-08-15 15:09:56 -0700867
868 if (!tun)
869 return -EBADFD;
870
871 DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
872
873 strcpy(ifr->ifr_name, tun->dev->name);
874
875 ifr->ifr_flags = 0;
876
877 if (ifr->ifr_flags & TUN_TUN_DEV)
878 ifr->ifr_flags |= IFF_TUN;
879 else
880 ifr->ifr_flags |= IFF_TAP;
881
882 if (tun->flags & TUN_NO_PI)
883 ifr->ifr_flags |= IFF_NO_PI;
884
885 if (tun->flags & TUN_ONE_QUEUE)
886 ifr->ifr_flags |= IFF_ONE_QUEUE;
887
888 if (tun->flags & TUN_VNET_HDR)
889 ifr->ifr_flags |= IFF_VNET_HDR;
890
Eric W. Biederman631ab462009-01-20 11:00:40 +0000891 tun_put(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -0700892 return 0;
893}
894
Rusty Russell5228ddc2008-07-03 03:46:16 -0700895/* This is like a cut-down ethtool ops, except done via tun fd so no
896 * privs required. */
897static int set_offload(struct net_device *dev, unsigned long arg)
898{
899 unsigned int old_features, features;
900
901 old_features = dev->features;
902 /* Unset features, set them as we chew on the arg. */
903 features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
904 |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
905
906 if (arg & TUN_F_CSUM) {
907 features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
908 arg &= ~TUN_F_CSUM;
909
910 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
911 if (arg & TUN_F_TSO_ECN) {
912 features |= NETIF_F_TSO_ECN;
913 arg &= ~TUN_F_TSO_ECN;
914 }
915 if (arg & TUN_F_TSO4)
916 features |= NETIF_F_TSO;
917 if (arg & TUN_F_TSO6)
918 features |= NETIF_F_TSO6;
919 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
920 }
921 }
922
923 /* This gives the user a way to test for new features in future by
924 * trying to set them. */
925 if (arg)
926 return -EINVAL;
927
928 dev->features = features;
929 if (old_features != dev->features)
930 netdev_features_change(dev);
931
932 return 0;
933}
934
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400935static int tun_chr_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned int cmd, unsigned long arg)
937{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000938 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000939 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 void __user* argp = (void __user*)arg;
941 struct ifreq ifr;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700942 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
945 if (copy_from_user(&ifr, argp, sizeof ifr))
946 return -EFAULT;
947
Eric W. Biederman631ab462009-01-20 11:00:40 +0000948 if (cmd == TUNGETFEATURES) {
949 /* Currently this just means: "what IFF flags are valid?".
950 * This is needed because we never checked for invalid flags on
951 * TUNSETIFF. */
952 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
953 IFF_VNET_HDR,
954 (unsigned int __user*)argp);
955 }
956
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000957 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (cmd == TUNSETIFF && !tun) {
959 int err;
960
961 ifr.ifr_name[IFNAMSIZ-1] = '\0';
962
963 rtnl_lock();
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000964 err = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 rtnl_unlock();
966
967 if (err)
968 return err;
969
970 if (copy_to_user(argp, &ifr, sizeof(ifr)))
971 return -EFAULT;
972 return 0;
973 }
974
Rusty Russell07240fd2008-07-03 03:45:32 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!tun)
977 return -EBADFD;
978
979 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
980
Eric W. Biederman631ab462009-01-20 11:00:40 +0000981 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -0700983 case TUNGETIFF:
984 ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
985 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +0000986 break;
Mark McLoughline3b99552008-08-15 15:09:56 -0700987
988 if (copy_to_user(argp, &ifr, sizeof(ifr)))
Eric W. Biederman631ab462009-01-20 11:00:40 +0000989 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -0700990 break;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 case TUNSETNOCSUM:
993 /* Disable/Enable checksum */
994 if (arg)
995 tun->flags |= TUN_NOCHECKSUM;
996 else
997 tun->flags &= ~TUN_NOCHECKSUM;
998
999 DBG(KERN_INFO "%s: checksum %s\n",
1000 tun->dev->name, arg ? "disabled" : "enabled");
1001 break;
1002
1003 case TUNSETPERSIST:
1004 /* Disable/Enable persist mode */
1005 if (arg)
1006 tun->flags |= TUN_PERSIST;
1007 else
1008 tun->flags &= ~TUN_PERSIST;
1009
1010 DBG(KERN_INFO "%s: persist %s\n",
Toyo Abec6e991d2007-12-24 21:29:35 -08001011 tun->dev->name, arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013
1014 case TUNSETOWNER:
1015 /* Set owner of the device */
1016 tun->owner = (uid_t) arg;
1017
1018 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
1019 break;
1020
Guido Guenther8c644622007-07-02 22:50:25 -07001021 case TUNSETGROUP:
1022 /* Set group of the device */
1023 tun->group= (gid_t) arg;
1024
1025 DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
1026 break;
1027
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001028 case TUNSETLINK:
1029 /* Only allow setting the type when the interface is down */
David S. Miller48abfe02008-04-23 19:37:58 -07001030 rtnl_lock();
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001031 if (tun->dev->flags & IFF_UP) {
1032 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1033 tun->dev->name);
David S. Miller48abfe02008-04-23 19:37:58 -07001034 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001035 } else {
1036 tun->dev->type = (int) arg;
1037 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001038 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001039 }
David S. Miller48abfe02008-04-23 19:37:58 -07001040 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +00001041 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#ifdef TUN_DEBUG
1044 case TUNSETDEBUG:
1045 tun->debug = arg;
1046 break;
1047#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001048 case TUNSETOFFLOAD:
Rusty Russell5228ddc2008-07-03 03:46:16 -07001049 rtnl_lock();
1050 ret = set_offload(tun->dev, arg);
1051 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +00001052 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001053
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001054 case TUNSETTXFILTER:
1055 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001056 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001057 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001058 break;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001059 rtnl_lock();
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001060 ret = update_filter(&tun->txflt, (void __user *)arg);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001061 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +00001062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 case SIOCGIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001065 /* Get hw addres */
1066 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1067 ifr.ifr_hwaddr.sa_family = tun->dev->type;
1068 if (copy_to_user(argp, &ifr, sizeof ifr))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001069 ret = -EFAULT;
1070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001073 /* Set hw address */
Johannes Berge1749612008-10-27 15:59:26 -07001074 DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1075 tun->dev->name, ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001076
1077 rtnl_lock();
1078 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1079 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +00001080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001082 ret = -EINVAL;
1083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 };
1085
Eric W. Biederman631ab462009-01-20 11:00:40 +00001086 tun_put(tun);
1087 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static int tun_chr_fasync(int fd, struct file *file, int on)
1091{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001092 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int ret;
1094
1095 if (!tun)
1096 return -EBADFD;
1097
1098 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
1099
Jonathan Corbet9d319522008-06-19 15:50:37 -06001100 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001102 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001105 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 tun->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001109 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 tun->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001111 ret = 0;
1112out:
1113 unlock_kernel();
Eric W. Biederman631ab462009-01-20 11:00:40 +00001114 tun_put(tun);
Jonathan Corbet9d319522008-06-19 15:50:37 -06001115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static int tun_chr_open(struct inode *inode, struct file * file)
1119{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001120 struct tun_file *tfile;
Arnd Bergmannfd3e05b2008-05-20 19:16:24 +02001121 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 DBG1(KERN_INFO "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001123
1124 tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1125 if (!tfile)
1126 return -ENOMEM;
1127 tfile->tun = NULL;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001128 tfile->net = get_net(current->nsproxy->net_ns);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001129 file->private_data = tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131}
1132
1133static int tun_chr_close(struct inode *inode, struct file *file)
1134{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001135 struct tun_file *tfile = file->private_data;
1136 struct tun_struct *tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Eric W. Biederman631ab462009-01-20 11:00:40 +00001139 if (tun) {
1140 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Eric W. Biederman631ab462009-01-20 11:00:40 +00001142 rtnl_lock();
1143 __tun_detach(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Eric W. Biederman631ab462009-01-20 11:00:40 +00001145 /* If desireable, unregister the netdevice. */
1146 if (!(tun->flags & TUN_PERSIST))
1147 unregister_netdevice(tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Eric W. Biederman631ab462009-01-20 11:00:40 +00001149 rtnl_unlock();
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001152 put_net(tfile->net);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001153 kfree(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 return 0;
1156}
1157
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001158static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001159 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001161 .read = do_sync_read,
1162 .aio_read = tun_chr_aio_read,
1163 .write = do_sync_write,
1164 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 .poll = tun_chr_poll,
1166 .ioctl = tun_chr_ioctl,
1167 .open = tun_chr_open,
1168 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001169 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
1172static struct miscdevice tun_miscdev = {
1173 .minor = TUN_MINOR,
1174 .name = "tun",
1175 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176};
1177
1178/* ethtool interface */
1179
1180static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1181{
1182 cmd->supported = 0;
1183 cmd->advertising = 0;
1184 cmd->speed = SPEED_10;
1185 cmd->duplex = DUPLEX_FULL;
1186 cmd->port = PORT_TP;
1187 cmd->phy_address = 0;
1188 cmd->transceiver = XCVR_INTERNAL;
1189 cmd->autoneg = AUTONEG_DISABLE;
1190 cmd->maxtxpkt = 0;
1191 cmd->maxrxpkt = 0;
1192 return 0;
1193}
1194
1195static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1196{
1197 struct tun_struct *tun = netdev_priv(dev);
1198
1199 strcpy(info->driver, DRV_NAME);
1200 strcpy(info->version, DRV_VERSION);
1201 strcpy(info->fw_version, "N/A");
1202
1203 switch (tun->flags & TUN_TYPE_MASK) {
1204 case TUN_TUN_DEV:
1205 strcpy(info->bus_info, "tun");
1206 break;
1207 case TUN_TAP_DEV:
1208 strcpy(info->bus_info, "tap");
1209 break;
1210 }
1211}
1212
1213static u32 tun_get_msglevel(struct net_device *dev)
1214{
1215#ifdef TUN_DEBUG
1216 struct tun_struct *tun = netdev_priv(dev);
1217 return tun->debug;
1218#else
1219 return -EOPNOTSUPP;
1220#endif
1221}
1222
1223static void tun_set_msglevel(struct net_device *dev, u32 value)
1224{
1225#ifdef TUN_DEBUG
1226 struct tun_struct *tun = netdev_priv(dev);
1227 tun->debug = value;
1228#endif
1229}
1230
1231static u32 tun_get_link(struct net_device *dev)
1232{
1233 struct tun_struct *tun = netdev_priv(dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001234 return !!tun->tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static u32 tun_get_rx_csum(struct net_device *dev)
1238{
1239 struct tun_struct *tun = netdev_priv(dev);
1240 return (tun->flags & TUN_NOCHECKSUM) == 0;
1241}
1242
1243static int tun_set_rx_csum(struct net_device *dev, u32 data)
1244{
1245 struct tun_struct *tun = netdev_priv(dev);
1246 if (data)
1247 tun->flags &= ~TUN_NOCHECKSUM;
1248 else
1249 tun->flags |= TUN_NOCHECKSUM;
1250 return 0;
1251}
1252
Jeff Garzik7282d492006-09-13 14:30:00 -04001253static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 .get_settings = tun_get_settings,
1255 .get_drvinfo = tun_get_drvinfo,
1256 .get_msglevel = tun_get_msglevel,
1257 .set_msglevel = tun_set_msglevel,
1258 .get_link = tun_get_link,
1259 .get_rx_csum = tun_get_rx_csum,
1260 .set_rx_csum = tun_set_rx_csum
1261};
1262
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001263static int tun_init_net(struct net *net)
1264{
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001265 return 0;
1266}
1267
1268static void tun_exit_net(struct net *net)
1269{
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001270 struct net_device *dev, *next;
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001271
1272 rtnl_lock();
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001273 for_each_netdev_safe(net, dev, next) {
1274 if (dev->ethtool_ops != &tun_ethtool_ops)
1275 continue;
1276 DBG(KERN_INFO "%s cleaned up\n", dev->name);
1277 unregister_netdevice(dev);
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001278 }
1279 rtnl_unlock();
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001280}
1281
1282static struct pernet_operations tun_net_ops = {
1283 .init = tun_init_net,
1284 .exit = tun_exit_net,
1285};
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287static int __init tun_init(void)
1288{
1289 int ret = 0;
1290
1291 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1292 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
1293
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001294 ret = register_pernet_device(&tun_net_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001295 if (ret) {
1296 printk(KERN_ERR "tun: Can't register pernet ops\n");
1297 goto err_pernet;
1298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001301 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001303 goto err_misc;
1304 }
1305 return 0;
1306
1307err_misc:
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001308 unregister_pernet_device(&tun_net_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001309err_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return ret;
1311}
1312
1313static void tun_cleanup(void)
1314{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001315 misc_deregister(&tun_miscdev);
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001316 unregister_pernet_device(&tun_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319module_init(tun_init);
1320module_exit(tun_cleanup);
1321MODULE_DESCRIPTION(DRV_DESCRIPTION);
1322MODULE_AUTHOR(DRV_COPYRIGHT);
1323MODULE_LICENSE("GPL");
1324MODULE_ALIAS_MISCDEV(TUN_MINOR);