85cdc01fb672b840f75ca95d4d3a377f2b2d18a3
[debian/uanytun.git] / src / linux / tun.c
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methodes used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *  
17  *
18  *  Copyright (C) 2007-2010 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation, either version 3 of the License, or
25  *  any later version.
26  *
27  *  uAnytun is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
34  */
35
36 #include <stdio.h>
37
38 #include "datatypes.h"
39
40 #include "tun.h"
41
42 #include "tun_helper.h"
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <sys/wait.h>
47 #include <fcntl.h>
48 #include <sys/ioctl.h>
49 #include <arpa/inet.h>
50 #include <errno.h>
51 #include <net/if.h>
52 #include <linux/ip.h>
53 #include <linux/if_ether.h>
54 #include <linux/if_tun.h>
55 #define DEFAULT_DEVICE "/dev/net/tun"
56
57 #include "log.h"
58 #include "sysexec.h"
59
60 int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix){
61   if(!dev) 
62     return -1;
63  
64   tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400);
65   dev->actual_name_ = NULL;
66
67         dev->fd_ = open(DEFAULT_DEVICE, O_RDWR);
68         if(dev->fd_ < 0) {
69     log_printf(ERROR, "can't open device file (%s): %s", DEFAULT_DEVICE, strerror(errno));
70     tun_close(dev);
71     return -1;
72   }
73
74         struct ifreq ifr;
75         memset(&ifr, 0, sizeof(ifr));
76
77   if(dev->type_ == TYPE_TUN) {
78     ifr.ifr_flags = IFF_TUN;
79     dev->with_pi_ = 1;
80   } 
81   else if(dev->type_ == TYPE_TAP) {
82     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
83     dev->with_pi_ = 0;
84   } 
85   else {
86     log_printf(ERROR, "unable to recognize type of device (tun or tap)");
87     tun_close(dev);
88     return -1;
89   }
90
91         if(dev_name)
92                 strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
93
94         if(!ioctl(dev->fd_, TUNSETIFF, &ifr)) {
95                 dev->actual_name_ = strdup(ifr.ifr_name);
96         } else if(!ioctl(dev->fd_, (('T' << 8) | 202), &ifr)) {
97                 dev->actual_name_ = strdup(ifr.ifr_name);
98         } else {
99     log_printf(ERROR, "tun/tap device ioctl failed: %s", strerror(errno));
100     tun_close(dev);
101     return -1;
102   }
103
104   if(!dev->actual_name_) {
105     log_printf(ERROR, "can't open device file: memory error");
106     tun_close(dev);
107     return -2;
108   }
109
110   if(ifcfg_addr)
111     tun_do_ifconfig(dev);
112
113   return 0;
114 }
115
116 int tun_init_post(tun_device_t* dev)
117 {
118 // nothing yet
119 }
120
121 void tun_close(tun_device_t* dev)
122 {
123   if(!dev)
124     return;
125
126   if(dev->fd_ > 0)
127     close(dev->fd_);
128
129   if(dev->actual_name_)
130     free(dev->actual_name_);
131
132   if(dev->net_addr_)
133     free(dev->net_addr_);
134
135   if(dev->net_mask_)
136     free(dev->net_mask_);
137 }
138
139 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
140 {
141   if(!dev || dev->fd_ < 0)
142     return -1;
143
144   if(dev->with_pi_)
145   {
146     struct iovec iov[2];
147     struct tun_pi tpi;
148     
149     iov[0].iov_base = &tpi;
150     iov[0].iov_len = sizeof(tpi);
151     iov[1].iov_base = buf;
152     iov[1].iov_len = len;
153     return(tun_fix_return(readv(dev->fd_, iov, 2), sizeof(tpi)));
154   }
155   else
156     return(read(dev->fd_, buf, len));
157 }
158
159 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
160 {
161   if(!dev || dev->fd_ < 0)
162     return -1;
163
164   if(!buf)
165     return 0;
166
167   if(dev->with_pi_)
168   {
169     struct iovec iov[2];
170     struct tun_pi tpi;
171     struct iphdr *hdr = (struct iphdr *)buf;
172     
173     tpi.flags = 0;
174     if(hdr->version == 4)
175       tpi.proto = htons(ETH_P_IP);
176     else
177       tpi.proto = htons(ETH_P_IPV6);
178     
179     iov[0].iov_base = &tpi;
180     iov[0].iov_len = sizeof(tpi);
181     iov[1].iov_base = buf;
182     iov[1].iov_len = len;
183     return(tun_fix_return(writev(dev->fd_, iov, 2), sizeof(tpi)));
184   }
185   else
186     return(write(dev->fd_, buf, len));
187 }
188
189 void tun_do_ifconfig(tun_device_t* dev)
190 {
191   if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
192     return;
193
194   char* mtu_str = NULL;
195   asprintf(&mtu_str, "%d", dev->mtu_);
196   if(!mtu_str) {
197     log_printf(ERROR, "Execution of ifconfig failed");
198     return;
199   }
200
201   char* const argv[] = { "/sbin/ifconfig", dev->actual_name_, dev->net_addr_, "netmask", dev->net_mask_, "mtu", mtu_str, NULL };
202   char* const evp[] = { NULL };
203   uanytun_exec("/sbin/ifconfig", argv, evp);
204
205   free(mtu_str);
206 }