Imported Upstream version 0.3.5
[anytun.git] / src / linux / tunDevice.cpp
1 /*
2  *  anytun
3  *
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 methods 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.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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
22  *  any later version.
23  *
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.
28  *
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/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include <string.h>
47 #include <sstream>
48 #include <boost/assign.hpp>
49
50 #include <fcntl.h>
51 #include <sys/ioctl.h>
52 #include <arpa/inet.h>
53 #include <errno.h>
54 #include <net/if.h>
55 #include <linux/ip.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_tun.h>
58 #define DEFAULT_DEVICE "/dev/net/tun"
59
60 #include "tunDevice.h"
61 #include "threadUtils.hpp"
62 #include "log.h"
63 #include "anytunError.h"
64 #include "sysExec.h"
65
66 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)
67 {
68   struct ifreq ifr;
69   memset(&ifr, 0, sizeof(ifr));
70
71   if(conf_.type_ == TYPE_TUN) {
72     ifr.ifr_flags = IFF_TUN;
73     with_pi_ = true;
74   } else if(conf_.type_ == TYPE_TAP) {
75     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
76     with_pi_ = false;
77   } else {
78     AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
79   }
80
81   if(dev_name != "") {
82     strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
83   }
84
85   fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
86   if(fd_ < 0) {
87     AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE  << "): " << AnytunErrno(errno);
88   }
89
90   if(!ioctl(fd_, TUNSETIFF, &ifr)) {
91     actual_name_ = ifr.ifr_name;
92   } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
93     actual_name_ = ifr.ifr_name;
94   } else {
95     ::close(fd_);
96     AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
97   }
98   actual_node_ = DEFAULT_DEVICE;
99
100   if(ifcfg_addr != "") {
101     do_ifconfig();
102   }
103 }
104
105 TunDevice::~TunDevice()
106 {
107   if(fd_ > 0) {
108     ::close(fd_);
109   }
110 }
111
112 int TunDevice::fix_return(int ret, size_t pi_length) const
113 {
114   if(ret < 0) {
115     return ret;
116   }
117
118   return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
119 }
120
121 int TunDevice::read(uint8_t* buf, uint32_t len)
122 {
123   if(fd_ < 0) {
124     return -1;
125   }
126
127   if(with_pi_) {
128     struct iovec iov[2];
129     struct tun_pi tpi;
130
131     iov[0].iov_base = &tpi;
132     iov[0].iov_len = sizeof(tpi);
133     iov[1].iov_base = buf;
134     iov[1].iov_len = len;
135     return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
136   } else {
137     return(::read(fd_, buf, len));
138   }
139 }
140
141 int TunDevice::write(uint8_t* buf, uint32_t len)
142 {
143   if(fd_ < 0) {
144     return -1;
145   }
146
147   if(!buf) {
148     return 0;
149   }
150
151   if(with_pi_) {
152     struct iovec iov[2];
153     struct tun_pi tpi;
154     struct iphdr* hdr = reinterpret_cast<struct iphdr*>(buf);
155
156     tpi.flags = 0;
157     if(hdr->version == 4) {
158       tpi.proto = htons(ETH_P_IP);
159     } else {
160       tpi.proto = htons(ETH_P_IPV6);
161     }
162
163     iov[0].iov_base = &tpi;
164     iov[0].iov_len = sizeof(tpi);
165     iov[1].iov_base = buf;
166     iov[1].iov_len = len;
167     return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
168   } else {
169     return(::write(fd_, buf, len));
170   }
171 }
172
173 void TunDevice::init_post()
174 {
175   // nothing to be done here
176 }
177
178 void TunDevice::do_ifconfig()
179 {
180   std::ostringstream mtu_ss;
181   mtu_ss << conf_.mtu_;
182   StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
183   sys_exec_ = new SysExec("/sbin/ifconfig", args);
184 }
185
186 void TunDevice::waitUntilReady()
187 {
188   if(sys_exec_) {
189     SysExec::waitAndDestroy(sys_exec_);
190   }
191 }