Imported Upstream version 0.3.3
[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, u_int16_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   } 
62   else if(conf_.type_ == TYPE_TAP) {
63     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
64     with_pi_ = false;
65   } 
66   else
67     AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
68
69         if(dev_name != "")
70                 strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
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         if(!ioctl(fd_, TUNSETIFF, &ifr)) {
77                 actual_name_ = ifr.ifr_name;
78         } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
79                 actual_name_ = ifr.ifr_name;
80         } else {
81     ::close(fd_);
82     AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
83   }
84   actual_node_ = DEFAULT_DEVICE;
85
86   if(ifcfg_addr != "")
87     do_ifconfig();
88 }
89
90 TunDevice::~TunDevice()
91 {
92   if(fd_ > 0)
93     ::close(fd_);
94 }
95
96 int TunDevice::fix_return(int ret, size_t pi_length) const
97 {
98   if(ret < 0)
99     return ret;
100
101   return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
102 }
103
104 int TunDevice::read(u_int8_t* buf, u_int32_t len)
105 {
106   if(fd_ < 0)
107     return -1;
108
109   if(with_pi_)
110   {
111     struct iovec iov[2];
112     struct tun_pi tpi;
113     
114     iov[0].iov_base = &tpi;
115     iov[0].iov_len = sizeof(tpi);
116     iov[1].iov_base = buf;
117     iov[1].iov_len = len;
118     return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
119   }
120   else
121     return(::read(fd_, buf, len));
122 }
123
124 int TunDevice::write(u_int8_t* buf, u_int32_t len)
125 {
126   if(fd_ < 0)
127     return -1;
128
129   if(!buf)
130     return 0;
131
132   if(with_pi_)
133   {
134     struct iovec iov[2];
135     struct tun_pi tpi;
136     struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
137     
138     tpi.flags = 0;
139     if(hdr->version == 4)
140       tpi.proto = htons(ETH_P_IP);
141     else
142       tpi.proto = htons(ETH_P_IPV6);
143     
144     iov[0].iov_base = &tpi;
145     iov[0].iov_len = sizeof(tpi);
146     iov[1].iov_base = buf;
147     iov[1].iov_len = len;
148     return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
149   }
150   else
151     return(::write(fd_, buf, len));
152 }
153
154 void TunDevice::init_post()
155 {
156 // nothing to be done here
157 }
158
159 void TunDevice::do_ifconfig()
160 {
161   std::ostringstream mtu_ss;
162   mtu_ss << conf_.mtu_;
163   StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
164   sys_exec_ = new SysExec("/sbin/ifconfig", args);
165 }
166
167 void TunDevice::waitUntilReady()
168 {
169   if(sys_exec_)
170     SysExec::waitAndDestroy(sys_exec_);
171 }