b5f0785e3b5283498c1f20d4efd50838eb9fd339
[tinc] / src / vde_device.c
1 /*
2     device.c -- VDE plug
3     Copyright (C) 2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <libvdeplug_dyn.h>
23
24 #include "conf.h"
25 #include "device.h"
26 #include "names.h"
27 #include "net.h"
28 #include "logger.h"
29 #include "route.h"
30 #include "xalloc.h"
31
32 static struct vdepluglib plug;
33 static struct vdeconn *conn = NULL;
34 static int port = 0;
35 static char *group = NULL;
36 static const char *device_info = "VDE socket";
37
38 static bool setup_device(void) {
39         libvdeplug_dynopen(plug);
40
41         if(!plug.dl_handle) {
42                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open libvdeplug library!");
43                 return false;
44         }
45
46         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
47                 xasprintf(&device, RUNSTATEDIR "/vde.ctl");
48         }
49
50         get_config_string(lookup_config(config_tree, "Interface"), &iface);
51
52         get_config_int(lookup_config(config_tree, "VDEPort"), &port);
53
54         get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
55
56         struct vde_open_args args = {
57                 .port = port,
58                 .group = group,
59                 .mode = 0700,
60         };
61
62         conn = plug.vde_open(device, identname, &args);
63
64         if(!conn) {
65                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open VDE socket %s", device);
66                 return false;
67         }
68
69         device_fd = plug.vde_datafd(conn);
70
71 #ifdef FD_CLOEXEC
72         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
73 #endif
74
75         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
76
77         if(routing_mode == RMODE_ROUTER) {
78                 overwrite_mac = true;
79         }
80
81         return true;
82 }
83
84 static void close_device(void) {
85         if(conn) {
86                 plug.vde_close(conn);
87                 conn = NULL;
88         }
89
90         if(plug.dl_handle) {
91                 libvdeplug_dynclose(plug);
92         }
93
94         free(device);
95         device = NULL;
96
97         free(iface);
98         iface = NULL;
99
100         device_info = NULL;
101 }
102
103 static bool read_packet(vpn_packet_t *packet) {
104         ssize_t lenin = (ssize_t) plug.vde_recv(conn, DATA(packet), MTU, 0);
105
106         if(lenin <= 0) {
107                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
108                 event_exit();
109                 return false;
110         }
111
112         packet->len = lenin;
113
114         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
115
116         return true;
117 }
118
119 static bool write_packet(vpn_packet_t *packet) {
120         if((ssize_t)plug.vde_send(conn, DATA(packet), packet->len, 0) < 0) {
121                 if(errno != EINTR && errno != EAGAIN) {
122                         logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
123                         event_exit();
124                 }
125
126                 return false;
127         }
128
129         return true;
130 }
131
132 const devops_t vde_devops = {
133         .setup = setup_device,
134         .close = close_device,
135         .read = read_packet,
136         .write = write_packet,
137 };