]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/tun.c
net: make struct tun_struct private to tun.c
[thirdparty/linux.git] / drivers / net / tun.c
CommitLineData
1da177e4
LT
1/*
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 *
36226a8d
BB
21 * 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 *
ff4cc3ac
MK
25 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
26 * Add TUNSETLINK ioctl to set the link encapsulation
27 *
1da177e4
LT
28 * 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
1da177e4
LT
46#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>
881d966b 65#include <net/net_namespace.h>
1da177e4
LT
66
67#include <asm/system.h>
68#include <asm/uaccess.h>
69
14daa021
RR
70/* Uncomment to enable debugging */
71/* #define TUN_DEBUG 1 */
72
1da177e4
LT
73#ifdef TUN_DEBUG
74static int debug;
14daa021
RR
75
76#define DBG if(tun->debug)printk
77#define DBG1 if(debug==2)printk
78#else
79#define DBG( a... )
80#define DBG1( a... )
81#endif
82
83struct 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;
1da177e4 104#endif
14daa021 105};
1da177e4
LT
106
107/* Network device part of the driver */
108
109static LIST_HEAD(tun_dev_list);
7282d491 110static const struct ethtool_ops tun_ethtool_ops;
1da177e4
LT
111
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. */
09f75cd7 146 dev->stats.tx_fifo_errors++;
1da177e4
LT
147 } 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:
09f75cd7 165 dev->stats.tx_dropped++;
1da177e4
LT
166 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;
0795af57 195 DECLARE_MAC_BUF(mac);
1da177e4
LT
196 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);
0795af57
JP
202 DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
203 dev->name, print_mac(mac, mclist->dmi_addr));
1da177e4
LT
204 }
205}
206
4885a504
ES
207#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
1da177e4
LT
219/* Initialize net device. */
220static void tun_net_init(struct net_device *dev)
221{
222 struct tun_struct *tun = netdev_priv(dev);
6aa20a22 223
1da177e4
LT
224 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;
4885a504 230 dev->change_mtu = tun_net_change_mtu;
1da177e4
LT
231
232 /* Zero header length */
6aa20a22 233 dev->type = ARPHRD_NONE;
1da177e4
LT
234 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);
4885a504 243 dev->change_mtu = tun_net_change_mtu;
36226a8d
BB
244
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
1da177e4
LT
248 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)
6aa20a22 257{
1da177e4
LT
258 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);
6aa20a22 267
b03efcfb 268 if (!skb_queue_empty(&tun->readq))
1da177e4
LT
269 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;
6aa20a22 291
1da177e4 292 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
09f75cd7 293 tun->dev->stats.rx_dropped++;
1da177e4
LT
294 return -ENOMEM;
295 }
296
297 if (align)
298 skb_reserve(skb, align);
8f22757e 299 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
09f75cd7 300 tun->dev->stats.rx_dropped++;
8f22757e 301 kfree_skb(skb);
1da177e4 302 return -EFAULT;
8f22757e 303 }
1da177e4 304
1da177e4
LT
305 switch (tun->flags & TUN_TYPE_MASK) {
306 case TUN_TUN_DEV:
459a98ed 307 skb_reset_mac_header(skb);
1da177e4 308 skb->protocol = pi.proto;
4c13eb66 309 skb->dev = tun->dev;
1da177e4
LT
310 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;
6aa20a22 318
1da177e4
LT
319 netif_rx_ni(skb);
320 tun->dev->last_rx = jiffies;
6aa20a22 321
09f75cd7
JG
322 tun->dev->stats.rx_packets++;
323 tun->dev->stats.rx_bytes += len;
1da177e4
LT
324
325 return count;
6aa20a22 326}
1da177e4 327
ee0b3e67
BP
328static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
329 unsigned long count, loff_t pos)
1da177e4 330{
ee0b3e67 331 struct tun_struct *tun = iocb->ki_filp->private_data;
1da177e4
LT
332
333 if (!tun)
334 return -EBADFD;
335
336 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
337
52427c9d 338 return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
1da177e4
LT
339}
340
1da177e4
LT
341/* 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 }
6aa20a22 357
1da177e4
LT
358 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
359 return -EFAULT;
360 total += sizeof(pi);
6aa20a22 361 }
1da177e4
LT
362
363 len = min_t(int, skb->len, len);
364
365 skb_copy_datagram_iovec(skb, 0, iv, len);
366 total += len;
367
09f75cd7
JG
368 tun->dev->stats.tx_packets++;
369 tun->dev->stats.tx_bytes += len;
1da177e4
LT
370
371 return total;
372}
373
ee0b3e67
BP
374static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
375 unsigned long count, loff_t pos)
1da177e4 376{
ee0b3e67 377 struct file *file = iocb->ki_filp;
1da177e4
LT
378 struct tun_struct *tun = file->private_data;
379 DECLARE_WAITQUEUE(wait, current);
380 struct sk_buff *skb;
381 ssize_t len, ret = 0;
0795af57 382 DECLARE_MAC_BUF(mac);
1da177e4
LT
383
384 if (!tun)
385 return -EBADFD;
386
387 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
388
52427c9d 389 len = iov_length(iv, count);
1da177e4
LT
390 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 */
d626f62b
ACM
427 skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
428 skb->len));
1da177e4
LT
429 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)))))) {
0795af57
JP
437 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
438 tun->dev->name, print_mac(mac, addr));
1da177e4
LT
439 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
440 kfree_skb(skb);
441 break;
442 } else {
0795af57
JP
443 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
444 tun->dev->name, print_mac(mac, addr));
1da177e4
LT
445 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
1da177e4
LT
456static 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;
8c644623 464 tun->group = -1;
1da177e4 465
1da177e4
LT
466 dev->open = tun_net_open;
467 dev->hard_start_xmit = tun_net_xmit;
468 dev->stop = tun_net_close;
1da177e4
LT
469 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 */
8c644623
GG
498 if (((tun->owner != -1 &&
499 current->euid != tun->owner) ||
500 (tun->group != -1 &&
501 current->egid != tun->group)) &&
502 !capable(CAP_NET_ADMIN))
1da177e4 503 return -EPERM;
6aa20a22 504 }
881d966b 505 else if (__dev_get_by_name(&init_net, ifr->ifr_name))
1da177e4
LT
506 return -EINVAL;
507 else {
508 char *name;
509 unsigned long flags = 0;
510
511 err = -EINVAL;
512
ca6bb5d7
DW
513 if (!capable(CAP_NET_ADMIN))
514 return -EPERM;
515
1da177e4
LT
516 /* 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";
6aa20a22 525 } else
1da177e4 526 goto failed;
6aa20a22 527
1da177e4
LT
528 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. */
a3edb083 542 *(__be16 *)tun->dev_addr = htons(0x00FF);
1da177e4
LT
543 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;
6aa20a22 557
1da177e4
LT
558 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;
a26af1e0
NF
565 else
566 tun->flags &= ~TUN_NO_PI;
1da177e4
LT
567
568 if (ifr->ifr_flags & IFF_ONE_QUEUE)
569 tun->flags |= TUN_ONE_QUEUE;
a26af1e0
NF
570 else
571 tun->flags &= ~TUN_ONE_QUEUE;
1da177e4
LT
572
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
6aa20a22 585static int tun_chr_ioctl(struct inode *inode, struct file *file,
1da177e4
LT
586 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;
0795af57 591 DECLARE_MAC_BUF(mac);
1da177e4
LT
592
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",
c6e991de 639 tun->dev->name, arg ? "enabled" : "disabled");
1da177e4
LT
640 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
8c644623
GG
649 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
ff4cc3ac
MK
656 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
1da177e4
LT
668#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:
36226a8d 689 /* Note: the actual net device's address may be different */
1da177e4
LT
690 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:
36226a8d
BB
697 {
698 /* try to set the actual net device's hw address */
40102371
KH
699 int ret;
700
701 rtnl_lock();
702 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
703 rtnl_unlock();
36226a8d
BB
704
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 }
1da177e4
LT
719
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);
0795af57
JP
724 DBG(KERN_DEBUG "%s: add multi: %s\n",
725 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
1da177e4
LT
726 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);
0795af57
JP
732 DBG(KERN_DEBUG "%s: del multi: %s\n",
733 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
1da177e4
LT
734 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)
6aa20a22
JG
754 return ret;
755
1da177e4 756 if (on) {
609d7fa9 757 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1da177e4
LT
758 if (ret)
759 return ret;
760 tun->flags |= TUN_FASYNC;
6aa20a22 761 } else
1da177e4
LT
762 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
d54b1fdb 804static const struct file_operations tun_fops = {
6aa20a22 805 .owner = THIS_MODULE,
1da177e4 806 .llseek = no_llseek,
ee0b3e67
BP
807 .read = do_sync_read,
808 .aio_read = tun_chr_aio_read,
809 .write = do_sync_write,
810 .aio_write = tun_chr_aio_write,
1da177e4
LT
811 .poll = tun_chr_poll,
812 .ioctl = tun_chr_ioctl,
813 .open = tun_chr_open,
814 .release = tun_chr_close,
6aa20a22 815 .fasync = tun_chr_fasync
1da177e4
LT
816};
817
818static struct miscdevice tun_miscdev = {
819 .minor = TUN_MINOR,
820 .name = "tun",
821 .fops = &tun_fops,
1da177e4
LT
822};
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
7282d491 899static const struct ethtool_ops tun_ethtool_ops = {
1da177e4
LT
900 .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
6aa20a22 926 misc_deregister(&tun_miscdev);
1da177e4
LT
927
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();
6aa20a22 934
1da177e4
LT
935}
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);