blob: 970ec4793442e001bc0008659ad1be485ddb7415 [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 *
Brian Braunstein36226a82007-04-26 01:00:55 -070021 * Brian Braunstein <linuxkernel@bristyle.com> 2007/03/23
22 * Fixed hw address handling. Now net_device.dev_addr is kept consistent
23 * with tun.dev_addr when the address is set by this module.
24 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070025 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
26 * Add TUNSETLINK ioctl to set the link encapsulation
27 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Mark Smith <markzzzsmith@yahoo.com.au>
29 * Use random_ether_addr() for tap MAC address.
30 *
31 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
32 * Fixes in packet dropping, queue length setting and queue wakeup.
33 * Increased default tx queue length.
34 * Added ethtool API.
35 * Minor cleanups
36 *
37 * Daniel Podlejski <underley@underley.eu.org>
38 * Modifications for 2.3.99-pre5 kernel.
39 */
40
41#define DRV_NAME "tun"
42#define DRV_VERSION "1.6"
43#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
44#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/errno.h>
48#include <linux/kernel.h>
49#include <linux/major.h>
50#include <linux/slab.h>
51#include <linux/poll.h>
52#include <linux/fcntl.h>
53#include <linux/init.h>
54#include <linux/skbuff.h>
55#include <linux/netdevice.h>
56#include <linux/etherdevice.h>
57#include <linux/miscdevice.h>
58#include <linux/ethtool.h>
59#include <linux/rtnetlink.h>
60#include <linux/if.h>
61#include <linux/if_arp.h>
62#include <linux/if_ether.h>
63#include <linux/if_tun.h>
64#include <linux/crc32.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070065#include <net/net_namespace.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
Rusty Russell14daa022008-04-12 18:48:58 -070083struct tun_struct {
84 struct list_head list;
85 unsigned long flags;
86 int attached;
87 uid_t owner;
88 gid_t group;
89
90 wait_queue_head_t read_wait;
91 struct sk_buff_head readq;
92
93 struct net_device *dev;
94
95 struct fasync_struct *fasync;
96
97 unsigned long if_flags;
98 u8 dev_addr[ETH_ALEN];
99 u32 chr_filter[2];
100 u32 net_filter[2];
101
102#ifdef TUN_DEBUG
103 int debug;
104#endif
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* Network device part of the driver */
108
109static LIST_HEAD(tun_dev_list);
Jeff Garzik7282d492006-09-13 14:30:00 -0400110static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Net device open. */
113static int tun_net_open(struct net_device *dev)
114{
115 netif_start_queue(dev);
116 return 0;
117}
118
119/* Net device close. */
120static int tun_net_close(struct net_device *dev)
121{
122 netif_stop_queue(dev);
123 return 0;
124}
125
126/* Net device start xmit */
127static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
128{
129 struct tun_struct *tun = netdev_priv(dev);
130
131 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
132
133 /* Drop packet if interface is not attached */
134 if (!tun->attached)
135 goto drop;
136
137 /* Packet dropping */
138 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
139 if (!(tun->flags & TUN_ONE_QUEUE)) {
140 /* Normal queueing mode. */
141 /* Packet scheduler handles dropping of further packets. */
142 netif_stop_queue(dev);
143
144 /* We won't see all dropped packets individually, so overrun
145 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700146 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } else {
148 /* Single queue mode.
149 * Driver handles dropping of all packets itself. */
150 goto drop;
151 }
152 }
153
154 /* Queue packet */
155 skb_queue_tail(&tun->readq, skb);
156 dev->trans_start = jiffies;
157
158 /* Notify and wake up reader process */
159 if (tun->flags & TUN_FASYNC)
160 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
161 wake_up_interruptible(&tun->read_wait);
162 return 0;
163
164drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700165 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kfree_skb(skb);
167 return 0;
168}
169
170/** Add the specified Ethernet address to this multicast filter. */
171static void
172add_multi(u32* filter, const u8* addr)
173{
174 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
175 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
176}
177
178/** Remove the specified Ethernet addres from this multicast filter. */
179static void
180del_multi(u32* filter, const u8* addr)
181{
182 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
183 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
184}
185
186/** Update the list of multicast groups to which the network device belongs.
187 * This list is used to filter packets being sent from the character device to
188 * the network device. */
189static void
190tun_net_mclist(struct net_device *dev)
191{
192 struct tun_struct *tun = netdev_priv(dev);
193 const struct dev_mc_list *mclist;
194 int i;
Joe Perches0795af52007-10-03 17:59:30 -0700195 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
197 dev->name, dev->mc_count);
198 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
199 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
200 i++, mclist = mclist->next) {
201 add_multi(tun->net_filter, mclist->dmi_addr);
Joe Perches0795af52007-10-03 17:59:30 -0700202 DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
203 dev->name, print_mac(mac, mclist->dmi_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205}
206
Ed Swierk4885a502007-09-16 12:21:38 -0700207#define MIN_MTU 68
208#define MAX_MTU 65535
209
210static int
211tun_net_change_mtu(struct net_device *dev, int new_mtu)
212{
213 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
214 return -EINVAL;
215 dev->mtu = new_mtu;
216 return 0;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/* Initialize net device. */
220static void tun_net_init(struct net_device *dev)
221{
222 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 switch (tun->flags & TUN_TYPE_MASK) {
225 case TUN_TUN_DEV:
226 /* Point-to-Point TUN Device */
227 dev->hard_header_len = 0;
228 dev->addr_len = 0;
229 dev->mtu = 1500;
Ed Swierk4885a502007-09-16 12:21:38 -0700230 dev->change_mtu = tun_net_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400233 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
235 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
236 break;
237
238 case TUN_TAP_DEV:
239 /* Ethernet TAP Device */
240 dev->set_multicast_list = tun_net_mclist;
241
242 ether_setup(dev);
Ed Swierk4885a502007-09-16 12:21:38 -0700243 dev->change_mtu = tun_net_change_mtu;
Brian Braunstein36226a82007-04-26 01:00:55 -0700244
245 /* random address already created for us by tun_set_iff, use it */
246 memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
249 break;
250 }
251}
252
253/* Character device part */
254
255/* Poll */
256static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct tun_struct *tun = file->private_data;
259 unsigned int mask = POLLOUT | POLLWRNORM;
260
261 if (!tun)
262 return -EBADFD;
263
264 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
265
266 poll_wait(file, &tun->read_wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400267
David S. Millerb03efcf2005-07-08 14:57:23 -0700268 if (!skb_queue_empty(&tun->readq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 mask |= POLLIN | POLLRDNORM;
270
271 return mask;
272}
273
274/* Get packet from user space buffer */
275static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
276{
277 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
278 struct sk_buff *skb;
279 size_t len = count, align = 0;
280
281 if (!(tun->flags & TUN_NO_PI)) {
282 if ((len -= sizeof(pi)) > count)
283 return -EINVAL;
284
285 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
286 return -EFAULT;
287 }
288
289 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
290 align = NET_IP_ALIGN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700293 tun->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -ENOMEM;
295 }
296
297 if (align)
298 skb_reserve(skb, align);
Dave Jones8f227572006-03-11 18:49:13 -0800299 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700300 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800301 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (tun->flags & TUN_TYPE_MASK) {
306 case TUN_TUN_DEV:
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700307 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700309 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 case TUN_TAP_DEV:
312 skb->protocol = eth_type_trans(skb, tun->dev);
313 break;
314 };
315
316 if (tun->flags & TUN_NOCHECKSUM)
317 skb->ip_summed = CHECKSUM_UNNECESSARY;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 netif_rx_ni(skb);
320 tun->dev->last_rx = jiffies;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400321
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700322 tun->dev->stats.rx_packets++;
323 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return count;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700328static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
329 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700331 struct tun_struct *tun = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (!tun)
334 return -EBADFD;
335
336 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
337
Akinobu Mita52427c92007-11-19 22:46:51 -0800338 return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/* Put packet to the user space buffer */
342static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
343 struct sk_buff *skb,
344 struct iovec *iv, int len)
345{
346 struct tun_pi pi = { 0, skb->protocol };
347 ssize_t total = 0;
348
349 if (!(tun->flags & TUN_NO_PI)) {
350 if ((len -= sizeof(pi)) < 0)
351 return -EINVAL;
352
353 if (len < skb->len) {
354 /* Packet will be striped */
355 pi.flags |= TUN_PKT_STRIP;
356 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
359 return -EFAULT;
360 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 len = min_t(int, skb->len, len);
364
365 skb_copy_datagram_iovec(skb, 0, iv, len);
366 total += len;
367
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700368 tun->dev->stats.tx_packets++;
369 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return total;
372}
373
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700374static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
375 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700377 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct tun_struct *tun = file->private_data;
379 DECLARE_WAITQUEUE(wait, current);
380 struct sk_buff *skb;
381 ssize_t len, ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700382 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!tun)
385 return -EBADFD;
386
387 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
388
Akinobu Mita52427c92007-11-19 22:46:51 -0800389 len = iov_length(iv, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (len < 0)
391 return -EINVAL;
392
393 add_wait_queue(&tun->read_wait, &wait);
394 while (len) {
395 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
396 u8 addr[ ETH_ALEN];
397 int bit_nr;
398
399 current->state = TASK_INTERRUPTIBLE;
400
401 /* Read frames from the queue */
402 if (!(skb=skb_dequeue(&tun->readq))) {
403 if (file->f_flags & O_NONBLOCK) {
404 ret = -EAGAIN;
405 break;
406 }
407 if (signal_pending(current)) {
408 ret = -ERESTARTSYS;
409 break;
410 }
411
412 /* Nothing to read, let's sleep */
413 schedule();
414 continue;
415 }
416 netif_wake_queue(tun->dev);
417
418 /** Decide whether to accept this packet. This code is designed to
419 * behave identically to an Ethernet interface. Accept the packet if
420 * - we are promiscuous.
421 * - the packet is addressed to us.
422 * - the packet is broadcast.
423 * - the packet is multicast and
424 * - we are multicast promiscous.
425 * - we belong to the multicast group.
426 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300427 skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
428 skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 bit_nr = ether_crc(sizeof addr, addr) >> 26;
430 if ((tun->if_flags & IFF_PROMISC) ||
431 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
432 memcmp(addr, ones, sizeof addr) == 0 ||
433 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
434 (addr[0] == 0x33 && addr[1] == 0x33)) &&
435 ((tun->if_flags & IFF_ALLMULTI) ||
436 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
Joe Perches0795af52007-10-03 17:59:30 -0700437 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
438 tun->dev->name, print_mac(mac, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
440 kfree_skb(skb);
441 break;
442 } else {
Joe Perches0795af52007-10-03 17:59:30 -0700443 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
444 tun->dev->name, print_mac(mac, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree_skb(skb);
446 continue;
447 }
448 }
449
450 current->state = TASK_RUNNING;
451 remove_wait_queue(&tun->read_wait, &wait);
452
453 return ret;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static void tun_setup(struct net_device *dev)
457{
458 struct tun_struct *tun = netdev_priv(dev);
459
460 skb_queue_head_init(&tun->readq);
461 init_waitqueue_head(&tun->read_wait);
462
463 tun->owner = -1;
Guido Guenther8c644622007-07-02 22:50:25 -0700464 tun->group = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 dev->open = tun_net_open;
467 dev->hard_start_xmit = tun_net_xmit;
468 dev->stop = tun_net_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dev->ethtool_ops = &tun_ethtool_ops;
470 dev->destructor = free_netdev;
471}
472
473static struct tun_struct *tun_get_by_name(const char *name)
474{
475 struct tun_struct *tun;
476
477 ASSERT_RTNL();
478 list_for_each_entry(tun, &tun_dev_list, list) {
479 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
480 return tun;
481 }
482
483 return NULL;
484}
485
486static int tun_set_iff(struct file *file, struct ifreq *ifr)
487{
488 struct tun_struct *tun;
489 struct net_device *dev;
490 int err;
491
492 tun = tun_get_by_name(ifr->ifr_name);
493 if (tun) {
494 if (tun->attached)
495 return -EBUSY;
496
497 /* Check permissions */
Guido Guenther8c644622007-07-02 22:50:25 -0700498 if (((tun->owner != -1 &&
499 current->euid != tun->owner) ||
500 (tun->group != -1 &&
501 current->egid != tun->group)) &&
502 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EPERM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400504 }
Eric W. Biederman881d9662007-09-17 11:56:21 -0700505 else if (__dev_get_by_name(&init_net, ifr->ifr_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EINVAL;
507 else {
508 char *name;
509 unsigned long flags = 0;
510
511 err = -EINVAL;
512
David Woodhouseca6bb5d2006-06-22 16:07:52 -0700513 if (!capable(CAP_NET_ADMIN))
514 return -EPERM;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* Set dev type */
517 if (ifr->ifr_flags & IFF_TUN) {
518 /* TUN device */
519 flags |= TUN_TUN_DEV;
520 name = "tun%d";
521 } else if (ifr->ifr_flags & IFF_TAP) {
522 /* TAP device */
523 flags |= TUN_TAP_DEV;
524 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400525 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto failed;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (*ifr->ifr_name)
529 name = ifr->ifr_name;
530
531 dev = alloc_netdev(sizeof(struct tun_struct), name,
532 tun_setup);
533 if (!dev)
534 return -ENOMEM;
535
536 tun = netdev_priv(dev);
537 tun->dev = dev;
538 tun->flags = flags;
539 /* Be promiscuous by default to maintain previous behaviour. */
540 tun->if_flags = IFF_PROMISC;
541 /* Generate random Ethernet address. */
Al Viroa3edb082007-12-22 17:52:42 +0000542 *(__be16 *)tun->dev_addr = htons(0x00FF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
544 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
545
546 tun_net_init(dev);
547
548 if (strchr(dev->name, '%')) {
549 err = dev_alloc_name(dev, dev->name);
550 if (err < 0)
551 goto err_free_dev;
552 }
553
554 err = register_netdevice(tun->dev);
555 if (err < 0)
556 goto err_free_dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 list_add(&tun->list, &tun_dev_list);
559 }
560
561 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
562
563 if (ifr->ifr_flags & IFF_NO_PI)
564 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800565 else
566 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (ifr->ifr_flags & IFF_ONE_QUEUE)
569 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -0800570 else
571 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 file->private_data = tun;
574 tun->attached = 1;
575
576 strcpy(ifr->ifr_name, tun->dev->name);
577 return 0;
578
579 err_free_dev:
580 free_netdev(dev);
581 failed:
582 return err;
583}
584
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400585static int tun_chr_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned int cmd, unsigned long arg)
587{
588 struct tun_struct *tun = file->private_data;
589 void __user* argp = (void __user*)arg;
590 struct ifreq ifr;
Joe Perches0795af52007-10-03 17:59:30 -0700591 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
594 if (copy_from_user(&ifr, argp, sizeof ifr))
595 return -EFAULT;
596
597 if (cmd == TUNSETIFF && !tun) {
598 int err;
599
600 ifr.ifr_name[IFNAMSIZ-1] = '\0';
601
602 rtnl_lock();
603 err = tun_set_iff(file, &ifr);
604 rtnl_unlock();
605
606 if (err)
607 return err;
608
609 if (copy_to_user(argp, &ifr, sizeof(ifr)))
610 return -EFAULT;
611 return 0;
612 }
613
614 if (!tun)
615 return -EBADFD;
616
617 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
618
619 switch (cmd) {
620 case TUNSETNOCSUM:
621 /* Disable/Enable checksum */
622 if (arg)
623 tun->flags |= TUN_NOCHECKSUM;
624 else
625 tun->flags &= ~TUN_NOCHECKSUM;
626
627 DBG(KERN_INFO "%s: checksum %s\n",
628 tun->dev->name, arg ? "disabled" : "enabled");
629 break;
630
631 case TUNSETPERSIST:
632 /* Disable/Enable persist mode */
633 if (arg)
634 tun->flags |= TUN_PERSIST;
635 else
636 tun->flags &= ~TUN_PERSIST;
637
638 DBG(KERN_INFO "%s: persist %s\n",
Toyo Abec6e991d2007-12-24 21:29:35 -0800639 tun->dev->name, arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641
642 case TUNSETOWNER:
643 /* Set owner of the device */
644 tun->owner = (uid_t) arg;
645
646 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
647 break;
648
Guido Guenther8c644622007-07-02 22:50:25 -0700649 case TUNSETGROUP:
650 /* Set group of the device */
651 tun->group= (gid_t) arg;
652
653 DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
654 break;
655
Mike Kershawff4cc3a2005-09-01 17:40:05 -0700656 case TUNSETLINK:
657 /* Only allow setting the type when the interface is down */
658 if (tun->dev->flags & IFF_UP) {
659 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
660 tun->dev->name);
661 return -EBUSY;
662 } else {
663 tun->dev->type = (int) arg;
664 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
665 }
666 break;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#ifdef TUN_DEBUG
669 case TUNSETDEBUG:
670 tun->debug = arg;
671 break;
672#endif
673
674 case SIOCGIFFLAGS:
675 ifr.ifr_flags = tun->if_flags;
676 if (copy_to_user( argp, &ifr, sizeof ifr))
677 return -EFAULT;
678 return 0;
679
680 case SIOCSIFFLAGS:
681 /** Set the character device's interface flags. Currently only
682 * IFF_PROMISC and IFF_ALLMULTI are used. */
683 tun->if_flags = ifr.ifr_flags;
684 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
685 tun->dev->name, tun->if_flags);
686 return 0;
687
688 case SIOCGIFHWADDR:
Brian Braunstein36226a82007-04-26 01:00:55 -0700689 /* Note: the actual net device's address may be different */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
691 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
692 if (copy_to_user( argp, &ifr, sizeof ifr))
693 return -EFAULT;
694 return 0;
695
696 case SIOCSIFHWADDR:
Brian Braunstein36226a82007-04-26 01:00:55 -0700697 {
698 /* try to set the actual net device's hw address */
Kim B. Heino40102372008-02-29 12:26:21 -0800699 int ret;
700
701 rtnl_lock();
702 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
703 rtnl_unlock();
Brian Braunstein36226a82007-04-26 01:00:55 -0700704
705 if (ret == 0) {
706 /** Set the character device's hardware address. This is used when
707 * filtering packets being sent from the network device to the character
708 * device. */
709 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
710 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
711 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
712 tun->dev->name,
713 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
714 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
715 }
716
717 return ret;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 case SIOCADDMULTI:
721 /** Add the specified group to the character device's multicast filter
722 * list. */
723 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
Joe Perches0795af52007-10-03 17:59:30 -0700724 DBG(KERN_DEBUG "%s: add multi: %s\n",
725 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727
728 case SIOCDELMULTI:
729 /** Remove the specified group from the character device's multicast
730 * filter list. */
731 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
Joe Perches0795af52007-10-03 17:59:30 -0700732 DBG(KERN_DEBUG "%s: del multi: %s\n",
733 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735
736 default:
737 return -EINVAL;
738 };
739
740 return 0;
741}
742
743static int tun_chr_fasync(int fd, struct file *file, int on)
744{
745 struct tun_struct *tun = file->private_data;
746 int ret;
747
748 if (!tun)
749 return -EBADFD;
750
751 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
752
753 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400754 return ret;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700757 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (ret)
759 return ret;
760 tun->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400761 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 tun->flags &= ~TUN_FASYNC;
763
764 return 0;
765}
766
767static int tun_chr_open(struct inode *inode, struct file * file)
768{
769 DBG1(KERN_INFO "tunX: tun_chr_open\n");
770 file->private_data = NULL;
771 return 0;
772}
773
774static int tun_chr_close(struct inode *inode, struct file *file)
775{
776 struct tun_struct *tun = file->private_data;
777
778 if (!tun)
779 return 0;
780
781 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
782
783 tun_chr_fasync(-1, file, 0);
784
785 rtnl_lock();
786
787 /* Detach from net device */
788 file->private_data = NULL;
789 tun->attached = 0;
790
791 /* Drop read queue */
792 skb_queue_purge(&tun->readq);
793
794 if (!(tun->flags & TUN_PERSIST)) {
795 list_del(&tun->list);
796 unregister_netdevice(tun->dev);
797 }
798
799 rtnl_unlock();
800
801 return 0;
802}
803
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800804static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400805 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700807 .read = do_sync_read,
808 .aio_read = tun_chr_aio_read,
809 .write = do_sync_write,
810 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .poll = tun_chr_poll,
812 .ioctl = tun_chr_ioctl,
813 .open = tun_chr_open,
814 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400815 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816};
817
818static struct miscdevice tun_miscdev = {
819 .minor = TUN_MINOR,
820 .name = "tun",
821 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822};
823
824/* ethtool interface */
825
826static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
827{
828 cmd->supported = 0;
829 cmd->advertising = 0;
830 cmd->speed = SPEED_10;
831 cmd->duplex = DUPLEX_FULL;
832 cmd->port = PORT_TP;
833 cmd->phy_address = 0;
834 cmd->transceiver = XCVR_INTERNAL;
835 cmd->autoneg = AUTONEG_DISABLE;
836 cmd->maxtxpkt = 0;
837 cmd->maxrxpkt = 0;
838 return 0;
839}
840
841static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
842{
843 struct tun_struct *tun = netdev_priv(dev);
844
845 strcpy(info->driver, DRV_NAME);
846 strcpy(info->version, DRV_VERSION);
847 strcpy(info->fw_version, "N/A");
848
849 switch (tun->flags & TUN_TYPE_MASK) {
850 case TUN_TUN_DEV:
851 strcpy(info->bus_info, "tun");
852 break;
853 case TUN_TAP_DEV:
854 strcpy(info->bus_info, "tap");
855 break;
856 }
857}
858
859static u32 tun_get_msglevel(struct net_device *dev)
860{
861#ifdef TUN_DEBUG
862 struct tun_struct *tun = netdev_priv(dev);
863 return tun->debug;
864#else
865 return -EOPNOTSUPP;
866#endif
867}
868
869static void tun_set_msglevel(struct net_device *dev, u32 value)
870{
871#ifdef TUN_DEBUG
872 struct tun_struct *tun = netdev_priv(dev);
873 tun->debug = value;
874#endif
875}
876
877static u32 tun_get_link(struct net_device *dev)
878{
879 struct tun_struct *tun = netdev_priv(dev);
880 return tun->attached;
881}
882
883static u32 tun_get_rx_csum(struct net_device *dev)
884{
885 struct tun_struct *tun = netdev_priv(dev);
886 return (tun->flags & TUN_NOCHECKSUM) == 0;
887}
888
889static int tun_set_rx_csum(struct net_device *dev, u32 data)
890{
891 struct tun_struct *tun = netdev_priv(dev);
892 if (data)
893 tun->flags &= ~TUN_NOCHECKSUM;
894 else
895 tun->flags |= TUN_NOCHECKSUM;
896 return 0;
897}
898
Jeff Garzik7282d492006-09-13 14:30:00 -0400899static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .get_settings = tun_get_settings,
901 .get_drvinfo = tun_get_drvinfo,
902 .get_msglevel = tun_get_msglevel,
903 .set_msglevel = tun_set_msglevel,
904 .get_link = tun_get_link,
905 .get_rx_csum = tun_get_rx_csum,
906 .set_rx_csum = tun_set_rx_csum
907};
908
909static int __init tun_init(void)
910{
911 int ret = 0;
912
913 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
914 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
915
916 ret = misc_register(&tun_miscdev);
917 if (ret)
918 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
919 return ret;
920}
921
922static void tun_cleanup(void)
923{
924 struct tun_struct *tun, *nxt;
925
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400926 misc_deregister(&tun_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 rtnl_lock();
929 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
930 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
931 unregister_netdevice(tun->dev);
932 }
933 rtnl_unlock();
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937module_init(tun_init);
938module_exit(tun_cleanup);
939MODULE_DESCRIPTION(DRV_DESCRIPTION);
940MODULE_AUTHOR(DRV_COPYRIGHT);
941MODULE_LICENSE("GPL");
942MODULE_ALIAS_MISCDEV(TUN_MINOR);