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, u_int16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
56 memset(&ifr, 0, sizeof(ifr));
58 if(conf_.type_ == TYPE_TUN) {
59 ifr.ifr_flags = IFF_TUN;
62 else if(conf_.type_ == TYPE_TAP) {
63 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
67 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
70 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);
76 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
77 actual_name_ = ifr.ifr_name;
78 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
79 actual_name_ = ifr.ifr_name;
82 AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
84 actual_node_ = DEFAULT_DEVICE;
90 TunDevice::~TunDevice()
96 int TunDevice::fix_return(int ret, size_t pi_length) const
101 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
104 int TunDevice::read(u_int8_t* buf, u_int32_t len)
114 iov[0].iov_base = &tpi;
115 iov[0].iov_len = sizeof(tpi);
116 iov[1].iov_base = buf;
117 iov[1].iov_len = len;
118 return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
121 return(::read(fd_, buf, len));
124 int TunDevice::write(u_int8_t* buf, u_int32_t len)
136 struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
139 if(hdr->version == 4)
140 tpi.proto = htons(ETH_P_IP);
142 tpi.proto = htons(ETH_P_IPV6);
144 iov[0].iov_base = &tpi;
145 iov[0].iov_len = sizeof(tpi);
146 iov[1].iov_base = buf;
147 iov[1].iov_len = len;
148 return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
151 return(::write(fd_, buf, len));
154 void TunDevice::init_post()
156 // nothing to be done here
159 void TunDevice::do_ifconfig()
161 std::ostringstream mtu_ss;
162 mtu_ss << conf_.mtu_;
163 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
164 anytun_exec("/sbin/ifconfig", args);