Allow linking with multiple device drivers.
[tinc] / src / uml_device.c
1 /*
2     device.c -- UML network socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2011 Guus Sliepen <guus@tinc-vpn.org>
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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include <sys/un.h>
24
25 #include "conf.h"
26 #include "device.h"
27 #include "net.h"
28 #include "logger.h"
29 #include "utils.h"
30 #include "route.h"
31 #include "xalloc.h"
32
33 static int listen_fd = -1;
34 static int request_fd = -1;
35 static int data_fd = -1;
36 static int write_fd = -1;
37 static int state = 0;
38 static char *device_info;
39
40 extern char *identname;
41 extern volatile bool running;
42
43 static uint64_t device_total_in = 0;
44 static uint64_t device_total_out = 0;
45
46 enum request_type { REQ_NEW_CONTROL };
47
48 static struct request {
49   uint32_t magic;
50   uint32_t version;
51   enum request_type type;
52   struct sockaddr_un sock;
53 } request;
54
55 static struct sockaddr_un data_sun;
56
57 static bool setup_device(void) {
58         struct sockaddr_un listen_sun;
59         static const int one = 1;
60         struct {
61                 char zero;
62                 int pid;
63                 int usecs;
64         } name;
65         struct timeval tv;
66
67         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
68                 xasprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
69
70         get_config_string(lookup_config(config_tree, "Interface"), &iface);
71
72         device_info = "UML network socket";
73
74         if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
75                 logger(LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
76                 running = false;
77                 return false;
78         }
79
80         setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
81
82         if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
83                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
84                 running = false;
85                 return false;
86         }
87
88         if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
89                 logger(LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
90                 running = false;
91                 return false;
92         }
93
94         setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
95
96         if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
97                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
98                 running = false;
99                 return false;
100         }
101
102         name.zero = 0;
103         name.pid = getpid();
104         gettimeofday(&tv, NULL);
105         name.usecs = tv.tv_usec;
106         data_sun.sun_family = AF_UNIX;
107         memcpy(&data_sun.sun_path, &name, sizeof name);
108         
109         if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof data_sun) < 0) {
110                 logger(LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
111                 running = false;
112                 return false;
113         }
114
115         if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
116                 logger(LOG_ERR, "Could not open %s: %s", device_info,
117                            strerror(errno));
118                 return false;
119         }
120
121         setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
122
123         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
124                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
125                 return false;
126         }
127
128         listen_sun.sun_family = AF_UNIX;
129         strncpy(listen_sun.sun_path, device, sizeof listen_sun.sun_path);
130         if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof listen_sun) < 0) {
131                 logger(LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
132                 return false;
133         }
134
135         if(listen(listen_fd, 1) < 0) {
136                 logger(LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
137                 return false;
138         }
139
140         device_fd = listen_fd;
141         state = 0;
142
143         logger(LOG_INFO, "%s is a %s", device, device_info);
144
145         if(routing_mode == RMODE_ROUTER)
146                 overwrite_mac = true;
147
148         return true;
149 }
150
151 void close_device(void) {
152         if(listen_fd >= 0)
153                 close(listen_fd);
154
155         if(request_fd >= 0)
156                 close(request_fd);
157
158         if(data_fd >= 0)
159                 close(data_fd);
160
161         if(write_fd >= 0)
162                 close(write_fd);
163
164         unlink(device);
165
166         free(device);
167         if(iface) free(iface);
168 }
169
170 static bool read_packet(vpn_packet_t *packet) {
171         int lenin;
172
173         switch(state) {
174                 case 0: {
175                         struct sockaddr sa;
176                         socklen_t salen = sizeof sa;
177
178                         request_fd = accept(listen_fd, &sa, &salen);
179                         if(request_fd < 0) {
180                                 logger(LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
181                                 return false;
182                         }
183
184                         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
185                                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
186                                 running = false;
187                                 return false;
188                         }
189
190                         close(listen_fd);
191                         listen_fd = -1;
192                         device_fd = request_fd;
193                         state = 1;
194
195                         return false;
196                 }
197
198                 case 1: {
199                         if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
200                                 logger(LOG_ERR, "Error while reading request from %s %s: %s", device_info,
201                                            device, strerror(errno));
202                                 running = false;
203                                 return false;
204                         }
205
206                         if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
207                                 logger(LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
208                                                 request.magic, request.version, request.type, device_info, device);
209                                 running = false;
210                                 return false;
211                         }
212
213                         if(connect(write_fd, &request.sock, sizeof request.sock) < 0) {
214                                 logger(LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
215                                 running = false;
216                                 return false;
217                         }
218
219                         write(request_fd, &data_sun, sizeof data_sun);
220                         device_fd = data_fd;
221
222                         logger(LOG_INFO, "Connection with UML established");
223
224                         state = 2;
225                         return false;
226                 }
227
228                 case 2: {
229                         if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
230                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
231                                            device, strerror(errno));
232                                 running = false;
233                                 return false;
234                         }
235
236                         packet->len = lenin;
237
238                         device_total_in += packet->len;
239
240                         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
241                                            device_info);
242
243                         return true;
244                 }
245
246                 default:
247                         logger(LOG_ERR, "Invalid value for state variable in " __FILE__);
248                         abort();
249         }
250 }
251
252 static bool write_packet(vpn_packet_t *packet) {
253         if(state != 2) {
254                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
255                                 packet->len, device_info);
256                 return false;
257         }
258
259         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
260                            packet->len, device_info);
261
262         if(write(write_fd, packet->data, packet->len) < 0) {
263                 if(errno != EINTR && errno != EAGAIN) {
264                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
265                         running = false;
266                 }
267
268                 return false;
269         }
270
271         device_total_out += packet->len;
272
273         return true;
274 }
275
276 static void dump_device_stats(void) {
277         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
278         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
279         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
280 }
281
282 const devops_t uml_devops = {
283         .setup = setup_device,
284         .close = close_device,
285         .read = read_packet,
286         .write = write_packet,
287         .dump_stats = dump_device_stats,
288 };