4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
36 #include <sys/ioctl.h>
37 #include <arpa/inet.h>
41 #include <linux/if_ether.h>
42 #include <linux/if_tun.h>
43 #define DEFAULT_DEVICE "/dev/net/tun"
45 #include "tunDevice.h"
46 #include "threadUtils.hpp"
48 #include "anytunError.h"
50 TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, u_int16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
53 memset(&ifr, 0, sizeof(ifr));
55 if(conf_.type_ == TYPE_TUN) {
56 ifr.ifr_flags = IFF_TUN;
59 else if(conf_.type_ == TYPE_TAP) {
60 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
64 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
67 strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
69 fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
71 AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE << "): " << AnytunErrno(errno);
73 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
74 actual_name_ = ifr.ifr_name;
75 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
76 actual_name_ = ifr.ifr_name;
79 AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
81 actual_node_ = DEFAULT_DEVICE;
87 TunDevice::~TunDevice()
93 int TunDevice::fix_return(int ret, size_t pi_length) const
98 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
101 int TunDevice::read(u_int8_t* buf, u_int32_t len)
111 iov[0].iov_base = &tpi;
112 iov[0].iov_len = sizeof(tpi);
113 iov[1].iov_base = buf;
114 iov[1].iov_len = len;
115 return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
118 return(::read(fd_, buf, len));
121 int TunDevice::write(u_int8_t* buf, u_int32_t len)
133 struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
136 if(hdr->version == 4)
137 tpi.proto = htons(ETH_P_IP);
139 tpi.proto = htons(ETH_P_IPV6);
141 iov[0].iov_base = &tpi;
142 iov[0].iov_len = sizeof(tpi);
143 iov[1].iov_base = buf;
144 iov[1].iov_len = len;
145 return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
148 return(::write(fd_, buf, len));
151 void TunDevice::init_post()
153 // nothing to be done here
156 void TunDevice::do_ifconfig()
158 std::ostringstream command;
159 command << "/sbin/ifconfig " << actual_name_ << " " << conf_.addr_.toString()
160 << " netmask " << conf_.netmask_.toString() << " mtu " << conf_.mtu_;
162 int result = system(command.str().c_str());
164 cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig failed: " << AnytunErrno(errno);
166 if(WIFEXITED(result))
167 cLog.msg(Log::PRIO_NOTICE) << "ifconfig returned " << WEXITSTATUS(result);
168 else if(WIFSIGNALED(result))
169 cLog.msg(Log::PRIO_NOTICE) << "ifconfig terminated after signal " << WTERMSIG(result);
171 cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig: unkown error";