Big bad commit:
[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.2 2001/10/27 12:13:17 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 /*
128   read, encrypt and send data that is
129   available through the ethertap device
130 */
131 int read_packet(vpn_packet_t *packet)
132 {
133   int lenin;
134 cp
135   if(device_type == DEVICE_TYPE_TUNTAP)
136     {
137       if((lenin = read(device_fd, packet->data, MTU)) <= 0)
138         {
139           syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
140           return -1;
141         }
142
143       packet->len = lenin;
144     }
145   else /* ethertap */
146     {
147       struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
148
149       if((lenin = readv(device_fd, vector, 2)) <= 0)
150         {
151           syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
152           return -1;
153         }
154
155       packet->len = lenin - 2;
156     }
157
158   device_total_in += packet->len;
159
160   if(debug_lvl >= DEBUG_TRAFFIC)
161     {
162       syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info);
163     }
164
165   return 0;
166 cp
167 }
168
169 int write_packet(vpn_packet_t *packet)
170 {
171 cp
172   if(debug_lvl >= DEBUG_TRAFFIC)
173     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
174            packet->len, device_info);
175
176   if(device_type == DEVICE_TYPE_TUNTAP)
177     {
178       if(write(device_fd, packet->data, packet->len) < 0)
179         {
180           syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
181           return -1;
182         }
183     }
184   else/* ethertap */
185     {
186       struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
187
188       if(writev(device_fd, vector, 2) < 0)
189         {
190           syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
191           return -1;
192         }
193     }
194
195   device_total_out += packet->len;
196 cp
197   return 0;
198 }