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.
52 #include "datatypes.h"
56 #include "tun_helper.h"
62 #include <sys/socket.h>
64 #include <net/if_tun.h>
65 #include <sys/ioctl.h>
66 #include <sys/types.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in.h>
70 #include <netinet/ip.h>
71 #define DEVICE_FILE_MAX 255
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)
81 tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400);
82 dev->actual_name_ = NULL;
84 char* device_file = NULL;
85 char* actual_name_start = NULL;
88 asprintf(&device_file, "/dev/%s", dev_name);
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";
97 else if(dev->type_ == TYPE_TUN) {
98 asprintf(&device_file, "/dev/tun");
99 actual_name_start = "tun";
101 else if(dev->type_ == TYPE_TAP) {
102 asprintf(&device_file, "/dev/tap");
103 actual_name_start = "tap";
107 log_printf(ERROR, "unable to recognize type of device (tun or tap)");
112 log_printf(ERROR, "can't open device file: memory error");
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);
123 if(!device_file_tmp) {
124 log_printf(ERROR, "can't open device file: memory error");
130 dev->fd_ = open(device_file_tmp, O_RDWR);
131 free(device_file_tmp);
137 dev->fd_ = open(device_file, O_RDWR);
142 log_printf(ERROR, "can't open device file dynamically: no unused node left");
144 log_printf(ERROR, "can't open device file (%s): %s", device_file, strerror(errno));
151 asprintf(&(dev->actual_name_), "%s%d", actual_name_start, dev_id);
153 dev->actual_name_ = strdup(dev_name);
155 if(!dev->actual_name_) {
156 log_printf(ERROR, "can't open device file: memory error");
161 int ret = tun_init_post(dev);
168 tun_do_ifconfig(dev);
174 #if defined(__GNUC__) && defined(__OpenBSD__)
176 int tun_init_post(tun_device_t* dev)
182 if(dev->type_ == TYPE_TAP)
187 if(ioctl(dev->fd_, TUNGIFINFO, &ti) < 0) {
188 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
192 ti.flags |= IFF_MULTICAST;
193 if(dev->type_ == TYPE_TUN)
194 ti.flags &= ~IFF_POINTOPOINT;
196 if(ioctl(dev->fd_, TUNSIFINFO, &ti) < 0) {
197 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
203 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
205 int tun_init_post(tun_device_t* dev)
211 if(dev->type_ == TYPE_TAP)
214 if(dev->type_ == TYPE_TUN) {
216 if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
217 log_printf(ERROR, "can't disable link-layer mode for interface: %s", strerror(errno));
222 if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
223 log_printf(ERROR, "can't enable multi-af mode for interface: %s", strerror(errno));
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));
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)
247 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
248 ioctl(dev->fd_, TUNSIFMODE, &arg);
250 ioctl(dev->fd_, TUNSLMODE, &arg);
256 #error This Device works just for OpenBSD, FreeBSD or NetBSD
261 void tun_close(tun_device_t* dev)
269 if(dev->actual_name_)
270 free(dev->actual_name_);
273 free(dev->net_addr_);
276 free(dev->net_mask_);
279 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
281 if(!dev || dev->fd_ < 0)
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)));
296 return(read(dev->fd_, buf, len));
299 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
301 if(!dev || dev->fd_ < 0)
311 struct ip *hdr = (struct ip*)buf;
315 type = htonl(AF_INET);
317 type = htonl(AF_INET6);
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)));
326 return(write(dev->fd_, buf, len));
329 void tun_do_ifconfig(tun_device_t* dev)
331 if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
335 if(dev->type_ == TYPE_TAP) {
336 #if defined(__GNUC__) && defined(__OpenBSD__)
338 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
340 #elif defined(__GNUC__) && defined(__NetBSD__)
343 #error This Device works just for OpenBSD, FreeBSD or NetBSD
349 char* mtu_str = NULL;
350 asprintf(&mtu_str, "%d", dev->mtu_);
352 log_printf(ERROR, "Execution of ifconfig failed");
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);