Allow linking with multiple device drivers.
[tinc] / src / vde_device.c
1 /*
2     device.c -- VDE plug
3     Copyright (C) 2011 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 "net.h"
27 #include "logger.h"
28 #include "utils.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 char *device_info;
37
38 extern char *identname;
39 extern volatile bool running;
40
41 static uint64_t device_total_in = 0;
42 static uint64_t device_total_out = 0;
43
44 static bool setup_device(void) {
45         libvdeplug_dynopen(plug);
46
47         if(!plug.dl_handle) {
48                 logger(LOG_ERR, "Could not open libvdeplug library!");
49                 return false;
50         }
51
52         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
53                 xasprintf(&device, LOCALSTATEDIR "/run/vde.ctl");
54
55         get_config_string(lookup_config(config_tree, "Interface"), &iface);
56
57         get_config_int(lookup_config(config_tree, "VDEPort"), &port);
58
59         get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
60
61         device_info = "VDE socket";
62
63         struct vde_open_args args = {
64                 .port = port,
65                 .group = group,
66                 .mode = 0700,
67         };
68
69         conn = plug.vde_open(device, identname, &args);
70         if(!conn) {
71                 logger(LOG_ERR, "Could not open VDE socket %s", device);
72                 return false;
73         }
74
75         device_fd = plug.vde_datafd(conn);
76
77         logger(LOG_INFO, "%s is a %s", device, device_info);
78
79         if(routing_mode == RMODE_ROUTER)
80                 overwrite_mac = true;
81
82         return true;
83 }
84
85 static void close_device(void) {
86         if(conn)
87                 plug.vde_close(conn);
88
89         if(plug.dl_handle)
90                 libvdeplug_dynclose(plug);
91
92         free(device);
93
94         free(iface);
95 }
96
97 static bool read_packet(vpn_packet_t *packet) {
98         int lenin = plug.vde_recv(conn, packet->data, MTU, 0);
99         if(lenin <= 0) {
100                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
101                 running = false;
102                 return false;
103         }
104
105         packet->len = lenin;
106         device_total_in += packet->len;
107         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
108
109         return true;
110 }
111
112 static bool write_packet(vpn_packet_t *packet) {
113         if(plug.vde_send(conn, packet->data, packet->len, 0) < 0) {
114                 if(errno != EINTR && errno != EAGAIN) {
115                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
116                         running = false;
117                 }
118
119                 return false;
120         }
121
122         device_total_out += packet->len;
123
124         return true;
125 }
126
127 static void dump_device_stats(void) {
128         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
129         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
130         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
131 }
132
133 const devops_t vde_devops = {
134         .setup = setup_device,
135         .close = close_device,
136         .read = read_packet,
137         .write = write_packet,
138         .dump_stats = dump_device_stats,
139 };