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