2 device.c -- UML network socket
3 Copyright (C) 2002-2005 Ivo Timmermans,
4 2002-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
21 #include "../system.h"
26 #include "../device.h"
29 #include "../logger.h"
32 #include "../xalloc.h"
34 static int listen_fd = -1;
35 static int request_fd = -1;
36 static int data_fd = -1;
37 static int write_fd = -1;
39 static const char *device_info = "UML network socket";
41 enum request_type { REQ_NEW_CONTROL };
43 static struct request {
46 enum request_type type;
47 struct sockaddr_un sock;
50 static struct sockaddr_un data_sun = {
51 .sun_family = AF_UNIX,
54 static bool setup_device(void) {
55 struct sockaddr_un listen_sun = {
56 .sun_family = AF_UNIX,
58 static const int one = 1;
66 if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
67 xasprintf(&device, RUNSTATEDIR "/%s.umlsocket", identname);
70 get_config_string(lookup_config(&config_tree, "Interface"), &iface);
72 if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
73 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
79 fcntl(write_fd, F_SETFD, FD_CLOEXEC);
82 setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
84 if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
85 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
90 if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
91 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
97 fcntl(data_fd, F_SETFD, FD_CLOEXEC);
100 setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
102 if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
103 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
110 gettimeofday(&tv, NULL);
111 name.usecs = (int) tv.tv_usec;
112 memcpy(&data_sun.sun_path, &name, sizeof(name));
114 if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof(data_sun)) < 0) {
115 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
120 if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
121 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device_info,
127 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
130 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
132 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
133 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
137 if(strlen(device) >= sizeof(listen_sun.sun_path)) {
138 logger(DEBUG_ALWAYS, LOG_ERR, "UML socket filename %s is too long!", device);
142 strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path));
144 if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
145 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
149 if(listen(listen_fd, 1) < 0) {
150 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
154 device_fd = listen_fd;
157 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
159 if(routing_mode == RMODE_ROUTER) {
160 overwrite_mac = true;
166 static void close_device(void) {
172 if(request_fd >= 0) {
198 static bool read_packet(vpn_packet_t *packet) {
202 socklen_t salen = sizeof(sa);
204 request_fd = accept(listen_fd, &sa, &salen);
207 logger(DEBUG_ALWAYS, LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
212 fcntl(request_fd, F_SETFD, FD_CLOEXEC);
215 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
216 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
223 device_fd = request_fd;
230 if(read(request_fd, &request, sizeof(request)) != sizeof(request)) {
231 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading request from %s %s: %s", device_info,
232 device, strerror(errno));
237 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
238 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
239 request.magic, request.version, request.type, device_info, device);
244 if(connect(write_fd, (const struct sockaddr *)&request.sock, sizeof(request.sock)) < 0) {
245 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
250 if(write(request_fd, &data_sun, sizeof(data_sun)) != sizeof(data_sun)) {
251 logger(DEBUG_ALWAYS, LOG_ERR, "Error while responding to request from %s %s: %s", device_info, device, strerror(errno));
258 logger(DEBUG_ALWAYS, LOG_INFO, "Connection with UML established");
265 ssize_t inlen = read(data_fd, DATA(packet), MTU);
268 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
269 device, strerror(errno));
276 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
283 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid value for state variable in " __FILE__);
288 static bool write_packet(vpn_packet_t *packet) {
290 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
291 packet->len, device_info);
295 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
296 packet->len, device_info);
298 if(write(write_fd, DATA(packet), packet->len) < 0) {
299 if(errno != EINTR && errno != EAGAIN) {
300 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
310 const devops_t uml_devops = {
311 .setup = setup_device,
312 .close = close_device,
314 .write = write_packet,