1 /*
2 VTun - Virtual Tunnel over TCP/IP network.
3
4 Copyright (C) 1998-2016 Maxim Krasnyansky <max_mk@yahoo.com>
5
6 VTun has been derived from VPPP package by Maxim Krasnyansky.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 */
18
19 /*
20 * $Id: tun_dev.c,v 1.6.2.3 2016/10/01 21:46:01 mtbishop Exp $
21 */
22
23 #include "config.h"
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <net/if_tun.h>
37
38 #include "vtun.h"
39 #include "lib.h"
40
41 /*
42 * Allocate TUN device, returns opened fd.
43 * Stores dev name in the first arg(must be large enough).
44 */
tun_open(char * dev)45 int tun_open(char *dev)
46 {
47 char tunname[14];
48 int i, fd = -1;
49
50 if( *dev ){
51 sprintf(tunname, "/dev/%s", dev);
52 fd = open(tunname, O_RDWR);
53 } else {
54 for(i=0; i < 255; i++){
55 sprintf(tunname, "/dev/tun%d", i);
56 /* Open device */
57 if( (fd=open(tunname, O_RDWR)) > 0 ){
58 sprintf(dev, "tun%d", i);
59 break;
60 }
61 }
62 }
63 return fd;
64 }
65
tun_close(int fd,char * dev)66 int tun_close(int fd, char *dev)
67 {
68 return close(fd);
69 }
70
71 /* Read/write frames from TUN device */
tun_write(int fd,char * buf,int len)72 int tun_write(int fd, char *buf, int len)
73 {
74 u_int32_t type = htonl(AF_INET);
75 struct iovec iv[2];
76
77 iv[0].iov_base = &type;
78 iv[0].iov_len = sizeof(type);
79 iv[1].iov_base = buf;
80 iv[1].iov_len = len;
81
82 return writev(fd, iv, 2);
83 }
84
tun_read(int fd,char * buf,int len)85 int tun_read(int fd, char *buf, int len)
86 {
87 struct iovec iv[2];
88 u_int32_t type;
89 register int rlen;
90
91 iv[0].iov_base = &type;
92 iv[0].iov_len = sizeof(type);
93 iv[1].iov_base = buf;
94 iv[1].iov_len = len;
95
96 if( (rlen = readv(fd, iv, 2)) > 0 )
97 return rlen - sizeof(type);
98 else
99 return rlen;
100 }
101