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.
18 * Copyright (C) 2007-2014 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
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
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.
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/>.
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
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.
49 #include "datatypes.h"
53 #include "tun_helper.h"
61 #include <sys/socket.h>
63 #include <net/if_tun.h>
64 #include <sys/ioctl.h>
65 #include <sys/types.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in.h>
69 #include <netinet/ip.h>
70 #define DEVICE_FILE_MAX 255
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)
77 tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400);
78 dev->actual_name_ = NULL;
80 char* device_file = NULL;
81 char* actual_name_start = NULL;
84 asprintf(&device_file, "/dev/%s", dev_name);
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";
93 else if(dev->type_ == TYPE_TUN) {
94 asprintf(&device_file, "/dev/tun");
95 actual_name_start = "tun";
97 else if(dev->type_ == TYPE_TAP) {
98 asprintf(&device_file, "/dev/tap");
99 actual_name_start = "tap";
103 log_printf(ERROR, "unable to recognize type of device (tun or tap)");
108 log_printf(ERROR, "can't open device file: memory error");
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);
119 if(!device_file_tmp) {
120 log_printf(ERROR, "can't open device file: memory error");
126 dev->fd_ = open(device_file_tmp, O_RDWR);
127 free(device_file_tmp);
133 dev->fd_ = open(device_file, O_RDWR);
138 log_printf(ERROR, "can't open device file dynamically: no unused node left");
140 log_printf(ERROR, "can't open device file (%s): %s", device_file, strerror(errno));
147 asprintf(&(dev->actual_name_), "%s%d", actual_name_start, dev_id);
149 dev->actual_name_ = strdup(dev_name);
151 if(!dev->actual_name_) {
152 log_printf(ERROR, "can't open device file: memory error");
157 int ret = tun_init_post(dev);
164 tun_do_ifconfig(dev);
170 #if defined(__GNUC__) && defined(__OpenBSD__)
172 int tun_init_post(tun_device_t* dev)
178 if(dev->type_ == TYPE_TAP)
183 if(ioctl(dev->fd_, TUNGIFINFO, &ti) < 0) {
184 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
188 ti.flags |= IFF_MULTICAST;
189 if(dev->type_ == TYPE_TUN)
190 ti.flags &= ~IFF_POINTOPOINT;
192 if(ioctl(dev->fd_, TUNSIFINFO, &ti) < 0) {
193 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
199 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
201 int tun_init_post(tun_device_t* dev)
207 if(dev->type_ == TYPE_TAP)
210 if(dev->type_ == TYPE_TUN) {
212 if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
213 log_printf(ERROR, "can't disable link-layer mode for interface: %s", strerror(errno));
218 if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
219 log_printf(ERROR, "can't enable multi-af mode for interface: %s", strerror(errno));
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));
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)
243 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
244 ioctl(dev->fd_, TUNSIFMODE, &arg);
246 ioctl(dev->fd_, TUNSLMODE, &arg);
252 #error This Device works just for OpenBSD, FreeBSD or NetBSD
257 void tun_close(tun_device_t* dev)
265 if(dev->actual_name_)
266 free(dev->actual_name_);
269 free(dev->net_addr_);
272 free(dev->net_mask_);
275 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
277 if(!dev || dev->fd_ < 0)
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)));
292 return(read(dev->fd_, buf, len));
295 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
297 if(!dev || dev->fd_ < 0)
307 struct ip *hdr = (struct ip*)buf;
311 type = htonl(AF_INET);
313 type = htonl(AF_INET6);
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)));
322 return(write(dev->fd_, buf, len));
325 void tun_do_ifconfig(tun_device_t* dev)
327 if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
331 if(dev->type_ == TYPE_TAP) {
332 #if defined(__GNUC__) && defined(__OpenBSD__)
334 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
336 #elif defined(__GNUC__) && defined(__NetBSD__)
339 #error This Device works just for OpenBSD, FreeBSD or NetBSD
345 char* mtu_str = NULL;
346 asprintf(&mtu_str, "%d", dev->mtu_);
348 log_printf(ERROR, "Execution of ifconfig failed");
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);