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-2009 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/>.
35 #include <boost/assign.hpp>
38 #include <sys/ioctl.h>
39 #include <arpa/inet.h>
43 #include <linux/if_ether.h>
44 #include <linux/if_tun.h>
45 #define DEFAULT_DEVICE "/dev/net/tun"
47 #include "tunDevice.h"
48 #include "threadUtils.hpp"
50 #include "anytunError.h"
53 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)
56 memset(&ifr, 0, sizeof(ifr));
58 if(conf_.type_ == TYPE_TUN) {
59 ifr.ifr_flags = IFF_TUN;
61 } else if(conf_.type_ == TYPE_TAP) {
62 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
65 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
69 strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
72 fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
74 AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE << "): " << AnytunErrno(errno);
77 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
78 actual_name_ = ifr.ifr_name;
79 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
80 actual_name_ = ifr.ifr_name;
83 AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
85 actual_node_ = DEFAULT_DEVICE;
87 if(ifcfg_addr != "") {
92 TunDevice::~TunDevice()
99 int TunDevice::fix_return(int ret, size_t pi_length) const
105 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
108 int TunDevice::read(uint8_t* buf, uint32_t len)
118 iov[0].iov_base = &tpi;
119 iov[0].iov_len = sizeof(tpi);
120 iov[1].iov_base = buf;
121 iov[1].iov_len = len;
122 return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
124 return(::read(fd_, buf, len));
128 int TunDevice::write(uint8_t* buf, uint32_t len)
141 struct iphdr* hdr = reinterpret_cast<struct iphdr*>(buf);
144 if(hdr->version == 4) {
145 tpi.proto = htons(ETH_P_IP);
147 tpi.proto = htons(ETH_P_IPV6);
150 iov[0].iov_base = &tpi;
151 iov[0].iov_len = sizeof(tpi);
152 iov[1].iov_base = buf;
153 iov[1].iov_len = len;
154 return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
156 return(::write(fd_, buf, len));
160 void TunDevice::init_post()
162 // nothing to be done here
165 void TunDevice::do_ifconfig()
167 std::ostringstream mtu_ss;
168 mtu_ss << conf_.mtu_;
169 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
170 sys_exec_ = new SysExec("/sbin/ifconfig", args);
173 void TunDevice::waitUntilReady()
176 SysExec::waitAndDestroy(sys_exec_);