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, u_int16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
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");
75 else if(conf_.type_ == TYPE_TAP) {
76 device_file.append("tap");
81 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);
99 AnytunError::throwErr() << "can't open device file dynamically: no unused node left";
101 AnytunError::throwErr() << "can't open device file (" << device_file << "): " << AnytunErrno(errno);
108 actual_name_ = s.str();
111 actual_name_ = dev_name;
113 actual_node_ = device_file;
121 TunDevice::~TunDevice()
127 #if defined(__GNUC__) && defined(__OpenBSD__)
129 void TunDevice::init_post()
132 if(conf_.type_ == TYPE_TAP)
137 if (ioctl(fd_, TUNGIFINFO, &ti) < 0) {
139 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
142 ti.flags |= IFF_MULTICAST;
143 if(conf_.type_ == TYPE_TUN)
144 ti.flags &= ~IFF_POINTOPOINT;
146 if (ioctl(fd_, TUNSIFINFO, &ti) < 0) {
148 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
152 #elif defined(__GNUC__) && defined(__FreeBSD__)
154 void TunDevice::init_post()
157 if(conf_.type_ == TYPE_TAP)
160 if(conf_.type_ == TYPE_TUN) {
162 if(ioctl(fd_, TUNSLMODE, &arg) < 0) {
164 AnytunError::throwErr() << "can't disable link-layer mode for interface: " << AnytunErrno(errno);
168 if(ioctl(fd_, TUNSIFHEAD, &arg) < 0) {
170 AnytunError::throwErr() << "can't enable multi-af modefor interface: " << AnytunErrno(errno);
174 arg |= IFF_MULTICAST;
175 if(ioctl(fd_, TUNSIFMODE, &arg) < 0) {
177 AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
182 #elif defined(__GNUC__) && defined(__NetBSD__)
184 void TunDevice::init_post()
188 int arg = IFF_POINTOPOINT|IFF_MULTICAST;
189 ioctl(fd_, TUNSIFMODE, &arg);
191 ioctl(fd_, TUNSLMODE, &arg);
195 #error This Device works just for OpenBSD, FreeBSD or NetBSD
198 int TunDevice::fix_return(int ret, size_t pi_length) const
203 return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
206 int TunDevice::read(u_int8_t* buf, u_int32_t len)
215 iov[0].iov_base = &type;
216 iov[0].iov_len = sizeof(type);
217 iov[1].iov_base = buf;
218 iov[1].iov_len = len;
219 return(fix_return(::readv(fd_, iov, 2), sizeof(type)));
222 return(::read(fd_, buf, len));
225 int TunDevice::write(u_int8_t* buf, u_int32_t len)
236 struct ip *hdr = reinterpret_cast<struct ip*>(buf);
240 type = htonl(AF_INET);
242 type = htonl(AF_INET6);
244 iov[0].iov_base = &type;
245 iov[0].iov_len = sizeof(type);
246 iov[1].iov_base = buf;
247 iov[1].iov_len = len;
248 return(fix_return(::writev(fd_, iov, 2), sizeof(type)));
251 return(::write(fd_, buf, len));
254 void TunDevice::do_ifconfig()
256 std::ostringstream mtu_ss;
257 mtu_ss << conf_.mtu_;
258 StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
260 if(conf_.type_ == TYPE_TUN)
261 args.push_back("up");
263 #if defined(__GNUC__) && defined(__OpenBSD__)
264 args.push_back("link0");
265 #elif defined(__GNUC__) && defined(__FreeBSD__)
266 args.push_back("up");
267 #elif defined(__GNUC__) && defined(__NetBSD__)
268 // nothing to be done here
270 #error This Device works just for OpenBSD, FreeBSD or NetBSD
274 anytun_exec("/sbin/ifconfig", args);