Imported Upstream version 0.3.4
[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 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.
12  *
13  *
14  *  Copyright (C) 2007-2009 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
33 #include <string.h>
34 #include <sstream>
35 #include <boost/assign.hpp>
36
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <arpa/inet.h>
40 #include <errno.h>
41 #include <net/if.h>
42 #include <linux/ip.h>
43 #include <linux/if_ether.h>
44 #include <linux/if_tun.h>
45 #define DEFAULT_DEVICE "/dev/net/tun"
46
47 #include "tunDevice.h"
48 #include "threadUtils.hpp"
49 #include "log.h"
50 #include "anytunError.h"
51 #include "sysExec.h"
52
53 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)
54 {
55   struct ifreq ifr;
56   memset(&ifr, 0, sizeof(ifr));
57
58   if(conf_.type_ == TYPE_TUN) {
59     ifr.ifr_flags = IFF_TUN;
60     with_pi_ = true;
61   } else if(conf_.type_ == TYPE_TAP) {
62     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
63     with_pi_ = false;
64   } else {
65     AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
66   }
67
68   if(dev_name != "") {
69     strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
70   }
71
72   fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
73   if(fd_ < 0) {
74     AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE  << "): " << AnytunErrno(errno);
75   }
76
77   if(!ioctl(fd_, TUNSETIFF, &ifr)) {
78     actual_name_ = ifr.ifr_name;
79   } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
80     actual_name_ = ifr.ifr_name;
81   } else {
82     ::close(fd_);
83     AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
84   }
85   actual_node_ = DEFAULT_DEVICE;
86
87   if(ifcfg_addr != "") {
88     do_ifconfig();
89   }
90 }
91
92 TunDevice::~TunDevice()
93 {
94   if(fd_ > 0) {
95     ::close(fd_);
96   }
97 }
98
99 int TunDevice::fix_return(int ret, size_t pi_length) const
100 {
101   if(ret < 0) {
102     return ret;
103   }
104
105   return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
106 }
107
108 int TunDevice::read(uint8_t* buf, uint32_t len)
109 {
110   if(fd_ < 0) {
111     return -1;
112   }
113
114   if(with_pi_) {
115     struct iovec iov[2];
116     struct tun_pi tpi;
117
118     iov[0].iov_base = &tpi;
119     iov[0].iov_len = sizeof(tpi);
120     iov[1].iov_base = buf;
121     iov[1].iov_len = len;
122     return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
123   } else {
124     return(::read(fd_, buf, len));
125   }
126 }
127
128 int TunDevice::write(uint8_t* buf, uint32_t len)
129 {
130   if(fd_ < 0) {
131     return -1;
132   }
133
134   if(!buf) {
135     return 0;
136   }
137
138   if(with_pi_) {
139     struct iovec iov[2];
140     struct tun_pi tpi;
141     struct iphdr* hdr = reinterpret_cast<struct iphdr*>(buf);
142
143     tpi.flags = 0;
144     if(hdr->version == 4) {
145       tpi.proto = htons(ETH_P_IP);
146     } else {
147       tpi.proto = htons(ETH_P_IPV6);
148     }
149
150     iov[0].iov_base = &tpi;
151     iov[0].iov_len = sizeof(tpi);
152     iov[1].iov_base = buf;
153     iov[1].iov_len = len;
154     return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
155   } else {
156     return(::write(fd_, buf, len));
157   }
158 }
159
160 void TunDevice::init_post()
161 {
162   // nothing to be done here
163 }
164
165 void TunDevice::do_ifconfig()
166 {
167   std::ostringstream mtu_ss;
168   mtu_ss << conf_.mtu_;
169   StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
170   sys_exec_ = new SysExec("/sbin/ifconfig", args);
171 }
172
173 void TunDevice::waitUntilReady()
174 {
175   if(sys_exec_) {
176     SysExec::waitAndDestroy(sys_exec_);
177   }
178 }