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