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/>.
34 #include <boost/assign.hpp>
39 #include <sys/socket.h>
41 #include <net/if_tun.h>
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
49 #include "tunDevice.h"
50 #include "threadUtils.hpp"
52 #include "anytunError.h"
55 #define DEVICE_FILE_MAX 255
57 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)
59 std::string device_file = "/dev/";
62 device_file.append(dev_name);
65 #if defined(__GNUC__) && defined(__OpenBSD__)
66 else if(conf_.type_ == TYPE_TUN || conf_.type_ == TYPE_TAP) {
67 device_file.append("tun");
71 else if(conf_.type_ == TYPE_TUN) {
72 device_file.append("tun");
74 } else if(conf_.type_ == TYPE_TAP) {
75 device_file.append("tap");
80 AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
85 for(; dev_id <= DEVICE_FILE_MAX; ++dev_id) {
86 std::ostringstream ds;
89 fd_ = ::open(ds.str().c_str(), O_RDWR);
95 fd_ = ::open(device_file.c_str(), O_RDWR);
100 AnytunError::throwErr() << "can't open device file dynamically: no unused node left";
102 AnytunError::throwErr() << "can't open device file (" << device_file << "): " << AnytunErrno(errno);
110 actual_name_ = s.str();
112 actual_name_ = dev_name;
115 actual_node_ = device_file;
119 if(ifcfg_addr != "") {
124 TunDevice::~TunDevice()
131 #if defined(__GNUC__) && defined(__OpenBSD__)
133 void TunDevice::init_post()
136 if(conf_.type_ == TYPE_TAP) {
142 if(ioctl(fd_, TUNGIFINFO, &ti) < 0) {
144 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
147 ti.flags |= IFF_MULTICAST;
148 if(conf_.type_ == TYPE_TUN) {
149 ti.flags &= ~IFF_POINTOPOINT;
152 if(ioctl(fd_, TUNSIFINFO, &ti) < 0) {
154 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
158 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
160 void TunDevice::init_post()
163 if(conf_.type_ == TYPE_TAP) {
167 if(conf_.type_ == TYPE_TUN) {
169 if(ioctl(fd_, TUNSLMODE, &arg) < 0) {
171 AnytunError::throwErr() << "can't disable link-layer mode for interface: " << AnytunErrno(errno);
175 if(ioctl(fd_, TUNSIFHEAD, &arg) < 0) {
177 AnytunError::throwErr() << "can't enable multi-af modefor interface: " << AnytunErrno(errno);
181 arg |= IFF_MULTICAST;
182 if(ioctl(fd_, TUNSIFMODE, &arg) < 0) {
184 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
189 #elif defined(__GNUC__) && defined(__NetBSD__)
191 void TunDevice::init_post()
195 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
196 ioctl(fd_, TUNSIFMODE, &arg);
198 ioctl(fd_, TUNSLMODE, &arg);
202 #error This Device works just for OpenBSD, FreeBSD or NetBSD
205 int TunDevice::fix_return(int ret, size_t pi_length) const
211 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
214 int TunDevice::read(uint8_t* buf, uint32_t len)
224 iov[0].iov_base = &type;
225 iov[0].iov_len = sizeof(type);
226 iov[1].iov_base = buf;
227 iov[1].iov_len = len;
228 return(fix_return(::readv(fd_, iov, 2), sizeof(type)));
230 return(::read(fd_, buf, len));
234 int TunDevice::write(uint8_t* buf, uint32_t len)
247 struct ip* hdr = reinterpret_cast<struct ip*>(buf);
251 type = htonl(AF_INET);
253 type = htonl(AF_INET6);
256 iov[0].iov_base = &type;
257 iov[0].iov_len = sizeof(type);
258 iov[1].iov_base = buf;
259 iov[1].iov_len = len;
260 return(fix_return(::writev(fd_, iov, 2), sizeof(type)));
262 return(::write(fd_, buf, len));
266 void TunDevice::do_ifconfig()
268 std::ostringstream mtu_ss;
269 mtu_ss << conf_.mtu_;
270 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
272 if(conf_.type_ == TYPE_TUN) {
273 args.push_back("up");
275 #if defined(__GNUC__) && defined(__OpenBSD__)
276 args.push_back("link0");
277 #elif defined(__GNUC__) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
278 args.push_back("up");
279 #elif defined(__GNUC__) && defined(__NetBSD__)
280 // nothing to be done here
282 #error This Device works just for OpenBSD, FreeBSD or NetBSD
285 sys_exec_ = new SysExec("/sbin/ifconfig", args);
288 void TunDevice::waitUntilReady()
291 SysExec::waitAndDestroy(sys_exec_);