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