Enable and fix many extra warnings supported by GCC and Clang.
[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 #ifdef HAVE_SYS_UN_H
26 #include <sys/un.h>
27
28 #include "conf.h"
29 #include "device.h"
30 #include "ethernet.h"
31 #include "logger.h"
32 #include "net.h"
33 #include "route.h"
34
35 struct unix_socket_addr {
36         size_t size;
37         struct sockaddr_un addr;
38 };
39
40 static int read_fd(int socket) {
41         char iobuf;
42         struct iovec iov = {0};
43         char cmsgbuf[CMSG_SPACE(sizeof(device_fd))];
44         struct msghdr msg = {0};
45         ssize_t ret;
46         struct cmsghdr *cmsgptr;
47
48         iov.iov_base = &iobuf;
49         iov.iov_len = 1;
50         msg.msg_iov = &iov;
51         msg.msg_iovlen = 1;
52         msg.msg_control = cmsgbuf;
53         msg.msg_controllen = sizeof(cmsgbuf);
54
55         if((ret = recvmsg(socket, &msg, 0)) < 1) {
56                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read from unix socket (error %ld)!", (long)ret);
57                 return -1;
58         }
59
60 #ifdef IP_RECVERR
61
62         if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) {
63 #else
64
65         if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB)) {
66 #endif
67                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while receiving message (flags %d)!", msg.msg_flags);
68                 return -1;
69         }
70
71         cmsgptr = CMSG_FIRSTHDR(&msg);
72
73         if(cmsgptr->cmsg_level != SOL_SOCKET) {
74                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG level: %d, expected %d!",
75                        cmsgptr->cmsg_level, SOL_SOCKET);
76                 return -1;
77         }
78
79         if(cmsgptr->cmsg_type != SCM_RIGHTS) {
80                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG type: %d, expected %d!",
81                        cmsgptr->cmsg_type, SCM_RIGHTS);
82                 return -1;
83         }
84
85         if(cmsgptr->cmsg_len != CMSG_LEN(sizeof(device_fd))) {
86                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG data length: %lu, expected %lu!",
87                        (unsigned long)cmsgptr->cmsg_len, (unsigned long)CMSG_LEN(sizeof(device_fd)));
88                 return -1;
89         }
90
91         return *(int *) CMSG_DATA(cmsgptr);
92 }
93
94 static int receive_fd(struct unix_socket_addr socket_addr) {
95         int socketfd;
96         int ret;
97         int result;
98
99         if((socketfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
100                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open stream socket (error %d)!", socketfd);
101                 return -1;
102         }
103
104         if((ret = connect(socketfd, (struct sockaddr *) &socket_addr.addr, socket_addr.size)) < 0) {
105                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect to Unix socket (error %d)!", ret);
106                 result = -1;
107                 goto end;
108         }
109
110         result = read_fd(socketfd);
111
112 end:
113         close(socketfd);
114         return result;
115 }
116
117 static struct unix_socket_addr parse_socket_addr(const char *path) {
118         struct sockaddr_un socket_addr = {
119                 .sun_family = AF_UNIX,
120         };
121         size_t path_length;
122
123         if(strlen(path) >= sizeof(socket_addr.sun_path)) {
124                 logger(DEBUG_ALWAYS, LOG_ERR, "Unix socket path too long!");
125                 return (struct unix_socket_addr) {
126                         0
127                 };
128         }
129
130         strncpy(socket_addr.sun_path, path, sizeof(socket_addr.sun_path));
131
132         if(path[0] == '@') {
133                 /* abstract namespace socket */
134                 socket_addr.sun_path[0] = '\0';
135                 path_length = strlen(path);
136         } else {
137                 /* filesystem path with NUL terminator */
138                 path_length = strlen(path) + 1;
139         }
140
141         return (struct unix_socket_addr) {
142                 .size = offsetof(struct sockaddr_un, sun_path) + path_length,
143                 .addr = socket_addr
144         };
145 }
146
147 static bool setup_device(void) {
148         if(routing_mode == RMODE_SWITCH) {
149                 logger(DEBUG_ALWAYS, LOG_ERR, "Switch mode not supported (requires unsupported TAP device)!");
150                 return false;
151         }
152
153         if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
154                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read device from configuration!");
155                 return false;
156         }
157
158         /* device is either directly a file descriptor or an unix socket to read it from */
159         if(sscanf(device, "%d", &device_fd) != 1) {
160                 logger(DEBUG_ALWAYS, LOG_INFO, "Receiving fd from Unix socket at %s.", device);
161                 device_fd = receive_fd(parse_socket_addr(device));
162         }
163
164         if(device_fd < 0) {
165                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s!", device, strerror(errno));
166                 return false;
167         }
168
169         logger(DEBUG_ALWAYS, LOG_INFO, "fd/%d adapter set up.", device_fd);
170
171         return true;
172 }
173
174 static void close_device(void) {
175         close(device_fd);
176         device_fd = -1;
177         free(iface);
178         iface = NULL;
179         free(device);
180         device = NULL;
181 }
182
183 static inline uint16_t get_ip_ethertype(vpn_packet_t *packet) {
184         switch(DATA(packet)[ETH_HLEN] >> 4) {
185         case 4:
186                 return ETH_P_IP;
187
188         case 6:
189                 return ETH_P_IPV6;
190
191         default:
192                 return ETH_P_MAX;
193         }
194 }
195
196 static inline void set_etherheader(vpn_packet_t *packet, uint16_t ethertype) {
197         memset(DATA(packet), 0, ETH_HLEN - ETHER_TYPE_LEN);
198
199         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN] = (ethertype >> 8) & 0xFF;
200         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN + 1] = ethertype & 0xFF;
201 }
202
203 static bool read_packet(vpn_packet_t *packet) {
204         ssize_t lenin = read(device_fd, DATA(packet) + ETH_HLEN, MTU - ETH_HLEN);
205
206         if(lenin <= 0) {
207                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from fd/%d: %s!", device_fd, strerror(errno));
208                 return false;
209         }
210
211         uint16_t ethertype = get_ip_ethertype(packet);
212
213         if(ethertype == ETH_P_MAX) {
214                 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version while reading packet from fd/%d!", device_fd);
215                 return false;
216         }
217
218         set_etherheader(packet, ethertype);
219         packet->len = lenin + ETH_HLEN;
220
221         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from fd/%d.", packet->len, device_fd);
222
223         return true;
224 }
225
226 static bool write_packet(vpn_packet_t *packet) {
227         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to fd/%d.", packet->len, device_fd);
228
229         if(write(device_fd, DATA(packet) + ETH_HLEN, packet->len - ETH_HLEN) < 0) {
230                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to fd/%d: %s!", device_fd, strerror(errno));
231                 return false;
232         }
233
234         return true;
235 }
236
237 const devops_t fd_devops = {
238         .setup = setup_device,
239         .close = close_device,
240         .read = read_packet,
241         .write = write_packet,
242 };
243 #endif