Update copyright notices.
[tinc] / src / uml_device.c
1 /*
2     device.c -- UML network socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2013 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 "device.h"
27 #include "names.h"
28 #include "net.h"
29 #include "logger.h"
30 #include "utils.h"
31 #include "route.h"
32 #include "xalloc.h"
33
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 static char *device_info;
40
41 enum request_type { REQ_NEW_CONTROL };
42
43 static struct request {
44   uint32_t magic;
45   uint32_t version;
46   enum request_type type;
47   struct sockaddr_un sock;
48 } request;
49
50 static struct sockaddr_un data_sun;
51
52 static bool setup_device(void) {
53         struct sockaddr_un listen_sun;
54         static const int one = 1;
55         struct {
56                 char zero;
57                 int pid;
58                 int usecs;
59         } name;
60         struct timeval tv;
61
62         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
63                 xasprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
64
65         get_config_string(lookup_config(config_tree, "Interface"), &iface);
66
67         device_info = "UML network socket";
68
69         if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
70                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
71                 event_exit();
72                 return false;
73         }
74
75 #ifdef FD_CLOEXEC
76         fcntl(write_fd, F_SETFD, FD_CLOEXEC);
77 #endif
78
79         setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
80
81         if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
82                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
83                 event_exit();
84                 return false;
85         }
86
87         if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
88                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
89                 event_exit();
90                 return false;
91         }
92
93 #ifdef FD_CLOEXEC
94         fcntl(data_fd, F_SETFD, FD_CLOEXEC);
95 #endif
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(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
101                 event_exit();
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(DEBUG_ALWAYS, LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
114                 event_exit();
115                 return false;
116         }
117
118         if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
119                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device_info,
120                            strerror(errno));
121                 return false;
122         }
123
124 #ifdef FD_CLOEXEC
125         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
126 #endif
127
128         setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
129
130         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
131                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
132                 return false;
133         }
134
135         listen_sun.sun_family = AF_UNIX;
136         strncpy(listen_sun.sun_path, device, sizeof listen_sun.sun_path);
137         if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof listen_sun) < 0) {
138                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
139                 return false;
140         }
141
142         if(listen(listen_fd, 1) < 0) {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
144                 return false;
145         }
146
147         device_fd = listen_fd;
148         state = 0;
149
150         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
151
152         if(routing_mode == RMODE_ROUTER)
153                 overwrite_mac = true;
154
155         return true;
156 }
157
158 void close_device(void) {
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 static bool read_packet(vpn_packet_t *packet) {
178         int inlen;
179
180         switch(state) {
181                 case 0: {
182                         struct sockaddr sa;
183                         socklen_t salen = sizeof sa;
184
185                         request_fd = accept(listen_fd, &sa, &salen);
186                         if(request_fd < 0) {
187                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
188                                 return false;
189                         }
190
191 #ifdef FD_CLOEXEC
192                         fcntl(request_fd, F_SETFD, FD_CLOEXEC);
193 #endif
194
195                         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
196                                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
197                                 event_exit();
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((inlen = read(request_fd, &request, sizeof request)) != sizeof request) {
211                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading request from %s %s: %s", device_info,
212                                            device, strerror(errno));
213                                 event_exit();
214                                 return false;
215                         }
216
217                         if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
218                                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
219                                                 request.magic, request.version, request.type, device_info, device);
220                                 event_exit();
221                                 return false;
222                         }
223
224                         if(connect(write_fd, &request.sock, sizeof request.sock) < 0) {
225                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
226                                 event_exit();
227                                 return false;
228                         }
229
230                         write(request_fd, &data_sun, sizeof data_sun);
231                         device_fd = data_fd;
232
233                         logger(DEBUG_ALWAYS, LOG_INFO, "Connection with UML established");
234
235                         state = 2;
236                         return false;
237                 }
238
239                 case 2: {
240                         if((inlen = read(data_fd, packet->data, MTU)) <= 0) {
241                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
242                                            device, strerror(errno));
243                                 event_exit();
244                                 return false;
245                         }
246
247                         packet->len = inlen;
248
249                         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
250                                            device_info);
251
252                         return true;
253                 }
254
255                 default:
256                         logger(DEBUG_ALWAYS, LOG_ERR, "Invalid value for state variable in " __FILE__);
257                         abort();
258         }
259 }
260
261 static bool write_packet(vpn_packet_t *packet) {
262         if(state != 2) {
263                 logger(DEBUG_TRAFFIC, 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         logger(DEBUG_TRAFFIC, 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(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
274                         event_exit();
275                 }
276
277                 return false;
278         }
279
280         return true;
281 }
282
283 const devops_t uml_devops = {
284         .setup = setup_device,
285         .close = close_device,
286         .read = read_packet,
287         .write = write_packet,
288 };