K&R style braces.
[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 "net.h"
27 #include "logger.h"
28 #include "utils.h"
29 #include "route.h"
30
31 int device_fd = -1;
32 static int listen_fd = -1;
33 static int request_fd = -1;
34 static int data_fd = -1;
35 static int write_fd = -1;
36 static int state = 0;
37 char *device = NULL;
38 char *iface = NULL;
39 static char *device_info;
40
41 extern char *identname;
42 extern bool running;
43
44 static int device_total_in = 0;
45 static int device_total_out = 0;
46
47 enum request_type { REQ_NEW_CONTROL };
48
49 static struct request {
50   uint32_t magic;
51   uint32_t version;
52   enum request_type type;
53   struct sockaddr_un sock;
54 } request;
55
56 static struct sockaddr_un data_sun;
57
58 bool setup_device(void) {
59         struct sockaddr_un listen_sun;
60         static const int one = 1;
61         struct {
62                 char zero;
63                 int pid;
64                 int usecs;
65         } name;
66         struct timeval tv;
67
68         cp();
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         cp();
156
157         if(listen_fd >= 0)
158                 close(listen_fd);
159
160         if(request_fd >= 0)
161                 close(request_fd);
162
163         if(data_fd >= 0)
164                 close(data_fd);
165
166         if(write_fd >= 0)
167                 close(write_fd);
168
169         unlink(device);
170
171         free(device);
172         if(iface) free(iface);
173 }
174
175 bool read_packet(vpn_packet_t *packet) {
176         int lenin;
177
178         cp();
179
180         switch(state) {
181                 case 0: {
182                         struct sockaddr sa;
183                         int salen = sizeof sa;
184
185                         request_fd = accept(listen_fd, &sa, &salen);
186                         if(request_fd < 0) {
187                                 logger(LOG_ERR, _("Could not accept connection to %s %s: %s"), device_info, device, strerror(errno));
188                                 return false;
189                         }
190
191                         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
192                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
193                                 running = false;
194                                 return false;
195                         }
196
197                         close(listen_fd);
198                         listen_fd = -1;
199                         device_fd = request_fd;
200                         state = 1;
201
202                         return false;
203                 }
204
205                 case 1: {
206                         if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
207                                 logger(LOG_ERR, _("Error while reading request from %s %s: %s"), device_info,
208                                            device, strerror(errno));
209                                 running = false;
210                                 return false;
211                         }
212
213                         if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
214                                 logger(LOG_ERR, _("Unknown magic %x, version %d, request type %d from %s %s"),
215                                                 request.magic, request.version, request.type, device_info, device);
216                                 running = false;
217                                 return false;
218                         }
219
220                         if(connect(write_fd, &request.sock, sizeof request.sock) < 0) {
221                                 logger(LOG_ERR, _("Could not bind write %s: %s"), device_info, strerror(errno));
222                                 running = false;
223                                 return false;
224                         }
225
226                         write(request_fd, &data_sun, sizeof data_sun);
227                         device_fd = data_fd;
228
229                         logger(LOG_INFO, _("Connection with UML established"));
230
231                         state = 2;
232                         return false;
233                 }
234
235                 case 2: {
236                         if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
237                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
238                                            device, strerror(errno));
239                                 running = false;
240                                 return false;
241                         }
242
243                         packet->len = lenin;
244
245                         device_total_in += packet->len;
246
247                         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
248                                            device_info);
249
250                         return true;
251                 }
252         }
253 }
254
255 bool write_packet(vpn_packet_t *packet) {
256         cp();
257
258         if(state != 2) {
259                 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Dropping packet of %d bytes to %s: not connected to UML yet"),
260                                 packet->len, device_info);
261                 return false;
262         }
263
264         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
265                            packet->len, device_info);
266
267         if(write(write_fd, packet->data, packet->len) < 0) {
268                 if(errno != EINTR && errno != EAGAIN) {
269                         logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno));
270                         running = false;
271                 }
272
273                 return false;
274         }
275
276         device_total_out += packet->len;
277
278         return true;
279 }
280
281 void dump_device_stats(void) {
282         cp();
283
284         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
285         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
286         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
287 }