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