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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
37 #include <linux/if_ether.h>
38 #include <linux/if_tun.h>
39 #define DEFAULT_DEVICE "/dev/net/tun"
41 #include "tunDevice.h"
42 #include "threadUtils.hpp"
45 TunDevice::TunDevice(const char* dev_name
, const char* dev_type
, const char* ifcfg_lp
, const char* ifcfg_rnmp
) : conf_(dev_name
, dev_type
, ifcfg_lp
, ifcfg_rnmp
)
47 fd_
= ::open(DEFAULT_DEVICE
, O_RDWR
);
49 std::string
msg("can't open device file: ");
50 msg
.append(strerror(errno
));
51 throw std::runtime_error(msg
);
55 memset(&ifr
, 0, sizeof(ifr
));
57 if(conf_
.type_
== TYPE_TUN
) {
58 ifr
.ifr_flags
= IFF_TUN
;
61 else if(conf_
.type_
== TYPE_TAP
) {
62 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
66 throw std::runtime_error("unable to recognize type of device (tun or tap)");
69 strncpy(ifr
.ifr_name
, dev_name
, IFNAMSIZ
);
71 if(!ioctl(fd_
, TUNSETIFF
, &ifr
)) {
72 actual_name_
= ifr
.ifr_name
;
73 } else if(!ioctl(fd_
, (('T' << 8) | 202), &ifr
)) {
74 actual_name_
= ifr
.ifr_name
;
76 std::string
msg("tun/tap device ioctl failed: ");
77 msg
.append(strerror(errno
));
78 throw std::runtime_error(msg
);
81 if(ifcfg_lp
&& ifcfg_rnmp
)
85 TunDevice::~TunDevice()
91 short TunDevice::read(u_int8_t
* buf
, u_int32_t len
)
101 iov
[0].iov_base
= &tpi
;
102 iov
[0].iov_len
= sizeof(tpi
);
103 iov
[1].iov_base
= buf
;
104 iov
[1].iov_len
= len
;
105 return(::readv(fd_
, iov
, 2) - sizeof(tpi
));
108 return(::read(fd_
, buf
, len
));
111 int TunDevice::write(u_int8_t
* buf
, u_int32_t len
)
120 struct iphdr
*hdr
= reinterpret_cast<struct iphdr
*>(buf
);
123 if(hdr
->version
== 4)
124 tpi
.proto
= htons(ETH_P_IP
);
126 tpi
.proto
= htons(ETH_P_IPV6
);
128 iov
[0].iov_base
= &tpi
;
129 iov
[0].iov_len
= sizeof(tpi
);
130 iov
[1].iov_base
= buf
;
131 iov
[1].iov_len
= len
;
132 return(::writev(fd_
, iov
, 2) - sizeof(tpi
));
135 return(::write(fd_
, buf
, len
));
138 const char* TunDevice::getActualName()
140 return actual_name_
.c_str();
143 device_type_t
TunDevice::getType()
148 const char* TunDevice::getTypeString()
155 case TYPE_UNDEF
: return "undef"; break;
156 case TYPE_TUN
: return "tun"; break;
157 case TYPE_TAP
: return "tap"; break;
162 void TunDevice::do_ifconfig()
164 std::string
command("/sbin/ifconfig ");
165 command
.append(actual_name_
);
167 command
.append(conf_
.local_
.toString());
170 if(conf_
.type_
== TYPE_TUN
)
171 command
.append("pointopoint ");
173 command
.append("netmask ");
175 command
.append(conf_
.remote_netmask_
.toString());
176 command
.append(" mtu 1400");
178 system(command
.c_str());