Replace remaining sizeof foo with sizeof(foo).
[oweals/tinc.git] / src / uml_device.c
1 /*
2     device.c -- UML network socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2017 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
66         get_config_string(lookup_config(config_tree, "Interface"), &iface);
67
68         device_info = "UML network socket";
69
70         if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
71                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
72                 event_exit();
73                 return false;
74         }
75
76 #ifdef FD_CLOEXEC
77         fcntl(write_fd, F_SETFD, FD_CLOEXEC);
78 #endif
79
80         setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
81
82         if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
83                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
84                 event_exit();
85                 return false;
86         }
87
88         if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
89                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
90                 event_exit();
91                 return false;
92         }
93
94 #ifdef FD_CLOEXEC
95         fcntl(data_fd, F_SETFD, FD_CLOEXEC);
96 #endif
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(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
102                 event_exit();
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(DEBUG_ALWAYS, LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
115                 event_exit();
116                 return false;
117         }
118
119         if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
120                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device_info,
121                        strerror(errno));
122                 return false;
123         }
124
125 #ifdef FD_CLOEXEC
126         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
127 #endif
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(DEBUG_ALWAYS, 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
139         if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
140                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
141                 return false;
142         }
143
144         if(listen(listen_fd, 1) < 0) {
145                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
146                 return false;
147         }
148
149         device_fd = listen_fd;
150         state = 0;
151
152         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
153
154         if(routing_mode == RMODE_ROUTER) {
155                 overwrite_mac = true;
156         }
157
158         return true;
159 }
160
161 void close_device(void) {
162         if(listen_fd >= 0) {
163                 close(listen_fd);
164                 listen_fd = -1;
165         }
166
167         if(request_fd >= 0) {
168                 close(request_fd);
169                 request_fd = -1;
170         }
171
172         if(data_fd >= 0) {
173                 close(data_fd);
174                 data_fd = -1;
175         }
176
177         if(write_fd >= 0) {
178                 close(write_fd);
179                 write_fd = -1;
180         }
181
182         unlink(device);
183
184         free(device);
185         device = NULL;
186
187         if(iface) {
188                 free(iface);
189                 iface = NULL;
190         }
191
192         device_info = NULL;
193 }
194
195 static bool read_packet(vpn_packet_t *packet) {
196         int inlen;
197
198         switch(state) {
199         case 0: {
200                 struct sockaddr sa;
201                 socklen_t salen = sizeof(sa);
202
203                 request_fd = accept(listen_fd, &sa, &salen);
204
205                 if(request_fd < 0) {
206                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
207                         return false;
208                 }
209
210 #ifdef FD_CLOEXEC
211                 fcntl(request_fd, F_SETFD, FD_CLOEXEC);
212 #endif
213
214                 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
215                         logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
216                         event_exit();
217                         return false;
218                 }
219
220                 close(listen_fd);
221                 listen_fd = -1;
222                 device_fd = request_fd;
223                 state = 1;
224
225                 return false;
226         }
227
228         case 1: {
229                 if((inlen = read(request_fd, &request, sizeof(request))) != sizeof(request)) {
230                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading request from %s %s: %s", device_info,
231                                device, strerror(errno));
232                         event_exit();
233                         return false;
234                 }
235
236                 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
237                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
238                                request.magic, request.version, request.type, device_info, device);
239                         event_exit();
240                         return false;
241                 }
242
243                 if(connect(write_fd, (struct sockkadr *)&request.sock, sizeof(request.sock)) < 0) {
244                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
245                         event_exit();
246                         return false;
247                 }
248
249                 write(request_fd, &data_sun, sizeof(data_sun));
250                 device_fd = data_fd;
251
252                 logger(DEBUG_ALWAYS, LOG_INFO, "Connection with UML established");
253
254                 state = 2;
255                 return false;
256         }
257
258         case 2: {
259                 if((inlen = read(data_fd, DATA(packet), MTU)) <= 0) {
260                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
261                                device, strerror(errno));
262                         event_exit();
263                         return false;
264                 }
265
266                 packet->len = inlen;
267
268                 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
269                        device_info);
270
271                 return true;
272         }
273
274         default:
275                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid value for state variable in " __FILE__);
276                 abort();
277         }
278 }
279
280 static bool write_packet(vpn_packet_t *packet) {
281         if(state != 2) {
282                 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
283                        packet->len, device_info);
284                 return false;
285         }
286
287         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
288                packet->len, device_info);
289
290         if(write(write_fd, DATA(packet), packet->len) < 0) {
291                 if(errno != EINTR && errno != EAGAIN) {
292                         logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
293                         event_exit();
294                 }
295
296                 return false;
297         }
298
299         return true;
300 }
301
302 const devops_t uml_devops = {
303         .setup = setup_device,
304         .close = close_device,
305         .read = read_packet,
306         .write = write_packet,
307 };