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