Fix building tinc and running tests on Solaris
[tinc] / src / fd_device.c
1 /*
2     fd_device.c -- Interaction with Android tun fd
3     Copyright (C)   2001-2005   Ivo Timmermans,
4                     2001-2022   Guus Sliepen <guus@tinc-vpn.org>
5                     2009        Grzegorz Dymarek <gregd72002@googlemail.com>
6                     2016-2020   Pacien TRAN-GIRARD <pacien@pacien.net>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include <sys/un.h>
26
27 #include "conf.h"
28 #include "device.h"
29 #include "ethernet.h"
30 #include "logger.h"
31 #include "net.h"
32 #include "route.h"
33
34 struct unix_socket_addr {
35         size_t size;
36         struct sockaddr_un addr;
37 };
38
39 static int read_fd(int socket) {
40         char iobuf;
41         struct iovec iov = {0};
42         char cmsgbuf[CMSG_SPACE(sizeof(device_fd))];
43         struct msghdr msg = {0};
44         ssize_t ret;
45         struct cmsghdr *cmsgptr;
46
47         iov.iov_base = &iobuf;
48         iov.iov_len = 1;
49         msg.msg_iov = &iov;
50         msg.msg_iovlen = 1;
51         msg.msg_control = cmsgbuf;
52         msg.msg_controllen = sizeof(cmsgbuf);
53
54         if((ret = recvmsg(socket, &msg, 0)) < 1) {
55                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read from unix socket (error %ld)!", (long)ret);
56                 return -1;
57         }
58
59 #ifdef IP_RECVERR
60
61         if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) {
62 #else
63
64         if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB)) {
65 #endif
66                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while receiving message (flags %d)!", msg.msg_flags);
67                 return -1;
68         }
69
70         cmsgptr = CMSG_FIRSTHDR(&msg);
71
72         if(cmsgptr->cmsg_level != SOL_SOCKET) {
73                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG level: %d, expected %d!",
74                        cmsgptr->cmsg_level, SOL_SOCKET);
75                 return -1;
76         }
77
78         if(cmsgptr->cmsg_type != SCM_RIGHTS) {
79                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG type: %d, expected %d!",
80                        cmsgptr->cmsg_type, SCM_RIGHTS);
81                 return -1;
82         }
83
84         if(cmsgptr->cmsg_len != CMSG_LEN(sizeof(device_fd))) {
85                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG data length: %lu, expected %lu!",
86                        (unsigned long)cmsgptr->cmsg_len, (unsigned long)CMSG_LEN(sizeof(device_fd)));
87                 return -1;
88         }
89
90         return *(int *) CMSG_DATA(cmsgptr);
91 }
92
93 static int receive_fd(struct unix_socket_addr socket_addr) {
94         int socketfd;
95         int ret;
96         int result;
97
98         if((socketfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
99                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open stream socket (error %d)!", socketfd);
100                 return -1;
101         }
102
103         if((ret = connect(socketfd, (struct sockaddr *) &socket_addr.addr, socket_addr.size)) < 0) {
104                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect to Unix socket (error %d)!", ret);
105                 result = -1;
106                 goto end;
107         }
108
109         result = read_fd(socketfd);
110
111 end:
112         close(socketfd);
113         return result;
114 }
115
116 static struct unix_socket_addr parse_socket_addr(const char *path) {
117         struct sockaddr_un socket_addr = {
118                 .sun_family = AF_UNIX,
119         };
120         size_t path_length;
121
122         if(strlen(path) >= sizeof(socket_addr.sun_path)) {
123                 logger(DEBUG_ALWAYS, LOG_ERR, "Unix socket path too long!");
124                 return (struct unix_socket_addr) {
125                         0
126                 };
127         }
128
129         strncpy(socket_addr.sun_path, path, sizeof(socket_addr.sun_path));
130
131         if(path[0] == '@') {
132                 /* abstract namespace socket */
133                 socket_addr.sun_path[0] = '\0';
134                 path_length = strlen(path);
135         } else {
136                 /* filesystem path with NUL terminator */
137                 path_length = strlen(path) + 1;
138         }
139
140         return (struct unix_socket_addr) {
141                 .size = offsetof(struct sockaddr_un, sun_path) + path_length,
142                 .addr = socket_addr
143         };
144 }
145
146 static bool setup_device(void) {
147         if(routing_mode == RMODE_SWITCH) {
148                 logger(DEBUG_ALWAYS, LOG_ERR, "Switch mode not supported (requires unsupported TAP device)!");
149                 return false;
150         }
151
152         if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
153                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read device from configuration!");
154                 return false;
155         }
156
157         /* device is either directly a file descriptor or an unix socket to read it from */
158         if(sscanf(device, "%d", &device_fd) != 1) {
159                 logger(DEBUG_ALWAYS, LOG_INFO, "Receiving fd from Unix socket at %s.", device);
160                 device_fd = receive_fd(parse_socket_addr(device));
161         }
162
163         if(device_fd < 0) {
164                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s!", device, strerror(errno));
165                 return false;
166         }
167
168         logger(DEBUG_ALWAYS, LOG_INFO, "fd/%d adapter set up.", device_fd);
169
170         return true;
171 }
172
173 static void close_device(void) {
174         close(device_fd);
175         device_fd = -1;
176         free(iface);
177         iface = NULL;
178         free(device);
179         device = NULL;
180 }
181
182 static inline uint16_t get_ip_ethertype(vpn_packet_t *packet) {
183         switch(DATA(packet)[ETH_HLEN] >> 4) {
184         case 4:
185                 return ETH_P_IP;
186
187         case 6:
188                 return ETH_P_IPV6;
189
190         default:
191                 return ETH_P_MAX;
192         }
193 }
194
195 static inline void set_etherheader(vpn_packet_t *packet, uint16_t ethertype) {
196         memset(DATA(packet), 0, ETH_HLEN - ETHER_TYPE_LEN);
197
198         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN] = (ethertype >> 8) & 0xFF;
199         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN + 1] = ethertype & 0xFF;
200 }
201
202 static bool read_packet(vpn_packet_t *packet) {
203         ssize_t lenin = read(device_fd, DATA(packet) + ETH_HLEN, MTU - ETH_HLEN);
204
205         if(lenin <= 0) {
206                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from fd/%d: %s!", device_fd, strerror(errno));
207                 return false;
208         }
209
210         uint16_t ethertype = get_ip_ethertype(packet);
211
212         if(ethertype == ETH_P_MAX) {
213                 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version while reading packet from fd/%d!", device_fd);
214                 return false;
215         }
216
217         set_etherheader(packet, ethertype);
218         packet->len = lenin + ETH_HLEN;
219
220         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from fd/%d.", packet->len, device_fd);
221
222         return true;
223 }
224
225 static bool write_packet(vpn_packet_t *packet) {
226         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to fd/%d.", packet->len, device_fd);
227
228         if(write(device_fd, DATA(packet) + ETH_HLEN, packet->len - ETH_HLEN) < 0) {
229                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to fd/%d: %s!", device_fd, strerror(errno));
230                 return false;
231         }
232
233         return true;
234 }
235
236 const devops_t fd_devops = {
237         .setup = setup_device,
238         .close = close_device,
239         .read = read_packet,
240         .write = write_packet,
241 };