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