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.
18 * Copyright (C) 2007-2010 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/>.
36 #include "datatypes.h"
40 #include "tun_helper.h"
48 #include <sys/socket.h>
50 #include <net/if_tun.h>
51 #include <sys/ioctl.h>
52 #include <sys/types.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in.h>
56 #include <netinet/ip.h>
57 #define DEVICE_FILE_MAX 255
59 int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix)
64 tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400);
65 dev->actual_name_ = NULL;
67 char* device_file = NULL;
68 char* actual_name_start = NULL;
71 asprintf(&device_file, "/dev/%s", dev_name);
74 #if defined(__GNUC__) && defined(__OpenBSD__)
75 else if(dev->type_ == TYPE_TUN || dev->type_ == TYPE_TAP) {
76 asprintf(&device_file, "/dev/tun");
77 actual_name_start = "tun";
80 else if(dev->type_ == TYPE_TUN) {
81 asprintf(&device_file, "/dev/tun");
82 actual_name_start = "tun";
84 else if(dev->type_ == TYPE_TAP) {
85 asprintf(&device_file, "/dev/tap");
86 actual_name_start = "tap";
90 log_printf(ERROR, "unable to recognize type of device (tun or tap)");
95 log_printf(ERROR, "can't open device file: memory error");
102 for(; dev_id <= DEVICE_FILE_MAX; ++dev_id) {
103 char* device_file_tmp = NULL;
104 asprintf(&device_file_tmp, "%s%d", device_file, dev_id);
106 if(!device_file_tmp) {
107 log_printf(ERROR, "can't open device file: memory error");
113 dev->fd_ = open(device_file_tmp, O_RDWR);
114 free(device_file_tmp);
120 dev->fd_ = open(device_file, O_RDWR);
125 log_printf(ERROR, "can't open device file dynamically: no unused node left");
127 log_printf(ERROR, "can't open device file (%s): %s", device_file, strerror(errno));
134 asprintf(&(dev->actual_name_), "%s%d", actual_name_start, dev_id);
136 dev->actual_name_ = strdup(dev_name);
138 if(!dev->actual_name_) {
139 log_printf(ERROR, "can't open device file: memory error");
144 int ret = tun_init_post(dev);
151 tun_do_ifconfig(dev);
157 #if defined(__GNUC__) && defined(__OpenBSD__)
159 int tun_init_post(tun_device_t* dev)
165 if(dev->type_ == TYPE_TAP)
170 if(ioctl(dev->fd_, TUNGIFINFO, &ti) < 0) {
171 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
175 ti.flags |= IFF_MULTICAST;
176 if(dev->type_ == TYPE_TUN)
177 ti.flags &= ~IFF_POINTOPOINT;
179 if(ioctl(dev->fd_, TUNSIFINFO, &ti) < 0) {
180 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
186 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
188 int tun_init_post(tun_device_t* dev)
194 if(dev->type_ == TYPE_TAP)
197 if(dev->type_ == TYPE_TUN) {
199 if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
200 log_printf(ERROR, "can't disable link-layer mode for interface: %s", strerror(errno));
205 if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
206 log_printf(ERROR, "can't enable multi-af mode for interface: %s", strerror(errno));
211 arg |= IFF_MULTICAST;
212 if(ioctl(dev->fd_, TUNSIFMODE, &arg) < 0) {
213 log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno));
221 #elif defined(__GNUC__) && defined(__NetBSD__)
222 #warning this device has never been tested on NetBSD and might not work
223 int tun_init_post(tun_device_t* dev)
230 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
231 ioctl(dev->fd_, TUNSIFMODE, &arg);
233 ioctl(dev->fd_, TUNSLMODE, &arg);
239 #error This Device works just for OpenBSD, FreeBSD or NetBSD
244 void tun_close(tun_device_t* dev)
252 if(dev->actual_name_)
253 free(dev->actual_name_);
256 free(dev->net_addr_);
259 free(dev->net_mask_);
262 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
264 if(!dev || dev->fd_ < 0)
272 iov[0].iov_base = &type;
273 iov[0].iov_len = sizeof(type);
274 iov[1].iov_base = buf;
275 iov[1].iov_len = len;
276 return(tun_fix_return(readv(dev->fd_, iov, 2), sizeof(type)));
279 return(read(dev->fd_, buf, len));
282 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
284 if(!dev || dev->fd_ < 0)
294 struct ip *hdr = (struct ip*)buf;
298 type = htonl(AF_INET);
300 type = htonl(AF_INET6);
302 iov[0].iov_base = &type;
303 iov[0].iov_len = sizeof(type);
304 iov[1].iov_base = buf;
305 iov[1].iov_len = len;
306 return(tun_fix_return(writev(dev->fd_, iov, 2), sizeof(type)));
309 return(write(dev->fd_, buf, len));
312 void tun_do_ifconfig(tun_device_t* dev)
314 if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
318 if(dev->type_ == TYPE_TAP) {
319 #if defined(__GNUC__) && defined(__OpenBSD__)
321 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
323 #elif defined(__GNUC__) && defined(__NetBSD__)
326 #error This Device works just for OpenBSD, FreeBSD or NetBSD
332 char* mtu_str = NULL;
333 asprintf(&mtu_str, "%d", dev->mtu_);
335 log_printf(ERROR, "Execution of ifconfig failed");
339 char* const argv[] = { "/sbin/ifconfig", dev->actual_name_, dev->net_addr_, "netmask", dev->net_mask_, "mtu", mtu_str, end, NULL };
340 char* const evp[] = { NULL };
341 uanytun_exec("/sbin/ifconfig", argv, evp);