blob: d3a665d2f7b837fd87033f4dfdb454306d20dbb2 [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;
92};
93
Rusty Russell14daa022008-04-12 18:48:58 -070094struct tun_struct {
Eric W. Biederman631ab462009-01-20 11:00:40 +000095 struct tun_file *tfile;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -070096 unsigned int flags;
Rusty Russell14daa022008-04-12 18:48:58 -070097 uid_t owner;
98 gid_t group;
99
100 wait_queue_head_t read_wait;
101 struct sk_buff_head readq;
102
103 struct net_device *dev;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700104 struct fasync_struct *fasync;
Rusty Russell14daa022008-04-12 18:48:58 -0700105
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700106 struct tap_filter txflt;
Rusty Russell14daa022008-04-12 18:48:58 -0700107
108#ifdef TUN_DEBUG
109 int debug;
110#endif
111};
112
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000113static int tun_attach(struct tun_struct *tun, struct file *file)
114{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000115 struct tun_file *tfile = file->private_data;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000116 const struct cred *cred = current_cred();
117
118 ASSERT_RTNL();
119
Eric W. Biederman631ab462009-01-20 11:00:40 +0000120 if (tfile->tun)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000121 return -EINVAL;
122
Eric W. Biederman631ab462009-01-20 11:00:40 +0000123 if (tun->tfile)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000124 return -EBUSY;
125
126 /* Check permissions */
127 if (((tun->owner != -1 && cred->euid != tun->owner) ||
128 (tun->group != -1 && cred->egid != tun->group)) &&
129 !capable(CAP_NET_ADMIN))
130 return -EPERM;
131
Eric W. Biederman631ab462009-01-20 11:00:40 +0000132 tfile->tun = tun;
133 tun->tfile = tfile;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000134 get_net(dev_net(tun->dev));
135
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;
146 put_net(dev_net(tun->dev));
147
148 /* Drop read queue */
149 skb_queue_purge(&tun->readq);
150}
151
152static struct tun_struct *__tun_get(struct tun_file *tfile)
153{
154 return tfile->tun;
155}
156
157static struct tun_struct *tun_get(struct file *file)
158{
159 return __tun_get(file->private_data);
160}
161
162static void tun_put(struct tun_struct *tun)
163{
164 /* Noop for now */
165}
166
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700167/* TAP filterting */
168static void addr_hash_set(u32 *mask, const u8 *addr)
169{
170 int n = ether_crc(ETH_ALEN, addr) >> 26;
171 mask[n >> 5] |= (1 << (n & 31));
172}
173
174static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
175{
176 int n = ether_crc(ETH_ALEN, addr) >> 26;
177 return mask[n >> 5] & (1 << (n & 31));
178}
179
180static int update_filter(struct tap_filter *filter, void __user *arg)
181{
182 struct { u8 u[ETH_ALEN]; } *addr;
183 struct tun_filter uf;
184 int err, alen, n, nexact;
185
186 if (copy_from_user(&uf, arg, sizeof(uf)))
187 return -EFAULT;
188
189 if (!uf.count) {
190 /* Disabled */
191 filter->count = 0;
192 return 0;
193 }
194
195 alen = ETH_ALEN * uf.count;
196 addr = kmalloc(alen, GFP_KERNEL);
197 if (!addr)
198 return -ENOMEM;
199
200 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
201 err = -EFAULT;
202 goto done;
203 }
204
205 /* The filter is updated without holding any locks. Which is
206 * perfectly safe. We disable it first and in the worst
207 * case we'll accept a few undesired packets. */
208 filter->count = 0;
209 wmb();
210
211 /* Use first set of addresses as an exact filter */
212 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
213 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
214
215 nexact = n;
216
217 /* The rest is hashed */
218 memset(filter->mask, 0, sizeof(filter->mask));
219 for (; n < uf.count; n++)
220 addr_hash_set(filter->mask, addr[n].u);
221
222 /* For ALLMULTI just set the mask to all ones.
223 * This overrides the mask populated above. */
224 if ((uf.flags & TUN_FLT_ALLMULTI))
225 memset(filter->mask, ~0, sizeof(filter->mask));
226
227 /* Now enable the filter */
228 wmb();
229 filter->count = nexact;
230
231 /* Return the number of exact filters */
232 err = nexact;
233
234done:
235 kfree(addr);
236 return err;
237}
238
239/* Returns: 0 - drop, !=0 - accept */
240static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
241{
242 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
243 * at this point. */
244 struct ethhdr *eh = (struct ethhdr *) skb->data;
245 int i;
246
247 /* Exact match */
248 for (i = 0; i < filter->count; i++)
249 if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
250 return 1;
251
252 /* Inexact match (multicast only) */
253 if (is_multicast_ether_addr(eh->h_dest))
254 return addr_hash_test(filter->mask, eh->h_dest);
255
256 return 0;
257}
258
259/*
260 * Checks whether the packet is accepted or not.
261 * Returns: 0 - drop, !=0 - accept
262 */
263static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
264{
265 if (!filter->count)
266 return 1;
267
268 return run_filter(filter, skb);
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/* Network device part of the driver */
272
Jeff Garzik7282d492006-09-13 14:30:00 -0400273static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/* Net device open. */
276static int tun_net_open(struct net_device *dev)
277{
278 netif_start_queue(dev);
279 return 0;
280}
281
282/* Net device close. */
283static int tun_net_close(struct net_device *dev)
284{
285 netif_stop_queue(dev);
286 return 0;
287}
288
289/* Net device start xmit */
290static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
291{
292 struct tun_struct *tun = netdev_priv(dev);
293
294 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
295
296 /* Drop packet if interface is not attached */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000297 if (!tun->tfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto drop;
299
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700300 /* Drop if the filter does not like it.
301 * This is a noop if the filter is disabled.
302 * Filter can be enabled only for the TAP devices. */
303 if (!check_filter(&tun->txflt, skb))
304 goto drop;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
307 if (!(tun->flags & TUN_ONE_QUEUE)) {
308 /* Normal queueing mode. */
309 /* Packet scheduler handles dropping of further packets. */
310 netif_stop_queue(dev);
311
312 /* We won't see all dropped packets individually, so overrun
313 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700314 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
316 /* Single queue mode.
317 * Driver handles dropping of all packets itself. */
318 goto drop;
319 }
320 }
321
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700322 /* Enqueue packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 skb_queue_tail(&tun->readq, skb);
324 dev->trans_start = jiffies;
325
326 /* Notify and wake up reader process */
327 if (tun->flags & TUN_FASYNC)
328 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
329 wake_up_interruptible(&tun->read_wait);
330 return 0;
331
332drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700333 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 kfree_skb(skb);
335 return 0;
336}
337
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700338static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700340 /*
341 * This callback is supposed to deal with mc filter in
342 * _rx_ path and has nothing to do with the _tx_ path.
343 * In rx path we always accept everything userspace gives us.
344 */
345 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Ed Swierk4885a502007-09-16 12:21:38 -0700348#define MIN_MTU 68
349#define MAX_MTU 65535
350
351static int
352tun_net_change_mtu(struct net_device *dev, int new_mtu)
353{
354 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
355 return -EINVAL;
356 dev->mtu = new_mtu;
357 return 0;
358}
359
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800360static const struct net_device_ops tun_netdev_ops = {
361 .ndo_open = tun_net_open,
362 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800363 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800364 .ndo_change_mtu = tun_net_change_mtu,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800365};
366
367static const struct net_device_ops tap_netdev_ops = {
368 .ndo_open = tun_net_open,
369 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800370 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800371 .ndo_change_mtu = tun_net_change_mtu,
372 .ndo_set_multicast_list = tun_net_mclist,
373 .ndo_set_mac_address = eth_mac_addr,
374 .ndo_validate_addr = eth_validate_addr,
375};
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/* Initialize net device. */
378static void tun_net_init(struct net_device *dev)
379{
380 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 switch (tun->flags & TUN_TYPE_MASK) {
383 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800384 dev->netdev_ops = &tun_netdev_ops;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Point-to-Point TUN Device */
387 dev->hard_header_len = 0;
388 dev->addr_len = 0;
389 dev->mtu = 1500;
390
391 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400392 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
394 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
395 break;
396
397 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800398 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700400 ether_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700402 random_ether_addr(dev->dev_addr);
Brian Braunstein36226a82007-04-26 01:00:55 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
405 break;
406 }
407}
408
409/* Character device part */
410
411/* Poll */
412static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400413{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000414 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned int mask = POLLOUT | POLLWRNORM;
416
417 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000418 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
421
422 poll_wait(file, &tun->read_wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400423
David S. Millerb03efcf2005-07-08 14:57:23 -0700424 if (!skb_queue_empty(&tun->readq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 mask |= POLLIN | POLLRDNORM;
426
Eric W. Biederman631ab462009-01-20 11:00:40 +0000427 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return mask;
429}
430
Rusty Russellf42157c2008-08-15 15:15:10 -0700431/* prepad is the amount to reserve at front. len is length after that.
432 * linear is a hint as to how much to copy (usually headers). */
433static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
434 gfp_t gfp)
435{
436 struct sk_buff *skb;
437 unsigned int i;
438
439 skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
440 if (skb) {
441 skb_reserve(skb, prepad);
442 skb_put(skb, len);
443 return skb;
444 }
445
446 /* Under a page? Don't bother with paged skb. */
447 if (prepad + len < PAGE_SIZE)
448 return NULL;
449
450 /* Start with a normal skb, and add pages. */
451 skb = alloc_skb(prepad + linear, gfp);
452 if (!skb)
453 return NULL;
454
455 skb_reserve(skb, prepad);
456 skb_put(skb, linear);
457
458 len -= linear;
459
460 for (i = 0; i < MAX_SKB_FRAGS; i++) {
461 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
462
463 f->page = alloc_page(gfp|__GFP_ZERO);
464 if (!f->page)
465 break;
466
467 f->page_offset = 0;
468 f->size = PAGE_SIZE;
469
470 skb->data_len += PAGE_SIZE;
471 skb->len += PAGE_SIZE;
472 skb->truesize += PAGE_SIZE;
473 skb_shinfo(skb)->nr_frags++;
474
475 if (len < PAGE_SIZE) {
476 len = 0;
477 break;
478 }
479 len -= PAGE_SIZE;
480 }
481
482 /* Too large, or alloc fail? */
483 if (unlikely(len)) {
484 kfree_skb(skb);
485 skb = NULL;
486 }
487
488 return skb;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/* Get packet from user space buffer */
492static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
493{
494 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
495 struct sk_buff *skb;
496 size_t len = count, align = 0;
Rusty Russellf43798c2008-07-03 03:48:02 -0700497 struct virtio_net_hdr gso = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (!(tun->flags & TUN_NO_PI)) {
500 if ((len -= sizeof(pi)) > count)
501 return -EINVAL;
502
503 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
504 return -EFAULT;
505 }
506
Rusty Russellf43798c2008-07-03 03:48:02 -0700507 if (tun->flags & TUN_VNET_HDR) {
508 if ((len -= sizeof(gso)) > count)
509 return -EINVAL;
510
511 if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
512 return -EFAULT;
513
514 if (gso.hdr_len > len)
515 return -EINVAL;
516 }
517
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700518 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 align = NET_IP_ALIGN;
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700520 if (unlikely(len < ETH_HLEN))
521 return -EINVAL;
522 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400523
Rusty Russellf42157c2008-08-15 15:15:10 -0700524 if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700525 tun->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -ENOMEM;
527 }
528
Rusty Russellf42157c2008-08-15 15:15:10 -0700529 if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700530 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800531 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Rusty Russellf43798c2008-07-03 03:48:02 -0700535 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
536 if (!skb_partial_csum_set(skb, gso.csum_start,
537 gso.csum_offset)) {
538 tun->dev->stats.rx_frame_errors++;
539 kfree_skb(skb);
540 return -EINVAL;
541 }
542 } else if (tun->flags & TUN_NOCHECKSUM)
543 skb->ip_summed = CHECKSUM_UNNECESSARY;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 switch (tun->flags & TUN_TYPE_MASK) {
546 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700547 if (tun->flags & TUN_NO_PI) {
548 switch (skb->data[0] & 0xf0) {
549 case 0x40:
550 pi.proto = htons(ETH_P_IP);
551 break;
552 case 0x60:
553 pi.proto = htons(ETH_P_IPV6);
554 break;
555 default:
556 tun->dev->stats.rx_dropped++;
557 kfree_skb(skb);
558 return -EINVAL;
559 }
560 }
561
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700562 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700564 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
566 case TUN_TAP_DEV:
567 skb->protocol = eth_type_trans(skb, tun->dev);
568 break;
569 };
570
Rusty Russellf43798c2008-07-03 03:48:02 -0700571 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
572 pr_debug("GSO!\n");
573 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
574 case VIRTIO_NET_HDR_GSO_TCPV4:
575 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
576 break;
577 case VIRTIO_NET_HDR_GSO_TCPV6:
578 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
579 break;
580 default:
581 tun->dev->stats.rx_frame_errors++;
582 kfree_skb(skb);
583 return -EINVAL;
584 }
585
586 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
587 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
588
589 skb_shinfo(skb)->gso_size = gso.gso_size;
590 if (skb_shinfo(skb)->gso_size == 0) {
591 tun->dev->stats.rx_frame_errors++;
592 kfree_skb(skb);
593 return -EINVAL;
594 }
595
596 /* Header must be checked, and gso_segs computed. */
597 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
598 skb_shinfo(skb)->gso_segs = 0;
599 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400602
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700603 tun->dev->stats.rx_packets++;
604 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return count;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700609static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
610 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000612 struct tun_struct *tun = tun_get(iocb->ki_filp);
613 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (!tun)
616 return -EBADFD;
617
618 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
619
Eric W. Biederman631ab462009-01-20 11:00:40 +0000620 result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
621
622 tun_put(tun);
623 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* Put packet to the user space buffer */
627static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
628 struct sk_buff *skb,
629 struct iovec *iv, int len)
630{
631 struct tun_pi pi = { 0, skb->protocol };
632 ssize_t total = 0;
633
634 if (!(tun->flags & TUN_NO_PI)) {
635 if ((len -= sizeof(pi)) < 0)
636 return -EINVAL;
637
638 if (len < skb->len) {
639 /* Packet will be striped */
640 pi.flags |= TUN_PKT_STRIP;
641 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
644 return -EFAULT;
645 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Rusty Russellf43798c2008-07-03 03:48:02 -0700648 if (tun->flags & TUN_VNET_HDR) {
649 struct virtio_net_hdr gso = { 0 }; /* no info leak */
650 if ((len -= sizeof(gso)) < 0)
651 return -EINVAL;
652
653 if (skb_is_gso(skb)) {
654 struct skb_shared_info *sinfo = skb_shinfo(skb);
655
656 /* This is a hint as to how much should be linear. */
657 gso.hdr_len = skb_headlen(skb);
658 gso.gso_size = sinfo->gso_size;
659 if (sinfo->gso_type & SKB_GSO_TCPV4)
660 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
661 else if (sinfo->gso_type & SKB_GSO_TCPV6)
662 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
663 else
664 BUG();
665 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
666 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
667 } else
668 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
669
670 if (skb->ip_summed == CHECKSUM_PARTIAL) {
671 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
672 gso.csum_start = skb->csum_start - skb_headroom(skb);
673 gso.csum_offset = skb->csum_offset;
674 } /* else everything is zero */
675
676 if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
677 return -EFAULT;
678 total += sizeof(gso);
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 len = min_t(int, skb->len, len);
682
683 skb_copy_datagram_iovec(skb, 0, iv, len);
684 total += len;
685
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700686 tun->dev->stats.tx_packets++;
687 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return total;
690}
691
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700692static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
693 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700695 struct file *file = iocb->ki_filp;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000696 struct tun_struct *tun = tun_get(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 DECLARE_WAITQUEUE(wait, current);
698 struct sk_buff *skb;
699 ssize_t len, ret = 0;
700
701 if (!tun)
702 return -EBADFD;
703
704 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
705
Akinobu Mita52427c92007-11-19 22:46:51 -0800706 len = iov_length(iv, count);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000707 if (len < 0) {
708 ret = -EINVAL;
709 goto out;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 add_wait_queue(&tun->read_wait, &wait);
713 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 current->state = TASK_INTERRUPTIBLE;
715
716 /* Read frames from the queue */
717 if (!(skb=skb_dequeue(&tun->readq))) {
718 if (file->f_flags & O_NONBLOCK) {
719 ret = -EAGAIN;
720 break;
721 }
722 if (signal_pending(current)) {
723 ret = -ERESTARTSYS;
724 break;
725 }
726
727 /* Nothing to read, let's sleep */
728 schedule();
729 continue;
730 }
731 netif_wake_queue(tun->dev);
732
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700733 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
734 kfree_skb(skb);
735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 current->state = TASK_RUNNING;
739 remove_wait_queue(&tun->read_wait, &wait);
740
Eric W. Biederman631ab462009-01-20 11:00:40 +0000741out:
742 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static void tun_setup(struct net_device *dev)
747{
748 struct tun_struct *tun = netdev_priv(dev);
749
750 skb_queue_head_init(&tun->readq);
751 init_waitqueue_head(&tun->read_wait);
752
753 tun->owner = -1;
Guido Guenther8c644622007-07-02 22:50:25 -0700754 tun->group = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dev->ethtool_ops = &tun_ethtool_ops;
757 dev->destructor = free_netdev;
Pavel Emelyanovfc54c652008-04-16 00:41:53 -0700758 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Pavel Emelyanovd647a592008-04-16 00:41:16 -0700761static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct tun_struct *tun;
764 struct net_device *dev;
765 int err;
766
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +0000767 dev = __dev_get_by_name(net, ifr->ifr_name);
768 if (dev) {
769 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
770 tun = netdev_priv(dev);
771 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
772 tun = netdev_priv(dev);
773 else
774 return -EINVAL;
775
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000776 err = tun_attach(tun, file);
777 if (err < 0)
778 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 else {
781 char *name;
782 unsigned long flags = 0;
783
784 err = -EINVAL;
785
David Woodhouseca6bb5d2006-06-22 16:07:52 -0700786 if (!capable(CAP_NET_ADMIN))
787 return -EPERM;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Set dev type */
790 if (ifr->ifr_flags & IFF_TUN) {
791 /* TUN device */
792 flags |= TUN_TUN_DEV;
793 name = "tun%d";
794 } else if (ifr->ifr_flags & IFF_TAP) {
795 /* TAP device */
796 flags |= TUN_TAP_DEV;
797 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400798 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto failed;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (*ifr->ifr_name)
802 name = ifr->ifr_name;
803
804 dev = alloc_netdev(sizeof(struct tun_struct), name,
805 tun_setup);
806 if (!dev)
807 return -ENOMEM;
808
Pavel Emelyanovfc54c652008-04-16 00:41:53 -0700809 dev_net_set(dev, net);
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 tun = netdev_priv(dev);
812 tun->dev = dev;
813 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700814 tun->txflt.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 tun_net_init(dev);
817
818 if (strchr(dev->name, '%')) {
819 err = dev_alloc_name(dev, dev->name);
820 if (err < 0)
821 goto err_free_dev;
822 }
823
824 err = register_netdevice(tun->dev);
825 if (err < 0)
826 goto err_free_dev;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000827
828 err = tun_attach(tun, file);
829 if (err < 0)
830 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
834
835 if (ifr->ifr_flags & IFF_NO_PI)
836 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800837 else
838 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (ifr->ifr_flags & IFF_ONE_QUEUE)
841 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800842 else
843 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Rusty Russellf43798c2008-07-03 03:48:02 -0700845 if (ifr->ifr_flags & IFF_VNET_HDR)
846 tun->flags |= TUN_VNET_HDR;
847 else
848 tun->flags &= ~TUN_VNET_HDR;
849
Max Krasnyanskye35259a2008-07-10 16:59:11 -0700850 /* Make sure persistent devices do not get stuck in
851 * xoff state.
852 */
853 if (netif_running(tun->dev))
854 netif_wake_queue(tun->dev);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 strcpy(ifr->ifr_name, tun->dev->name);
857 return 0;
858
859 err_free_dev:
860 free_netdev(dev);
861 failed:
862 return err;
863}
864
Mark McLoughline3b99552008-08-15 15:09:56 -0700865static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
866{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000867 struct tun_struct *tun = tun_get(file);
Mark McLoughline3b99552008-08-15 15:09:56 -0700868
869 if (!tun)
870 return -EBADFD;
871
872 DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
873
874 strcpy(ifr->ifr_name, tun->dev->name);
875
876 ifr->ifr_flags = 0;
877
878 if (ifr->ifr_flags & TUN_TUN_DEV)
879 ifr->ifr_flags |= IFF_TUN;
880 else
881 ifr->ifr_flags |= IFF_TAP;
882
883 if (tun->flags & TUN_NO_PI)
884 ifr->ifr_flags |= IFF_NO_PI;
885
886 if (tun->flags & TUN_ONE_QUEUE)
887 ifr->ifr_flags |= IFF_ONE_QUEUE;
888
889 if (tun->flags & TUN_VNET_HDR)
890 ifr->ifr_flags |= IFF_VNET_HDR;
891
Eric W. Biederman631ab462009-01-20 11:00:40 +0000892 tun_put(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -0700893 return 0;
894}
895
Rusty Russell5228ddc2008-07-03 03:46:16 -0700896/* This is like a cut-down ethtool ops, except done via tun fd so no
897 * privs required. */
898static int set_offload(struct net_device *dev, unsigned long arg)
899{
900 unsigned int old_features, features;
901
902 old_features = dev->features;
903 /* Unset features, set them as we chew on the arg. */
904 features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
905 |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
906
907 if (arg & TUN_F_CSUM) {
908 features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
909 arg &= ~TUN_F_CSUM;
910
911 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
912 if (arg & TUN_F_TSO_ECN) {
913 features |= NETIF_F_TSO_ECN;
914 arg &= ~TUN_F_TSO_ECN;
915 }
916 if (arg & TUN_F_TSO4)
917 features |= NETIF_F_TSO;
918 if (arg & TUN_F_TSO6)
919 features |= NETIF_F_TSO6;
920 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
921 }
922 }
923
924 /* This gives the user a way to test for new features in future by
925 * trying to set them. */
926 if (arg)
927 return -EINVAL;
928
929 dev->features = features;
930 if (old_features != dev->features)
931 netdev_features_change(dev);
932
933 return 0;
934}
935
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400936static int tun_chr_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 unsigned int cmd, unsigned long arg)
938{
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
957 tun = tun_get(file);
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();
Pavel Emelyanovd647a592008-04-16 00:41:16 -0700964 err = tun_set_iff(current->nsproxy->net_ns, 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;
1128 file->private_data = tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return 0;
1130}
1131
1132static int tun_chr_close(struct inode *inode, struct file *file)
1133{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001134 struct tun_file *tfile = file->private_data;
1135 struct tun_struct *tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Eric W. Biederman631ab462009-01-20 11:00:40 +00001138 if (tun) {
1139 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Eric W. Biederman631ab462009-01-20 11:00:40 +00001141 rtnl_lock();
1142 __tun_detach(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Eric W. Biederman631ab462009-01-20 11:00:40 +00001144 /* If desireable, unregister the netdevice. */
1145 if (!(tun->flags & TUN_PERSIST))
1146 unregister_netdevice(tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Eric W. Biederman631ab462009-01-20 11:00:40 +00001148 rtnl_unlock();
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Eric W. Biederman631ab462009-01-20 11:00:40 +00001151 kfree(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 return 0;
1154}
1155
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001156static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001157 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001159 .read = do_sync_read,
1160 .aio_read = tun_chr_aio_read,
1161 .write = do_sync_write,
1162 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 .poll = tun_chr_poll,
1164 .ioctl = tun_chr_ioctl,
1165 .open = tun_chr_open,
1166 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001167 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168};
1169
1170static struct miscdevice tun_miscdev = {
1171 .minor = TUN_MINOR,
1172 .name = "tun",
1173 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174};
1175
1176/* ethtool interface */
1177
1178static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1179{
1180 cmd->supported = 0;
1181 cmd->advertising = 0;
1182 cmd->speed = SPEED_10;
1183 cmd->duplex = DUPLEX_FULL;
1184 cmd->port = PORT_TP;
1185 cmd->phy_address = 0;
1186 cmd->transceiver = XCVR_INTERNAL;
1187 cmd->autoneg = AUTONEG_DISABLE;
1188 cmd->maxtxpkt = 0;
1189 cmd->maxrxpkt = 0;
1190 return 0;
1191}
1192
1193static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1194{
1195 struct tun_struct *tun = netdev_priv(dev);
1196
1197 strcpy(info->driver, DRV_NAME);
1198 strcpy(info->version, DRV_VERSION);
1199 strcpy(info->fw_version, "N/A");
1200
1201 switch (tun->flags & TUN_TYPE_MASK) {
1202 case TUN_TUN_DEV:
1203 strcpy(info->bus_info, "tun");
1204 break;
1205 case TUN_TAP_DEV:
1206 strcpy(info->bus_info, "tap");
1207 break;
1208 }
1209}
1210
1211static u32 tun_get_msglevel(struct net_device *dev)
1212{
1213#ifdef TUN_DEBUG
1214 struct tun_struct *tun = netdev_priv(dev);
1215 return tun->debug;
1216#else
1217 return -EOPNOTSUPP;
1218#endif
1219}
1220
1221static void tun_set_msglevel(struct net_device *dev, u32 value)
1222{
1223#ifdef TUN_DEBUG
1224 struct tun_struct *tun = netdev_priv(dev);
1225 tun->debug = value;
1226#endif
1227}
1228
1229static u32 tun_get_link(struct net_device *dev)
1230{
1231 struct tun_struct *tun = netdev_priv(dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001232 return !!tun->tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235static u32 tun_get_rx_csum(struct net_device *dev)
1236{
1237 struct tun_struct *tun = netdev_priv(dev);
1238 return (tun->flags & TUN_NOCHECKSUM) == 0;
1239}
1240
1241static int tun_set_rx_csum(struct net_device *dev, u32 data)
1242{
1243 struct tun_struct *tun = netdev_priv(dev);
1244 if (data)
1245 tun->flags &= ~TUN_NOCHECKSUM;
1246 else
1247 tun->flags |= TUN_NOCHECKSUM;
1248 return 0;
1249}
1250
Jeff Garzik7282d492006-09-13 14:30:00 -04001251static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .get_settings = tun_get_settings,
1253 .get_drvinfo = tun_get_drvinfo,
1254 .get_msglevel = tun_get_msglevel,
1255 .set_msglevel = tun_set_msglevel,
1256 .get_link = tun_get_link,
1257 .get_rx_csum = tun_get_rx_csum,
1258 .set_rx_csum = tun_set_rx_csum
1259};
1260
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001261static int tun_init_net(struct net *net)
1262{
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001263 return 0;
1264}
1265
1266static void tun_exit_net(struct net *net)
1267{
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001268 struct net_device *dev, *next;
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001269
1270 rtnl_lock();
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001271 for_each_netdev_safe(net, dev, next) {
1272 if (dev->ethtool_ops != &tun_ethtool_ops)
1273 continue;
1274 DBG(KERN_INFO "%s cleaned up\n", dev->name);
1275 unregister_netdevice(dev);
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001276 }
1277 rtnl_unlock();
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001278}
1279
1280static struct pernet_operations tun_net_ops = {
1281 .init = tun_init_net,
1282 .exit = tun_exit_net,
1283};
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static int __init tun_init(void)
1286{
1287 int ret = 0;
1288
1289 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1290 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
1291
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001292 ret = register_pernet_device(&tun_net_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001293 if (ret) {
1294 printk(KERN_ERR "tun: Can't register pernet ops\n");
1295 goto err_pernet;
1296 }
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001299 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001301 goto err_misc;
1302 }
1303 return 0;
1304
1305err_misc:
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001306 unregister_pernet_device(&tun_net_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001307err_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return ret;
1309}
1310
1311static void tun_cleanup(void)
1312{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001313 misc_deregister(&tun_miscdev);
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001314 unregister_pernet_device(&tun_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317module_init(tun_init);
1318module_exit(tun_cleanup);
1319MODULE_DESCRIPTION(DRV_DESCRIPTION);
1320MODULE_AUTHOR(DRV_COPYRIGHT);
1321MODULE_LICENSE("GPL");
1322MODULE_ALIAS_MISCDEV(TUN_MINOR);