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