Make sure everything links.
[tinc] / src / linux / device.c
1 /*
2     device.c -- Interaction with Linux ethertap and tun/tap device
3     Copyright (C) 2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2001 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: device.c,v 1.1.2.3 2001/10/27 13:13:35 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <net/if.h>
30 #include <unistd.h>
31 #include <syslog.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34
35 #ifdef HAVE_TUNTAP
36  #ifdef LINUX_IF_TUN_H
37   #include LINUX_IF_TUN_H
38  #else
39   #include <linux/if_tun.h>
40  #endif
41  #define DEFAULT_DEVICE "/dev/misc/net/tun"
42 #else
43  #define DEFAULT_DEVICE "/dev/tap0"
44 #endif
45
46 #include <utils.h>
47 #include "conf.h"
48 #include "net.h"
49 #include "subnet.h"
50
51 #include "system.h"
52
53 #define DEVICE_TYPE_ETHERTAP 0
54 #define DEVICE_TYPE_TUNTAP 1
55
56 int device_fd = -1;
57 int device_type;
58 char *device_fname;
59 char *device_info;
60
61 int device_total_in = 0;
62 int device_total_out = 0;
63
64 subnet_t mymac;
65
66 /*
67   open the local ethertap device
68 */
69 int setup_device(void)
70 {
71   struct ifreq ifr;
72
73 cp
74   if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname))
75     device_fname = DEFAULT_DEVICE;
76
77 cp
78   if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
79     {
80       syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
81       return -1;
82     }
83 cp
84   /* Set default MAC address for ethertap devices */
85
86   mymac.type = SUBNET_MAC;
87   mymac.net.mac.address.x[0] = 0xfe;
88   mymac.net.mac.address.x[1] = 0xfd;
89   mymac.net.mac.address.x[2] = 0x00;
90   mymac.net.mac.address.x[3] = 0x00;
91   mymac.net.mac.address.x[4] = 0x00;
92   mymac.net.mac.address.x[5] = 0x00;
93
94 #ifdef HAVE_TUNTAP
95   /* Ok now check if this is an old ethertap or a new tun/tap thingie */
96
97   memset(&ifr, 0, sizeof(ifr));
98 cp
99   ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
100   if (netname)
101     strncpy(ifr.ifr_name, netname, IFNAMSIZ);
102 cp
103   if (!ioctl(device_fd, TUNSETIFF, (void *) &ifr))
104   {
105       device_info = _("Linux tun/tap device");
106     device_type = DEVICE_TYPE_TUNTAP;
107   }
108   else
109     if (!ioctl(device_fd, (('T'<< 8) | 202), (void *) &ifr))
110     {
111       syslog(LOG_WARNING, _("Old ioctl() request was needed for %s"), device_fname);
112       device_type = DEVICE_TYPE_TUNTAP;
113       device_info = _("Linux tun/tap device");
114     }
115     else
116 #endif
117     {
118       device_info = _("Linux ethertap device");
119       device_type = DEVICE_TYPE_ETHERTAP;
120     }
121
122   syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
123 cp
124   return 0;
125 }
126
127 void close_device(void)
128 {
129 cp
130   close(device_fd);
131 }
132
133 /*
134   read, encrypt and send data that is
135   available through the ethertap device
136 */
137 int read_packet(vpn_packet_t *packet)
138 {
139   int lenin;
140 cp
141   if(device_type == DEVICE_TYPE_TUNTAP)
142     {
143       if((lenin = read(device_fd, packet->data, MTU)) <= 0)
144         {
145           syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
146           return -1;
147         }
148
149       packet->len = lenin;
150     }
151   else /* ethertap */
152     {
153       struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
154
155       if((lenin = readv(device_fd, vector, 2)) <= 0)
156         {
157           syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
158           return -1;
159         }
160
161       packet->len = lenin - 2;
162     }
163
164   device_total_in += packet->len;
165
166   if(debug_lvl >= DEBUG_TRAFFIC)
167     {
168       syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info);
169     }
170
171   return 0;
172 cp
173 }
174
175 int write_packet(vpn_packet_t *packet)
176 {
177 cp
178   if(debug_lvl >= DEBUG_TRAFFIC)
179     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
180            packet->len, device_info);
181
182   if(device_type == DEVICE_TYPE_TUNTAP)
183     {
184       if(write(device_fd, packet->data, packet->len) < 0)
185         {
186           syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
187           return -1;
188         }
189     }
190   else/* ethertap */
191     {
192       struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
193
194       if(writev(device_fd, vector, 2) < 0)
195         {
196           syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
197           return -1;
198         }
199     }
200
201   device_total_out += packet->len;
202 cp
203   return 0;
204 }
205
206 void dump_device_stats(void)
207 {
208 cp
209   syslog(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device_fname);
210   syslog(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
211   syslog(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
212 cp
213 }