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.
48 #include <boost/assign.hpp>
51 #include <sys/ioctl.h>
52 #include <arpa/inet.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_tun.h>
58 #define DEFAULT_DEVICE "/dev/net/tun"
60 #include "tunDevice.h"
61 #include "threadUtils.hpp"
63 #include "anytunError.h"
66 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)
69 memset(&ifr, 0, sizeof(ifr));
71 if(conf_.type_ == TYPE_TUN) {
72 ifr.ifr_flags = IFF_TUN;
74 } else if(conf_.type_ == TYPE_TAP) {
75 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
78 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
82 strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ-1);
85 fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
87 AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE << "): " << AnytunErrno(errno);
90 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
91 actual_name_ = ifr.ifr_name;
92 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
93 actual_name_ = ifr.ifr_name;
96 AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
98 actual_node_ = DEFAULT_DEVICE;
100 if(ifcfg_addr != "") {
105 TunDevice::~TunDevice()
112 int TunDevice::fix_return(int ret, size_t pi_length) const
118 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
121 int TunDevice::read(uint8_t* buf, uint32_t len)
131 iov[0].iov_base = &tpi;
132 iov[0].iov_len = sizeof(tpi);
133 iov[1].iov_base = buf;
134 iov[1].iov_len = len;
135 return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
137 return(::read(fd_, buf, len));
141 int TunDevice::write(uint8_t* buf, uint32_t len)
154 struct iphdr* hdr = reinterpret_cast<struct iphdr*>(buf);
157 if(hdr->version == 4) {
158 tpi.proto = htons(ETH_P_IP);
160 tpi.proto = htons(ETH_P_IPV6);
163 iov[0].iov_base = &tpi;
164 iov[0].iov_len = sizeof(tpi);
165 iov[1].iov_base = buf;
166 iov[1].iov_len = len;
167 return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
169 return(::write(fd_, buf, len));
173 void TunDevice::init_post()
175 // nothing to be done here
178 void TunDevice::do_ifconfig()
180 std::ostringstream mtu_ss;
181 mtu_ss << conf_.mtu_;
182 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
183 sys_exec_ = new SysExec("/sbin/ifconfig", args);
186 void TunDevice::waitUntilReady()
189 SysExec::waitAndDestroy(sys_exec_);