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-2008 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/>.
38 #include "datatypes.h"
42 #include "tun_helper.h"
48 #include <sys/ioctl.h>
49 #include <arpa/inet.h>
53 #include <linux/if_ether.h>
54 #include <linux/if_tun.h>
55 #define DEFAULT_DEVICE "/dev/net/tun"
60 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 dev->fd_ = open(DEFAULT_DEVICE, O_RDWR);
69 log_printf(ERROR, "can't open device file (%s): %s", DEFAULT_DEVICE, strerror(errno));
75 memset(&ifr, 0, sizeof(ifr));
77 if(dev->type_ == TYPE_TUN) {
78 ifr.ifr_flags = IFF_TUN;
81 else if(dev->type_ == TYPE_TAP) {
82 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
86 log_printf(ERROR, "unable to recognize type of device (tun or tap)");
92 strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
94 if(!ioctl(dev->fd_, TUNSETIFF, &ifr)) {
95 dev->actual_name_ = strdup(ifr.ifr_name);
96 } else if(!ioctl(dev->fd_, (('T' << 8) | 202), &ifr)) {
97 dev->actual_name_ = strdup(ifr.ifr_name);
99 log_printf(ERROR, "tun/tap device ioctl failed: %s", strerror(errno));
104 if(!dev->actual_name_) {
105 log_printf(ERROR, "can't open device file: memory error");
111 tun_do_ifconfig(dev);
116 int tun_init_post(tun_device_t* dev)
121 void tun_close(tun_device_t* dev)
129 if(dev->actual_name_)
130 free(dev->actual_name_);
133 free(dev->net_addr_);
136 free(dev->net_mask_);
139 int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
141 if(!dev || dev->fd_ < 0)
149 iov[0].iov_base = &tpi;
150 iov[0].iov_len = sizeof(tpi);
151 iov[1].iov_base = buf;
152 iov[1].iov_len = len;
153 return(tun_fix_return(readv(dev->fd_, iov, 2), sizeof(tpi)));
156 return(read(dev->fd_, buf, len));
159 int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
161 if(!dev || dev->fd_ < 0)
171 struct iphdr *hdr = (struct iphdr *)buf;
174 if(hdr->version == 4)
175 tpi.proto = htons(ETH_P_IP);
177 tpi.proto = htons(ETH_P_IPV6);
179 iov[0].iov_base = &tpi;
180 iov[0].iov_len = sizeof(tpi);
181 iov[1].iov_base = buf;
182 iov[1].iov_len = len;
183 return(tun_fix_return(writev(dev->fd_, iov, 2), sizeof(tpi)));
186 return(write(dev->fd_, buf, len));
189 void tun_do_ifconfig(tun_device_t* dev)
191 if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_)
194 char* mtu_str = NULL;
195 asprintf(&mtu_str, "%d", dev->mtu_);
197 log_printf(ERROR, "Execution of ifconfig failed");
201 char* const argv[] = { "/sbin/ifconfig", dev->actual_name_, dev->net_addr_, "netmask", dev->net_mask_, "mtu", mtu_str, NULL };
202 char* const evp[] = { NULL };
203 uanytun_exec("/sbin/ifconfig", argv, evp);