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/>.
35 #include <sys/socket.h>
37 #include <net/if_tun.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
47 #include "tunDevice.h"
48 #include "threadUtils.hpp"
50 #include "anytunError.h"
52 #define DEVICE_FILE_MAX 255
54 TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, std::string ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
56 std::string device_file = "/dev/";
59 device_file.append(dev_name);
62 #if defined(__GNUC__) && defined(__OpenBSD__)
63 else if(conf_.type_ == TYPE_TUN || conf_.type_ == TYPE_TAP) {
64 device_file.append("tun");
68 else if(conf_.type_ == TYPE_TUN) {
69 device_file.append("tun");
72 else if(conf_.type_ == TYPE_TAP) {
73 device_file.append("tap");
78 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
82 for(; dev_id <= DEVICE_FILE_MAX; ++dev_id) {
83 std::ostringstream ds;
86 fd_ = ::open(ds.str().c_str(), O_RDWR);
92 fd_ = ::open(device_file.c_str(), O_RDWR);
96 AnytunError::throwErr() << "can't open device file dynamically: no unused node left";
98 AnytunError::throwErr() << "can't open device file (" << device_file << "): " << AnytunErrno(errno);
105 actual_name_ = s.str();
108 actual_name_ = dev_name;
110 actual_node_ = device_file;
118 TunDevice::~TunDevice()
124 #if defined(__GNUC__) && defined(__OpenBSD__)
126 void TunDevice::init_post()
129 if(conf_.type_ == TYPE_TAP)
134 if (ioctl(fd_, TUNGIFINFO, &ti) < 0) {
136 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
139 ti.flags |= IFF_MULTICAST;
140 if(conf_.type_ == TYPE_TUN)
141 ti.flags &= ~IFF_POINTOPOINT;
143 if (ioctl(fd_, TUNSIFINFO, &ti) < 0) {
145 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
149 #elif defined(__GNUC__) && defined(__FreeBSD__)
151 void TunDevice::init_post()
154 if(conf_.type_ == TYPE_TAP)
157 if(dev->type_ == TYPE_TUN) {
159 if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
161 AnytunError::throwErr() << "can't disable link-layer mode for interface: " << AnytunErrno(errno);
165 if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
167 AnytunError::throwErr() << "can't enable multi-af modefor interface: " << AnytunErrno(errno);
171 arg |= IFF_MULTICAST;
172 if(ioctl(dev->fd_, TUNSIFMODE, &arg) < 0) {
174 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
179 #elif defined(__GNUC__) && defined(__NetBSD__)
181 void TunDevice::init_post()
185 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
186 ioctl(fd_, TUNSIFMODE, &arg);
188 ioctl(fd_, TUNSLMODE, &arg);
192 #error This Device works just for OpenBSD, FreeBSD or NetBSD
195 int TunDevice::fix_return(int ret, size_t pi_length) const
200 return (static_cast<size_t>(ret) > type_length ? (ret - type_length) : 0);
203 int TunDevice::read(u_int8_t* buf, u_int32_t len)
212 iov[0].iov_base = &type;
213 iov[0].iov_len = sizeof(type);
214 iov[1].iov_base = buf;
215 iov[1].iov_len = len;
216 return(fix_return(::readv(fd_, iov, 2), sizeof(type)));
219 return(::read(fd_, buf, len));
222 int TunDevice::write(u_int8_t* buf, u_int32_t len)
233 struct ip *hdr = reinterpret_cast<struct ip*>(buf);
237 type = htonl(AF_INET);
239 type = htonl(AF_INET6);
241 iov[0].iov_base = &type;
242 iov[0].iov_len = sizeof(type);
243 iov[1].iov_base = buf;
244 iov[1].iov_len = len;
245 return(fix_return(::writev(fd_, iov, 2), sizeof(type)));
248 return(::write(fd_, buf, len));
251 void TunDevice::do_ifconfig()
253 std::ostringstream command;
254 command << "/sbin/ifconfig " << actual_name_ << " " << conf_.addr_.toString()
255 << " netmask " << conf_.netmask_.toString() << " mtu " << conf_.mtu_;
257 if(conf_.type_ == TYPE_TUN)
260 #if defined(__GNUC__) && defined(__OpenBSD__)
262 #elif defined(__GNUC__) && defined(__FreeBSD__)
264 #elif defined(__GNUC__) && defined(__NetBSD__)
267 #error This Device works just for OpenBSD, FreeBSD or NetBSD
271 int result = system(command.str().c_str());
273 cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig failed" << AnytunErrno(errno);
275 if(WIFEXITED(result))
276 cLog.msg(Log::PRIO_NOTICE) << "ifconfig returned " << WEXITSTATUS(result);
277 else if(WIFSIGNALED(result))
278 cLog.msg(Log::PRIO_NOTICE) << "ifconfig terminated after signal " << WTERMSIG(result);
280 cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig: unkown error";