Imported Upstream version 0.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-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
23  *  Anytun is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 #include <string.h>
33 #include <sstream>
34
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 #include <arpa/inet.h>
38 #include <errno.h>
39 #include <net/if.h>
40 #include <linux/ip.h>
41 #include <linux/if_ether.h>
42 #include <linux/if_tun.h>
43 #define DEFAULT_DEVICE "/dev/net/tun"
44
45 #include "tunDevice.h"
46 #include "threadUtils.hpp"
47 #include "log.h"
48 #include "anytunError.h"
49
50 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)
51 {
52         struct ifreq ifr;
53         memset(&ifr, 0, sizeof(ifr));
54
55   if(conf_.type_ == TYPE_TUN) {
56     ifr.ifr_flags = IFF_TUN;
57     with_pi_ = true;
58   } 
59   else if(conf_.type_ == TYPE_TAP) {
60     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
61     with_pi_ = false;
62   } 
63   else
64     AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
65
66         if(dev_name != "")
67                 strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ);
68
69         fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
70         if(fd_ < 0)
71     AnytunError::throwErr() << "can't open device file (" << DEFAULT_DEVICE  << "): " << AnytunErrno(errno);
72
73         if(!ioctl(fd_, TUNSETIFF, &ifr)) {
74                 actual_name_ = ifr.ifr_name;
75         } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
76                 actual_name_ = ifr.ifr_name;
77         } else {
78     ::close(fd_);
79     AnytunError::throwErr() << "tun/tap device ioctl failed: " << AnytunErrno(errno);
80   }
81   actual_node_ = DEFAULT_DEVICE;
82
83   if(ifcfg_addr != "")
84     do_ifconfig();
85 }
86
87 TunDevice::~TunDevice()
88 {
89   if(fd_ > 0)
90     ::close(fd_);
91 }
92
93 int TunDevice::fix_return(int ret, size_t pi_length) const
94 {
95   if(ret < 0)
96     return ret;
97
98   return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
99 }
100
101 int TunDevice::read(u_int8_t* buf, u_int32_t len)
102 {
103   if(fd_ < 0)
104     return -1;
105
106   if(with_pi_)
107   {
108     struct iovec iov[2];
109     struct tun_pi tpi;
110     
111     iov[0].iov_base = &tpi;
112     iov[0].iov_len = sizeof(tpi);
113     iov[1].iov_base = buf;
114     iov[1].iov_len = len;
115     return(fix_return(::readv(fd_, iov, 2), sizeof(tpi)));
116   }
117   else
118     return(::read(fd_, buf, len));
119 }
120
121 int TunDevice::write(u_int8_t* buf, u_int32_t len)
122 {
123   if(fd_ < 0)
124     return -1;
125
126   if(!buf)
127     return 0;
128
129   if(with_pi_)
130   {
131     struct iovec iov[2];
132     struct tun_pi tpi;
133     struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
134     
135     tpi.flags = 0;
136     if(hdr->version == 4)
137       tpi.proto = htons(ETH_P_IP);
138     else
139       tpi.proto = htons(ETH_P_IPV6);
140     
141     iov[0].iov_base = &tpi;
142     iov[0].iov_len = sizeof(tpi);
143     iov[1].iov_base = buf;
144     iov[1].iov_len = len;
145     return(fix_return(::writev(fd_, iov, 2), sizeof(tpi)));
146   }
147   else
148     return(::write(fd_, buf, len));
149 }
150
151 void TunDevice::init_post()
152 {
153 // nothing to be done here
154 }
155
156 void TunDevice::do_ifconfig()
157 {
158   std::ostringstream command;
159   command << "/sbin/ifconfig " << actual_name_ << " " << conf_.addr_.toString()
160           << " netmask " << conf_.netmask_.toString() << " mtu " << conf_.mtu_;
161
162   int result = system(command.str().c_str());
163   if(result == -1)
164     cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig failed: " << AnytunErrno(errno);
165   else {
166     if(WIFEXITED(result))
167       cLog.msg(Log::PRIO_NOTICE) << "ifconfig returned " << WEXITSTATUS(result);  
168     else if(WIFSIGNALED(result))
169       cLog.msg(Log::PRIO_NOTICE) << "ifconfig terminated after signal " << WTERMSIG(result);
170     else
171       cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig: unkown error";
172   }
173 }