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