fd_device: allow fd to be passed through a unix socket
[tinc] / src / fd_device.c
1 /*
2     fd_device.c -- Interaction with Android tun fd
3     Copyright (C)   2001-2005   Ivo Timmermans,
4                     2001-2016   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 <sys/un.h>
24
25 #include "system.h"
26 #include "conf.h"
27 #include "device.h"
28 #include "ethernet.h"
29 #include "logger.h"
30 #include "net.h"
31 #include "route.h"
32 #include "utils.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         int 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 %d)!", ret);
56                 return -1;
57         }
58         if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) {
59                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while receiving message (flags %d)!", msg.msg_flags);
60                 return -1;
61         }
62
63         cmsgptr = CMSG_FIRSTHDR(&msg);
64         if(cmsgptr->cmsg_level != SOL_SOCKET) {
65                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG level: %d, expected %d!",
66                         cmsgptr->cmsg_level, SOL_SOCKET);
67                 return -1;
68         }
69         if(cmsgptr->cmsg_type != SCM_RIGHTS) {
70                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG type: %d, expected %d!",
71                         cmsgptr->cmsg_type, SCM_RIGHTS);
72                 return -1;
73         }
74         if(cmsgptr->cmsg_len != CMSG_LEN(sizeof(device_fd))) {
75                 logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG data length: %lu, expected %lu!",
76                         cmsgptr->cmsg_len, CMSG_LEN(sizeof(device_fd)));
77                 return -1;
78         }
79
80         return *(int *) CMSG_DATA(cmsgptr);
81 }
82
83 static int receive_fd(struct unix_socket_addr socket_addr) {
84         int socketfd;
85         int ret;
86         int result;
87
88         if((socketfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
89                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open stream socket (error %d)!", socketfd);
90                 return -1;
91         }
92
93         if((ret = connect(socketfd, (struct sockaddr *) &socket_addr.addr, socket_addr.size)) < 0) {
94                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect to Unix socket (error %d)!", ret);
95                 result = -1;
96                 goto end;
97         }
98
99         result = read_fd(socketfd);
100
101 end:
102         close(socketfd);
103         return result;
104 }
105
106 static struct unix_socket_addr parse_socket_addr(const char *path) {
107         struct sockaddr_un socket_addr;
108         size_t path_length;
109
110         if(strlen(path) >= sizeof(socket_addr.sun_path)) {
111                 logger(DEBUG_ALWAYS, LOG_ERR, "Unix socket path too long!");
112                 return (struct unix_socket_addr) {0};
113         }
114
115         socket_addr.sun_family = AF_UNIX;
116         strncpy(socket_addr.sun_path, path, sizeof(socket_addr.sun_path));
117
118         if(path[0] == '@') {
119                 /* abstract namespace socket */
120                 socket_addr.sun_path[0] = '\0';
121                 path_length = strlen(path);
122         } else {
123                 /* filesystem path with NUL terminator */
124                 path_length = strlen(path) + 1;
125         }
126
127         return (struct unix_socket_addr) {
128                 .size = offsetof(struct sockaddr_un, sun_path) + path_length,
129                 .addr = socket_addr
130         };
131 }
132
133 static bool setup_device(void) {
134         if(routing_mode == RMODE_SWITCH) {
135                 logger(DEBUG_ALWAYS, LOG_ERR, "Switch mode not supported (requires unsupported TAP device)!");
136                 return false;
137         }
138
139         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
140                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read device from configuration!");
141                 return false;
142         }
143
144         /* device is either directly a file descriptor or an unix socket to read it from */
145         if(sscanf(device, "%d", &device_fd) != 1) {
146                 logger(DEBUG_ALWAYS, LOG_INFO, "Receiving fd from Unix socket at %s.", device);
147                 device_fd = receive_fd(parse_socket_addr(device));
148         }
149
150         if(device_fd < 0) {
151                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s!", device, strerror(errno));
152                 return false;
153         }
154
155         logger(DEBUG_ALWAYS, LOG_INFO, "fd/%d adapter set up.", device_fd);
156
157         return true;
158 }
159
160 static void close_device(void) {
161         close(device_fd);
162         device_fd = -1;
163 }
164
165 static inline uint16_t get_ip_ethertype(vpn_packet_t *packet) {
166         switch(DATA(packet)[ETH_HLEN] >> 4) {
167         case 4:
168                 return ETH_P_IP;
169
170         case 6:
171                 return ETH_P_IPV6;
172
173         default:
174                 return ETH_P_MAX;
175         }
176 }
177
178 static inline void set_etherheader(vpn_packet_t *packet, uint16_t ethertype) {
179         memset(DATA(packet), 0, ETH_HLEN - ETHER_TYPE_LEN);
180
181         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN] = (ethertype >> 8) & 0xFF;
182         DATA(packet)[ETH_HLEN - ETHER_TYPE_LEN + 1] = ethertype & 0xFF;
183 }
184
185 static bool read_packet(vpn_packet_t *packet) {
186         int lenin = read(device_fd, DATA(packet) + ETH_HLEN, MTU - ETH_HLEN);
187
188         if(lenin <= 0) {
189                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from fd/%d: %s!", device_fd, strerror(errno));
190                 return false;
191         }
192
193         uint16_t ethertype = get_ip_ethertype(packet);
194
195         if(ethertype == ETH_P_MAX) {
196                 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version while reading packet from fd/%d!", device_fd);
197                 return false;
198         }
199
200         set_etherheader(packet, ethertype);
201         packet->len = lenin + ETH_HLEN;
202
203         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from fd/%d.", packet->len, device_fd);
204
205         return true;
206 }
207
208 static bool write_packet(vpn_packet_t *packet) {
209         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to fd/%d.", packet->len, device_fd);
210
211         if(write(device_fd, DATA(packet) + ETH_HLEN, packet->len - ETH_HLEN) < 0) {
212                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to fd/%d: %s!", device_fd, strerror(errno));
213                 return false;
214         }
215
216         return true;
217 }
218
219 const devops_t fd_devops = {
220         .setup = setup_device,
221         .close = close_device,
222         .read = read_packet,
223         .write = write_packet,
224 };