Add debian/source/format file, 3.0 (quilt)
[debian/uanytun.git] / src / bsd / 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 methods 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-2014 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  *  In addition, as a special exception, the copyright holders give
36  *  permission to link the code of portions of this program with the
37  *  OpenSSL library under certain conditions as described in each
38  *  individual source file, and distribute linked combinations
39  *  including the two.
40  *  You must obey the GNU General Public License in all respects
41  *  for all of the code used other than OpenSSL.  If you modify
42  *  file(s) with this exception, you may extend this exception to your
43  *  version of the file(s), but you are not obligated to do so.  If you
44  *  do not wish to do so, delete this exception statement from your
45  *  version.  If you delete this exception statement from all source
46  *  files in the program, then also delete it here.
47  */
48
49 #include "datatypes.h"
50
51 #include "tun.h"
52
53 #include "tun_helper.h"
54
55 #include "log.h"
56
57 #include <fcntl.h>
58 #include <unistd.h>
59 #include <errno.h>
60 #include <sys/wait.h>
61 #include <sys/socket.h>
62 #include <net/if.h>
63 #include <net/if_tun.h>
64 #include <sys/ioctl.h>
65 #include <sys/types.h>
66 #include <sys/uio.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in.h>
69 #include <netinet/ip.h>
70 #define DEVICE_FILE_MAX 255
71
72 int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix)
73 {
74   if(!dev)
75     return -1;
76
77   tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400);
78   dev->actual_name_ = NULL;
79
80   char* device_file = NULL;
81   char* actual_name_start = NULL;
82   int dynamic = 1;
83   if(dev_name) {
84     asprintf(&device_file, "/dev/%s", dev_name);
85     dynamic = 0;
86   }
87 #if defined(__GNUC__) && defined(__OpenBSD__)
88   else if(dev->type_ == TYPE_TUN || dev->type_ == TYPE_TAP) {
89     asprintf(&device_file, "/dev/tun");
90     actual_name_start = "tun";
91   }
92 #else
93   else if(dev->type_ == TYPE_TUN) {
94     asprintf(&device_file, "/dev/tun");
95     actual_name_start = "tun";
96   }
97   else if(dev->type_ == TYPE_TAP) {
98     asprintf(&device_file, "/dev/tap");
99     actual_name_start = "tap";
100   }
101 #endif
102   else {
103     log_printf(ERROR, "unable to recognize type of device (tun or tap)");
104     tun_close(dev);
105     return -1;
106   }
107   if(!device_file) {
108     log_printf(ERROR, "can't open device file: memory error");
109     tun_close(dev);
110     return -2;
111   }
112
113   u_int32_t dev_id=0;
114   if(dynamic) {
115     for(; dev_id <= DEVICE_FILE_MAX; ++dev_id) {
116       char* device_file_tmp = NULL;
117       asprintf(&device_file_tmp, "%s%d", device_file, dev_id);
118
119       if(!device_file_tmp) {
120         log_printf(ERROR, "can't open device file: memory error");
121         free(device_file);
122         tun_close(dev);
123         return -2;
124       }
125
126       dev->fd_ = open(device_file_tmp, O_RDWR);
127       free(device_file_tmp);
128       if(dev->fd_ >= 0)
129         break;
130     }
131   }
132   else
133     dev->fd_ = open(device_file, O_RDWR);
134   free(device_file);
135
136   if(dev->fd_ < 0) {
137     if(dynamic)
138       log_printf(ERROR, "can't open device file dynamically: no unused node left");
139     else
140       log_printf(ERROR, "can't open device file (%s): %s", device_file, strerror(errno));
141
142     tun_close(dev);
143     return -1;
144   }
145
146   if(dynamic)
147     asprintf(&(dev->actual_name_), "%s%d", actual_name_start, dev_id);
148   else
149     dev->actual_name_ = strdup(dev_name);
150
151   if(!dev->actual_name_) {
152     log_printf(ERROR, "can't open device file: memory error");
153     tun_close(dev);
154     return -2;
155   }
156
157   int ret = tun_init_post(dev);
158   if(ret) {
159     tun_close(dev);
160     return ret;
161   }
162
163   if(ifcfg_addr)
164     tun_do_ifconfig(dev);
165
166   return 0;
167 }
168
169
170 #if defined(__GNUC__) && defined(__OpenBSD__)
171
172 int tun_init_post(tun_device_t* dev)
173 {
174   if(!dev)
175     return -1;
176
177   dev->with_pi_ = 1;
178   if(dev->type_ == TYPE_TAP)
179     dev->with_pi_ = 0;
180
181   struct tuninfo ti;
182
183   if(ioctl(dev->fd_, TUNGIFINFO, &ti) < 0) {
184     log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
185     return -1;
186   }
187
188   ti.flags |= IFF_MULTICAST;
189   if(dev->type_ == TYPE_TUN)
190     ti.flags &= ~IFF_POINTOPOINT;
191
192   if(ioctl(dev->fd_, TUNSIFINFO, &ti) < 0) {
193     log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
194     return -1;
195   }
196   return 0;
197 }
198
199 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
200
201 int tun_init_post(tun_device_t* dev)
202 {
203   if(!dev)
204     return -1;
205
206   dev->with_pi_ = 1;
207   if(dev->type_ == TYPE_TAP)
208     dev->with_pi_ = 0;
209
210   if(dev->type_ == TYPE_TUN) {
211     int arg = 0;
212     if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
213       log_printf(ERROR, "can't disable link-layer mode for interface: %s", strerror(errno));
214       return -1;
215     }
216
217     arg = 1;
218     if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
219       log_printf(ERROR, "can't enable multi-af mode for interface: %s", strerror(errno));
220       return -1;
221     }
222
223     arg = IFF_BROADCAST;
224     arg |= IFF_MULTICAST;
225     if(ioctl(dev->fd_, TUNSIFMODE, &arg) < 0) {
226       log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
227       return -1;
228     }
229   }
230
231   return 0;
232 }
233
234 #elif defined(__GNUC__) && defined(__NetBSD__)
235  #warning this device has never been tested on NetBSD and might not work
236 int tun_init_post(tun_device_t* dev)
237 {
238   if(!dev)
239     return -1;
240
241   dev->with_pi_ = 0;
242
243   int arg = IFF_POINTOPOINT|IFF_MULTICAST;
244   ioctl(dev->fd_, TUNSIFMODE, &arg);
245   arg = 0;
246   ioctl(dev->fd_, TUNSLMODE, &arg);
247
248   return 0;
249 }
250
251 #else
252  #error This Device works just for OpenBSD, FreeBSD or NetBSD
253 #endif
254
255
256
257 void tun_close(tun_device_t* dev)
258 {
259   if(!dev)
260     return;
261
262   if(dev->fd_ > 0)
263     close(dev->fd_);
264
265   if(dev->actual_name_)
266     free(dev->actual_name_);
267
268   if(dev->net_addr_)
269     free(dev->net_addr_);
270
271   if(dev->net_mask_)
272     free(dev->net_mask_);
273 }
274
275 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
276 {
277   if(!dev || dev->fd_ < 0)
278     return -1;
279
280   if(dev->with_pi_)
281   {
282     struct iovec iov[2];
283     u_int32_t type;
284
285     iov[0].iov_base = &type;
286     iov[0].iov_len = sizeof(type);
287     iov[1].iov_base = buf;
288     iov[1].iov_len = len;
289     return(tun_fix_return(readv(dev->fd_, iov, 2), sizeof(type)));
290   }
291   else
292     return(read(dev->fd_, buf, len));
293 }
294
295 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
296 {
297   if(!dev || dev->fd_ < 0)
298     return -1;
299
300   if(!buf)
301     return 0;
302
303   if(dev->with_pi_)
304   {
305     struct iovec iov[2];
306     u_int32_t type;
307     struct ip *hdr = (struct ip*)buf;
308
309     type = 0;
310     if(hdr->ip_v == 4)
311       type = htonl(AF_INET);
312     else
313       type = htonl(AF_INET6);
314
315     iov[0].iov_base = &type;
316     iov[0].iov_len = sizeof(type);
317     iov[1].iov_base = buf;
318     iov[1].iov_len = len;
319     return(tun_fix_return(writev(dev->fd_, iov, 2), sizeof(type)));
320   }
321   else
322     return(write(dev->fd_, buf, len));
323 }
324
325 void tun_do_ifconfig(tun_device_t* dev)
326 {
327   if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
328     return;
329
330   char* end;
331   if(dev->type_ == TYPE_TAP) {
332 #if defined(__GNUC__) && defined(__OpenBSD__)
333     end = "link0";
334 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
335     end = "up";
336 #elif defined(__GNUC__) && defined(__NetBSD__)
337     end = NULL;
338 #else
339  #error This Device works just for OpenBSD, FreeBSD or NetBSD
340 #endif
341   }
342   else
343     end = "up";
344
345   char* mtu_str = NULL;
346   asprintf(&mtu_str, "%d", dev->mtu_);
347   if(!mtu_str) {
348     log_printf(ERROR, "Execution of ifconfig failed");
349     return;
350   }
351
352   char* const argv[] = { "/sbin/ifconfig", dev->actual_name_, dev->net_addr_, "netmask", dev->net_mask_, "mtu", mtu_str, end, NULL };
353   char* const evp[] = { NULL };
354   uanytun_exec("/sbin/ifconfig", argv, evp);
355
356   free(mtu_str);
357 }