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 methods 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-2014 Markus Grüneis, 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
24 * Anytun is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with Anytun. If not, see <http://www.gnu.org/licenses/>.
32 * In addition, as a special exception, the copyright holders give
33 * permission to link the code of portions of this program with the
34 * OpenSSL library under certain conditions as described in each
35 * individual source file, and distribute linked combinations
37 * You must obey the GNU General Public License in all respects
38 * for all of the code used other than OpenSSL. If you modify
39 * file(s) with this exception, you may extend this exception to your
40 * version of the file(s), but you are not obligated to do so. If you
41 * do not wish to do so, delete this exception statement from your
42 * version. If you delete this exception statement from all source
43 * files in the program, then also delete it here.
47 #include <boost/assign.hpp>
52 #include <sys/socket.h>
54 #include <net/if_tun.h>
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
62 #include "tunDevice.h"
63 #include "threadUtils.hpp"
65 #include "anytunError.h"
68 #define DEVICE_FILE_MAX 255
70 TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, uint16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400),sys_exec_(NULL)
72 std::string device_file = "/dev/";
75 device_file.append(dev_name);
78 #if defined(__GNUC__) && defined(__OpenBSD__)
79 else if(conf_.type_ == TYPE_TUN || conf_.type_ == TYPE_TAP) {
80 device_file.append("tun");
84 else if(conf_.type_ == TYPE_TUN) {
85 device_file.append("tun");
87 } else if(conf_.type_ == TYPE_TAP) {
88 device_file.append("tap");
93 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
98 for(; dev_id <= DEVICE_FILE_MAX; ++dev_id) {
99 std::ostringstream ds;
102 fd_ = ::open(ds.str().c_str(), O_RDWR);
108 fd_ = ::open(device_file.c_str(), O_RDWR);
113 AnytunError::throwErr() << "can't open device file dynamically: no unused node left";
115 AnytunError::throwErr() << "can't open device file (" << device_file << "): " << AnytunErrno(errno);
123 actual_name_ = s.str();
125 actual_name_ = dev_name;
128 actual_node_ = device_file;
132 if(ifcfg_addr != "") {
137 TunDevice::~TunDevice()
144 #if defined(__GNUC__) && defined(__OpenBSD__)
146 void TunDevice::init_post()
149 if(conf_.type_ == TYPE_TAP) {
155 if(ioctl(fd_, TUNGIFINFO, &ti) < 0) {
157 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
160 ti.flags |= IFF_MULTICAST;
161 if(conf_.type_ == TYPE_TUN) {
162 ti.flags &= ~IFF_POINTOPOINT;
165 if(ioctl(fd_, TUNSIFINFO, &ti) < 0) {
167 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
171 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
173 void TunDevice::init_post()
176 if(conf_.type_ == TYPE_TAP) {
180 if(conf_.type_ == TYPE_TUN) {
182 if(ioctl(fd_, TUNSLMODE, &arg) < 0) {
184 AnytunError::throwErr() << "can't disable link-layer mode for interface: " << AnytunErrno(errno);
188 if(ioctl(fd_, TUNSIFHEAD, &arg) < 0) {
190 AnytunError::throwErr() << "can't enable multi-af modefor interface: " << AnytunErrno(errno);
194 arg |= IFF_MULTICAST;
195 if(ioctl(fd_, TUNSIFMODE, &arg) < 0) {
197 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
202 #elif defined(__GNUC__) && defined(__NetBSD__)
204 void TunDevice::init_post()
208 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
209 ioctl(fd_, TUNSIFMODE, &arg);
211 ioctl(fd_, TUNSLMODE, &arg);
215 #error This Device works just for OpenBSD, FreeBSD or NetBSD
218 int TunDevice::fix_return(int ret, size_t pi_length) const
224 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
227 int TunDevice::read(uint8_t* buf, uint32_t len)
237 iov[0].iov_base = &type;
238 iov[0].iov_len = sizeof(type);
239 iov[1].iov_base = buf;
240 iov[1].iov_len = len;
241 return(fix_return(::readv(fd_, iov, 2), sizeof(type)));
243 return(::read(fd_, buf, len));
247 int TunDevice::write(uint8_t* buf, uint32_t len)
260 struct ip* hdr = reinterpret_cast<struct ip*>(buf);
264 type = htonl(AF_INET);
266 type = htonl(AF_INET6);
269 iov[0].iov_base = &type;
270 iov[0].iov_len = sizeof(type);
271 iov[1].iov_base = buf;
272 iov[1].iov_len = len;
273 return(fix_return(::writev(fd_, iov, 2), sizeof(type)));
275 return(::write(fd_, buf, len));
279 void TunDevice::do_ifconfig()
281 std::ostringstream mtu_ss;
282 mtu_ss << conf_.mtu_;
283 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
285 if(conf_.type_ == TYPE_TUN) {
286 args.push_back("up");
288 #if defined(__GNUC__) && defined(__OpenBSD__)
289 args.push_back("link0");
290 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
291 args.push_back("up");
292 #elif defined(__GNUC__) && defined(__NetBSD__)
293 // nothing to be done here
295 #error This Device works just for OpenBSD, FreeBSD or NetBSD
298 sys_exec_ = new SysExec("/sbin/ifconfig", args);
301 void TunDevice::waitUntilReady()
304 SysExec::waitAndDestroy(sys_exec_);