patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / tun.c
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  *  Daniel Podlejski <underley@underley.eu.org>
20  *    Modifications for 2.3.99-pre5 kernel.
21  */
22
23 #define TUN_VER "1.5"
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/major.h>
30 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/fcntl.h>
33 #include <linux/init.h>
34 #include <linux/random.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/miscdevice.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/if.h>
41 #include <linux/if_arp.h>
42 #include <linux/if_ether.h>
43 #include <linux/if_tun.h>
44
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
47
48 #ifdef TUN_DEBUG
49 static int debug;
50 #endif
51
52 /* Network device part of the driver */
53
54 static LIST_HEAD(tun_dev_list);
55
56 /* Net device open. */
57 static int tun_net_open(struct net_device *dev)
58 {
59         netif_start_queue(dev);
60         return 0;
61 }
62
63 /* Net device close. */
64 static int tun_net_close(struct net_device *dev)
65 {
66         netif_stop_queue(dev);
67         return 0;
68 }
69
70 /* Net device start xmit */
71 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
72 {
73         struct tun_struct *tun = netdev_priv(dev);
74
75         DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
76
77         /* Drop packet if interface is not attached */
78         if (!tun->attached)
79                 goto drop;
80
81         /* Queue packet */
82         if (!(tun->flags & TUN_ONE_QUEUE)) {
83                 /* Normal queueing mode.
84                  * Packet scheduler handles dropping. */
85                 if (skb_queue_len(&tun->readq) >= TUN_READQ_SIZE)
86                         netif_stop_queue(dev);
87         } else {
88                 /* Single queue mode.
89                  * Driver handles dropping itself. */
90                 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len)
91                         goto drop;
92         }
93         skb_queue_tail(&tun->readq, skb);
94
95         /* Notify and wake up reader process */
96         if (tun->flags & TUN_FASYNC)
97                 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
98         wake_up_interruptible(&tun->read_wait);
99         return 0;
100
101 drop:
102         tun->stats.tx_dropped++;
103         kfree_skb(skb);
104         return 0;
105 }
106
107 static void tun_net_mclist(struct net_device *dev)
108 {
109         /* Nothing to do for multicast filters. 
110          * We always accept all frames. */
111         return;
112 }
113
114 static struct net_device_stats *tun_net_stats(struct net_device *dev)
115 {
116         struct tun_struct *tun = netdev_priv(dev);
117         return &tun->stats;
118 }
119
120 /* Initialize net device. */
121 static void tun_net_init(struct net_device *dev)
122 {
123         struct tun_struct *tun = netdev_priv(dev);
124    
125         switch (tun->flags & TUN_TYPE_MASK) {
126         case TUN_TUN_DEV:
127                 /* Point-to-Point TUN Device */
128                 dev->hard_header_len = 0;
129                 dev->addr_len = 0;
130                 dev->mtu = 1500;
131
132                 /* Zero header length */
133                 dev->type = ARPHRD_NONE; 
134                 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
135                 dev->tx_queue_len = 10;
136                 break;
137
138         case TUN_TAP_DEV:
139                 /* Ethernet TAP Device */
140                 dev->set_multicast_list = tun_net_mclist;
141
142                 /* Generate random Ethernet address.  */
143                 *(u16 *)dev->dev_addr = htons(0x00FF);
144                 get_random_bytes(dev->dev_addr + sizeof(u16), 4);
145
146                 ether_setup(dev);
147                 break;
148         }
149 }
150
151 /* Character device part */
152
153 /* Poll */
154 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
155 {  
156         struct tun_struct *tun = file->private_data;
157         unsigned int mask = POLLOUT | POLLWRNORM;
158
159         if (!tun)
160                 return -EBADFD;
161
162         DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
163
164         poll_wait(file, &tun->read_wait, wait);
165  
166         if (skb_queue_len(&tun->readq))
167                 mask |= POLLIN | POLLRDNORM;
168
169         return mask;
170 }
171
172 /* Get packet from user space buffer */
173 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
174 {
175         struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
176         struct sk_buff *skb;
177         size_t len = count;
178
179         if (!(tun->flags & TUN_NO_PI)) {
180                 if ((len -= sizeof(pi)) > len)
181                         return -EINVAL;
182
183                 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
184                         return -EFAULT;
185         }
186  
187         if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
188                 tun->stats.rx_dropped++;
189                 return -ENOMEM;
190         }
191
192         skb_reserve(skb, 2);
193         if (memcpy_fromiovec(skb_put(skb, len), iv, len))
194                 return -EFAULT;
195
196         skb->dev = tun->dev;
197         switch (tun->flags & TUN_TYPE_MASK) {
198         case TUN_TUN_DEV:
199                 skb->mac.raw = skb->data;
200                 skb->protocol = pi.proto;
201                 break;
202         case TUN_TAP_DEV:
203                 skb->protocol = eth_type_trans(skb, tun->dev);
204                 break;
205         };
206
207         if (tun->flags & TUN_NOCHECKSUM)
208                 skb->ip_summed = CHECKSUM_UNNECESSARY;
209  
210         netif_rx_ni(skb);
211    
212         tun->stats.rx_packets++;
213         tun->stats.rx_bytes += len;
214
215         return count;
216
217
218 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
219 {
220         unsigned long i;
221         size_t len;
222
223         for (i = 0, len = 0; i < count; i++) 
224                 len += iv[i].iov_len;
225
226         return len;
227 }
228
229 /* Writev */
230 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv, 
231                               unsigned long count, loff_t *pos)
232 {
233         struct tun_struct *tun = file->private_data;
234
235         if (!tun)
236                 return -EBADFD;
237
238         DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
239
240         return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
241 }
242
243 /* Write */
244 static ssize_t tun_chr_write(struct file * file, const char __user * buf, 
245                              size_t count, loff_t *pos)
246 {
247         struct iovec iv = { (void __user *) buf, count };
248         return tun_chr_writev(file, &iv, 1, pos);
249 }
250
251 /* Put packet to the user space buffer */
252 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
253                                        struct sk_buff *skb,
254                                        struct iovec *iv, int len)
255 {
256         struct tun_pi pi = { 0, skb->protocol };
257         ssize_t total = 0;
258
259         if (!(tun->flags & TUN_NO_PI)) {
260                 if ((len -= sizeof(pi)) < 0)
261                         return -EINVAL;
262
263                 if (len < skb->len) {
264                         /* Packet will be striped */
265                         pi.flags |= TUN_PKT_STRIP;
266                 }
267  
268                 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
269                         return -EFAULT;
270                 total += sizeof(pi);
271         }       
272
273         len = min_t(int, skb->len, len);
274
275         skb_copy_datagram_iovec(skb, 0, iv, len);
276         total += len;
277
278         tun->stats.tx_packets++;
279         tun->stats.tx_bytes += len;
280
281         return total;
282 }
283
284 /* Readv */
285 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
286                             unsigned long count, loff_t *pos)
287 {
288         struct tun_struct *tun = file->private_data;
289         DECLARE_WAITQUEUE(wait, current);
290         struct sk_buff *skb;
291         ssize_t len, ret = 0;
292
293         if (!tun)
294                 return -EBADFD;
295
296         DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
297
298         len = iov_total(iv, count);
299         if (len < 0)
300                 return -EINVAL;
301
302         add_wait_queue(&tun->read_wait, &wait);
303         while (len) {
304                 current->state = TASK_INTERRUPTIBLE;
305
306                 /* Read frames from the queue */
307                 if (!(skb=skb_dequeue(&tun->readq))) {
308                         if (file->f_flags & O_NONBLOCK) {
309                                 ret = -EAGAIN;
310                                 break;
311                         }
312                         if (signal_pending(current)) {
313                                 ret = -ERESTARTSYS;
314                                 break;
315                         }
316
317                         /* Nothing to read, let's sleep */
318                         schedule();
319                         continue;
320                 }
321                 netif_start_queue(tun->dev);
322
323                 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
324
325                 kfree_skb(skb);
326                 break;
327         }
328
329         current->state = TASK_RUNNING;
330         remove_wait_queue(&tun->read_wait, &wait);
331
332         return ret;
333 }
334
335 /* Read */
336 static ssize_t tun_chr_read(struct file * file, char __user * buf, 
337                             size_t count, loff_t *pos)
338 {
339         struct iovec iv = { buf, count };
340         return tun_chr_readv(file, &iv, 1, pos);
341 }
342
343 static void tun_setup(struct net_device *dev)
344 {
345         struct tun_struct *tun = netdev_priv(dev);
346
347         skb_queue_head_init(&tun->readq);
348         init_waitqueue_head(&tun->read_wait);
349
350         tun->owner = -1;
351
352         SET_MODULE_OWNER(dev);
353         dev->open = tun_net_open;
354         dev->hard_start_xmit = tun_net_xmit;
355         dev->stop = tun_net_close;
356         dev->get_stats = tun_net_stats;
357         dev->destructor = free_netdev;
358 }
359
360 static struct tun_struct *tun_get_by_name(const char *name)
361 {
362         struct tun_struct *tun;
363
364         ASSERT_RTNL();
365         list_for_each_entry(tun, &tun_dev_list, list) {
366                 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
367                     return tun;
368         }
369
370         return NULL;
371 }
372
373 static int tun_set_iff(struct file *file, struct ifreq *ifr)
374 {
375         struct tun_struct *tun;
376         struct net_device *dev;
377         int err;
378
379         tun = tun_get_by_name(ifr->ifr_name);
380         if (tun) {
381                 if (tun->attached)
382                         return -EBUSY;
383
384                 /* Check permissions */
385                 if (tun->owner != -1 &&
386                     current->euid != tun->owner && !capable(CAP_NET_ADMIN))
387                         return -EPERM;
388         } 
389         else if (__dev_get_by_name(ifr->ifr_name)) 
390                 return -EINVAL;
391         else {
392                 char *name;
393                 unsigned long flags = 0;
394
395                 err = -EINVAL;
396
397                 /* Set dev type */
398                 if (ifr->ifr_flags & IFF_TUN) {
399                         /* TUN device */
400                         flags |= TUN_TUN_DEV;
401                         name = "tun%d";
402                 } else if (ifr->ifr_flags & IFF_TAP) {
403                         /* TAP device */
404                         flags |= TUN_TAP_DEV;
405                         name = "tap%d";
406                 } else 
407                         goto failed;
408    
409                 if (*ifr->ifr_name)
410                         name = ifr->ifr_name;
411
412                 dev = alloc_netdev(sizeof(struct tun_struct), name,
413                                    tun_setup);
414                 if (!dev)
415                         return -ENOMEM;
416
417                 tun = netdev_priv(dev);
418                 tun->dev = dev;
419                 tun->flags = flags;
420
421                 tun_net_init(dev);
422
423                 if (strchr(dev->name, '%')) {
424                         err = dev_alloc_name(dev, dev->name);
425                         if (err < 0)
426                                 goto err_free_dev;
427                 }
428
429                 err = register_netdevice(tun->dev);
430                 if (err < 0)
431                         goto err_free_dev;
432         
433                 list_add(&tun->list, &tun_dev_list);
434         }
435
436         DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
437
438         if (ifr->ifr_flags & IFF_NO_PI)
439                 tun->flags |= TUN_NO_PI;
440
441         if (ifr->ifr_flags & IFF_ONE_QUEUE)
442                 tun->flags |= TUN_ONE_QUEUE;
443
444         file->private_data = tun;
445         tun->attached = 1;
446
447         strcpy(ifr->ifr_name, tun->dev->name);
448         return 0;
449
450  err_free_dev:
451         free_netdev(dev);
452  failed:
453         return err;
454 }
455
456 static int tun_chr_ioctl(struct inode *inode, struct file *file, 
457                          unsigned int cmd, unsigned long arg)
458 {
459         struct tun_struct *tun = file->private_data;
460
461         if (cmd == TUNSETIFF && !tun) {
462                 struct ifreq ifr;
463                 int err;
464
465                 if (copy_from_user(&ifr, (void __user *)arg, sizeof(ifr)))
466                         return -EFAULT;
467                 ifr.ifr_name[IFNAMSIZ-1] = '\0';
468
469                 rtnl_lock();
470                 err = tun_set_iff(file, &ifr);
471                 rtnl_unlock();
472
473                 if (err)
474                         return err;
475
476                 if (copy_to_user((void __user *)arg, &ifr, sizeof(ifr)))
477                         return -EFAULT;
478                 return 0;
479         }
480
481         if (!tun)
482                 return -EBADFD;
483
484         DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
485
486         switch (cmd) {
487         case TUNSETNOCSUM:
488                 /* Disable/Enable checksum */
489                 if (arg)
490                         tun->flags |= TUN_NOCHECKSUM;
491                 else
492                         tun->flags &= ~TUN_NOCHECKSUM;
493
494                 DBG(KERN_INFO "%s: checksum %s\n",
495                     tun->dev->name, arg ? "disabled" : "enabled");
496                 break;
497
498         case TUNSETPERSIST:
499                 /* Disable/Enable persist mode */
500                 if (arg)
501                         tun->flags |= TUN_PERSIST;
502                 else
503                         tun->flags &= ~TUN_PERSIST;
504
505                 DBG(KERN_INFO "%s: persist %s\n",
506                     tun->dev->name, arg ? "disabled" : "enabled");
507                 break;
508
509         case TUNSETOWNER:
510                 /* Set owner of the device */
511                 tun->owner = (uid_t) arg;
512
513                 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
514                 break;
515
516 #ifdef TUN_DEBUG
517         case TUNSETDEBUG:
518                 tun->debug = arg;
519                 break;
520 #endif
521
522         default:
523                 return -EINVAL;
524         };
525
526         return 0;
527 }
528
529 static int tun_chr_fasync(int fd, struct file *file, int on)
530 {
531         struct tun_struct *tun = file->private_data;
532         int ret;
533
534         if (!tun)
535                 return -EBADFD;
536
537         DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
538
539         if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
540                 return ret; 
541  
542         if (on) {
543                 ret = f_setown(file, current->pid, 0);
544                 if (ret)
545                         return ret;
546                 tun->flags |= TUN_FASYNC;
547         } else 
548                 tun->flags &= ~TUN_FASYNC;
549
550         return 0;
551 }
552
553 static int tun_chr_open(struct inode *inode, struct file * file)
554 {
555         DBG1(KERN_INFO "tunX: tun_chr_open\n");
556         file->private_data = NULL;
557         return 0;
558 }
559
560 static int tun_chr_close(struct inode *inode, struct file *file)
561 {
562         struct tun_struct *tun = file->private_data;
563
564         if (!tun)
565                 return 0;
566
567         DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
568
569         tun_chr_fasync(-1, file, 0);
570
571         rtnl_lock();
572
573         /* Detach from net device */
574         file->private_data = NULL;
575         tun->attached = 0;
576
577         /* Drop read queue */
578         skb_queue_purge(&tun->readq);
579
580         if (!(tun->flags & TUN_PERSIST)) {
581                 list_del(&tun->list);
582                 unregister_netdevice(tun->dev);
583         }
584
585         rtnl_unlock();
586
587         return 0;
588 }
589
590 static struct file_operations tun_fops = {
591         .owner  = THIS_MODULE,  
592         .llseek = no_llseek,
593         .read   = tun_chr_read,
594         .readv  = tun_chr_readv,
595         .write  = tun_chr_write,
596         .writev = tun_chr_writev,
597         .poll   = tun_chr_poll,
598         .ioctl  = tun_chr_ioctl,
599         .open   = tun_chr_open,
600         .release = tun_chr_close,
601         .fasync = tun_chr_fasync                
602 };
603
604 static struct miscdevice tun_miscdev = {
605         .minor = TUN_MINOR,
606         .name = "tun",
607         .fops = &tun_fops,
608         .devfs_name = "net/tun",
609 };
610
611 int __init tun_init(void)
612 {
613         int ret = 0;
614
615         printk(KERN_INFO "Universal TUN/TAP device driver %s " 
616                "(C)1999-2002 Maxim Krasnyansky\n", TUN_VER);
617
618         ret = misc_register(&tun_miscdev);
619         if (ret)
620                 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
621         return ret;
622 }
623
624 void tun_cleanup(void)
625 {
626         struct tun_struct *tun, *nxt;
627
628         misc_deregister(&tun_miscdev);  
629
630         rtnl_lock();
631         list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
632                 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
633                 unregister_netdevice(tun->dev);
634         }
635         rtnl_unlock();
636         
637 }
638
639 module_init(tun_init);
640 module_exit(tun_cleanup);
641 MODULE_LICENSE("GPL");
642 MODULE_ALIAS_MISCDEV(TUN_MINOR);