~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/net/tun.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  TUN - Universal TUN/TAP device driver.
  3  *  Copyright (C) 1999-2000 Maxim Krasnyansky <max_mk@yahoo.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.3 2000/10/23 10:01:25 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.3"
 24 
 25 #include <linux/module.h>
 26 
 27 #include <linux/errno.h>
 28 #include <linux/kernel.h>
 29 #include <linux/major.h>
 30 #include <linux/sched.h>
 31 #include <linux/malloc.h>
 32 #include <linux/poll.h>
 33 #include <linux/fcntl.h>
 34 #include <linux/init.h>
 35 #include <linux/random.h>
 36 
 37 #include <linux/skbuff.h>
 38 #include <linux/netdevice.h>
 39 #include <linux/etherdevice.h>
 40 #include <linux/miscdevice.h>
 41 #include <linux/rtnetlink.h>
 42 #include <linux/if.h>
 43 #include <linux/if_arp.h>
 44 #include <linux/if_ether.h>
 45 #include <linux/if_tun.h>
 46 
 47 #include <asm/system.h>
 48 #include <asm/uaccess.h>
 49 
 50 #ifdef TUN_DEBUG
 51 static int debug=0;
 52 #endif
 53 
 54 /* Network device part of the driver */
 55 
 56 /* Net device open. */
 57 static int tun_net_open(struct net_device *dev)
 58 {
 59 #ifdef TUN_DEBUG  
 60         struct tun_struct *tun = (struct tun_struct *)dev->priv;
 61 
 62         DBG(KERN_INFO "%s: tun_net_open\n", tun->name);
 63 #endif
 64 
 65         netif_start_queue(dev);
 66 
 67         return 0;
 68 }
 69 
 70 /* Net device close. */
 71 static int tun_net_close(struct net_device *dev)
 72 {
 73 #ifdef TUN_DEBUG  
 74         struct tun_struct *tun = (struct tun_struct *)dev->priv;
 75 
 76         DBG(KERN_INFO "%s: tun_net_close\n", tun->name);
 77 #endif
 78 
 79         netif_stop_queue(dev);
 80 
 81         return 0;
 82 }
 83 
 84 /* Net device start xmit */
 85 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
 86 {
 87         struct tun_struct *tun = (struct tun_struct *)dev->priv;
 88 
 89         DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->name, skb->len);
 90 
 91         /* Queue frame */
 92         skb_queue_tail(&tun->txq, skb);
 93         if (skb_queue_len(&tun->txq) >= TUN_TXQ_SIZE)
 94                 netif_stop_queue(dev);
 95 
 96         if (tun->flags & TUN_FASYNC)
 97                 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
 98 
 99         /* Wake up process */ 
100         wake_up_interruptible(&tun->read_wait);
101 
102         return 0;
103 }
104 
105 static void tun_net_mclist(struct net_device *dev)
106 {
107 #ifdef TUN_DEBUG
108         struct tun_struct *tun = (struct tun_struct *)dev->priv;
109 
110         DBG(KERN_INFO "%s: tun_net_mclist\n", tun->name);
111 #endif
112 
113         /* Nothing to do for multicast filters. 
114          * We always accept all frames.
115          */
116         return;
117 }
118 
119 static struct net_device_stats *tun_net_stats(struct net_device *dev)
120 {
121         struct tun_struct *tun = (struct tun_struct *)dev->priv;
122 
123         return &tun->stats;
124 }
125 
126 /* Initialize net device. */
127 int tun_net_init(struct net_device *dev)
128 {
129         struct tun_struct *tun = (struct tun_struct *)dev->priv;
130    
131         DBG(KERN_INFO "%s: tun_net_init\n", tun->name);
132 
133         SET_MODULE_OWNER(dev);
134         dev->open = tun_net_open;
135         dev->hard_start_xmit = tun_net_xmit;
136         dev->stop = tun_net_close;
137         dev->get_stats = tun_net_stats;
138 
139         switch (tun->flags & TUN_TYPE_MASK) {
140         case TUN_TUN_DEV:
141                 /* Point-to-Point TUN Device */
142                 dev->hard_header_len = 0;
143                 dev->addr_len = 0;
144                 dev->mtu = 1500;
145 
146                 /* Type PPP seems most suitable */
147                 dev->type = ARPHRD_PPP; 
148                 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
149                 dev->tx_queue_len = 10;
150 
151                 dev_init_buffers(dev);
152                 break;
153 
154         case TUN_TAP_DEV:
155                 /* Ethernet TAP Device */
156                 dev->set_multicast_list = tun_net_mclist;
157 
158                 /* Generate random Ethernet address.  */
159                 *(u16 *)dev->dev_addr = htons(0x00FF);
160                 get_random_bytes(dev->dev_addr + sizeof(u16), 4);
161 
162                 ether_setup(dev);
163                 break;
164         };
165 
166         return 0;
167 }
168 
169 /* Character device part */
170 
171 /* Poll */
172 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
173 {  
174         struct tun_struct *tun = (struct tun_struct *)file->private_data;
175 
176         DBG(KERN_INFO "%s: tun_chr_poll\n", tun->name);
177 
178         poll_wait(file, &tun->read_wait, wait);
179  
180         if (skb_queue_len(&tun->txq))
181                 return POLLIN | POLLRDNORM;
182 
183         return POLLOUT | POLLWRNORM;
184 }
185 
186 /* Get packet from user space buffer(already verified) */
187 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, const char *buf, size_t count)
188 {
189         struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
190         register const char *ptr = buf; 
191         register int len = count;
192         struct sk_buff *skb;
193 
194         if (len > TUN_MAX_FRAME)
195                 return -EINVAL;
196 
197         if (!(tun->flags & TUN_NO_PI)) {
198                 if ((len -= sizeof(pi)) < 0)
199                         return -EINVAL;
200 
201                 copy_from_user(&pi, ptr, sizeof(pi));
202                 ptr += sizeof(pi);
203         }
204  
205         if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
206                 tun->stats.rx_dropped++;
207                 return -ENOMEM;
208         }
209 
210         skb_reserve(skb, 2);
211         copy_from_user(skb_put(skb, len), ptr, len); 
212 
213         skb->dev = &tun->dev;
214         switch (tun->flags & TUN_TYPE_MASK) {
215         case TUN_TUN_DEV:
216                 skb->mac.raw = skb->data;
217                 skb->protocol = pi.proto;
218                 break;
219         case TUN_TAP_DEV:
220                 skb->protocol = eth_type_trans(skb, &tun->dev);
221                 break;
222         };
223 
224         if (tun->flags & TUN_NOCHECKSUM)
225                 skb->ip_summed = CHECKSUM_UNNECESSARY;
226  
227         netif_rx(skb);
228    
229         tun->stats.rx_packets++;
230         tun->stats.rx_bytes += len;
231 
232         return count;
233 } 
234 
235 /* Write */
236 static ssize_t tun_chr_write(struct file * file, const char * buf, 
237                              size_t count, loff_t *pos)
238 {
239         struct tun_struct *tun = (struct tun_struct *)file->private_data;
240 
241         DBG(KERN_INFO "%s: tun_chr_write %d\n", tun->name, count);
242 
243         if (!(tun->flags & TUN_IFF_SET))
244                 return -EBUSY;
245 
246         if (verify_area(VERIFY_READ, buf, count))
247                 return -EFAULT;
248 
249         return tun_get_user(tun, buf, count);
250 }
251 
252 /* Put packet to user space buffer(already verified) */
253 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
254                                        struct sk_buff *skb,
255                                        char *buf, int count)
256 {
257         struct tun_pi pi = { 0, skb->protocol };
258         int len = count, total = 0;
259         char *ptr = buf;
260 
261         if (!(tun->flags & TUN_NO_PI)) {
262                 if ((len -= sizeof(pi)) < 0)
263                         return -EINVAL;
264 
265                 if (len < skb->len) {
266                         /* Packet will be striped */
267                         pi.flags |= TUN_PKT_STRIP;
268                 }
269  
270                 copy_to_user(ptr, &pi, sizeof(pi));
271 
272                 total += sizeof(pi);
273                 ptr += sizeof(pi);
274         }       
275 
276         len = MIN(skb->len, len); 
277         copy_to_user(ptr, skb->data, len); 
278         total += len;
279 
280         tun->stats.tx_packets++;
281         tun->stats.tx_bytes += len;
282 
283         return total;
284 }
285 
286 /* Read */
287 static ssize_t tun_chr_read(struct file * file, char * buf, 
288                             size_t count, loff_t *pos)
289 {
290         struct tun_struct *tun = (struct tun_struct *)file->private_data;
291         DECLARE_WAITQUEUE(wait, current);
292         struct sk_buff *skb;
293         ssize_t ret = 0;
294 
295         DBG(KERN_INFO "%s: tun_chr_read\n", tun->name);
296 
297         add_wait_queue(&tun->read_wait, &wait);
298         while (count) {
299                 current->state = TASK_INTERRUPTIBLE;
300 
301                 /* Read frames from device queue */
302                 if (!(skb=skb_dequeue(&tun->txq))) {
303                         if (file->f_flags & O_NONBLOCK) {
304                                 ret = -EAGAIN;
305                                 break;
306                         }
307                         if (signal_pending(current)) {
308                                 ret = -ERESTARTSYS;
309                                 break;
310                         }
311 
312                         /* Nothing to read, let's sleep */
313                         schedule();
314                         continue;
315                 }
316                 netif_start_queue(&tun->dev);
317 
318                 if (!verify_area(VERIFY_WRITE, buf, count))
319                         ret = tun_put_user(tun, skb, buf, count);
320                 else
321                         ret = -EFAULT;
322 
323                 kfree_skb(skb);
324                 break;
325         }
326 
327         current->state = TASK_RUNNING;
328         remove_wait_queue(&tun->read_wait, &wait);
329 
330         return ret;
331 }
332 
333 static loff_t tun_chr_lseek(struct file * file, loff_t offset, int origin)
334 {
335         return -ESPIPE;
336 }
337 
338 static int tun_set_iff(struct tun_struct *tun, unsigned long arg)
339 {
340         struct ifreq ifr;
341         char *mask;
342 
343         if (copy_from_user(&ifr, (void *)arg, sizeof(ifr)))
344                 return -EFAULT;
345         ifr.ifr_name[IFNAMSIZ-1] = '\0';
346 
347         if (tun->flags & TUN_IFF_SET)
348                 return -EEXIST;
349 
350         /* Set dev type */
351         if (ifr.ifr_flags & IFF_TUN) {
352                 /* TUN device */
353                 tun->flags |= TUN_TUN_DEV;
354                 mask = "tun%d";
355         } else if (ifr.ifr_flags & IFF_TAP) {
356                 /* TAP device */
357                 tun->flags |= TUN_TAP_DEV;
358                 mask = "tap%d";
359         } else 
360                 return -EINVAL;
361    
362         if (ifr.ifr_flags & IFF_NO_PI)
363                 tun->flags |= TUN_NO_PI;
364 
365         if (*ifr.ifr_name)
366                 strcpy(tun->dev.name, ifr.ifr_name);
367         else
368                 strcpy(tun->dev.name, mask);
369 
370         /* Register net device */
371         if (register_netdev(&tun->dev))
372                 return -EBUSY;
373 
374         tun->flags |= TUN_IFF_SET;
375         strcpy(tun->name, tun->dev.name);
376 
377         /* Return iface info to the user space */
378         strcpy(ifr.ifr_name, tun->dev.name);
379         copy_to_user((void *)arg, &ifr, sizeof(ifr));
380 
381         return 0;   
382 }
383 
384 static int tun_chr_ioctl(struct inode *inode, struct file *file, 
385                          unsigned int cmd, unsigned long arg)
386 {
387         struct tun_struct *tun = (struct tun_struct *)file->private_data;
388 
389         DBG(KERN_INFO "%s: tun_chr_ioctl\n", tun->name);
390 
391         switch (cmd) {
392         case TUNSETIFF:
393                 return tun_set_iff(tun, arg); 
394 
395         case TUNSETNOCSUM:
396                 /* Disable/Enable checksum on net iface */
397                 if (arg)
398                         tun->flags |= TUN_NOCHECKSUM;
399                 else
400                         tun->flags &= ~TUN_NOCHECKSUM;
401 
402                 DBG(KERN_INFO "%s: checksum %s\n",
403                     tun->name, arg ? "disabled" : "enabled");
404                 break;
405 
406 #ifdef TUN_DEBUG
407         case TUNSETDEBUG:
408                 tun->debug = arg;
409                 break;
410 #endif
411 
412         default:
413                 return -EINVAL;
414         };
415 
416         return 0;
417 }
418 
419 static int tun_chr_fasync(int fd, struct file *file, int on)
420 {
421         struct tun_struct *tun = (struct tun_struct *)file->private_data;
422         int ret;
423 
424         DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->name, on);
425 
426         if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
427                 return ret; 
428  
429         if (on)
430                 tun->flags |= TUN_FASYNC;
431         else 
432                 tun->flags &= ~TUN_FASYNC;
433 
434         return 0;
435 }
436 
437 static int tun_chr_open(struct inode *inode, struct file * file)
438 {
439         struct tun_struct *tun = NULL; 
440 
441         DBG1(KERN_INFO "tunX: tun_chr_open\n");
442 
443         tun = kmalloc(sizeof(struct tun_struct), GFP_KERNEL);
444         if (tun == NULL)
445                 return -ENOMEM;
446 
447         memset(tun, 0, sizeof(struct tun_struct));
448         file->private_data = tun;
449 
450         skb_queue_head_init(&tun->txq);
451         init_waitqueue_head(&tun->read_wait);
452 
453         sprintf(tun->name, "tunX");
454 
455         tun->dev.init = tun_net_init;
456         tun->dev.priv = tun;
457 
458         return 0;
459 }
460 
461 static int tun_chr_close(struct inode *inode, struct file *file)
462 {
463         struct tun_struct *tun = (struct tun_struct *)file->private_data;
464 
465         DBG(KERN_INFO "%s: tun_chr_close\n", tun->name);
466 
467         if (tun->flags & TUN_IFF_SET) {
468                 rtnl_lock();
469                 dev_close(&tun->dev);
470                 rtnl_unlock();
471 
472                 /* Drop TX queue */
473                 skb_queue_purge(&tun->txq);
474 
475                 unregister_netdev(&tun->dev);
476         }
477 
478         kfree(tun);
479         file->private_data = NULL;
480 
481         return 0;
482 }
483 
484 static struct file_operations tun_fops = {
485         owner:  THIS_MODULE,    
486         llseek: tun_chr_lseek,
487         read:   tun_chr_read,
488         write:  tun_chr_write,
489         poll:   tun_chr_poll,
490         ioctl:  tun_chr_ioctl,
491         open:   tun_chr_open,
492         release:tun_chr_close,
493         fasync: tun_chr_fasync          
494 };
495 
496 static struct miscdevice tun_miscdev=
497 {
498         TUN_MINOR,
499         "net/tun",
500         &tun_fops
501 };
502 
503 int __init tun_init(void)
504 {
505         printk(KERN_INFO "Universal TUN/TAP device driver %s " 
506                "(C)1999-2000 Maxim Krasnyansky\n", TUN_VER);
507 
508         if (misc_register(&tun_miscdev)) {
509                 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
510                 return -EIO;
511         }
512 
513         return 0;
514 }
515 
516 void tun_cleanup(void)
517 {
518         misc_deregister(&tun_miscdev);  
519 }
520 
521 module_init(tun_init);
522 module_exit(tun_cleanup);
523 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.