Require OpenSSL 1.1.0 or later.
[tinc] / src / solaris / device.c
1 /*
2     device.c -- Interaction with Solaris tun device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
5                   2001-2017 Guus Sliepen <guus@tinc-vpn.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22
23 #include "../system.h"
24
25 #include <sys/stropts.h>
26 #include <sys/sockio.h>
27 #include <stropts.h>
28
29 #include "../conf.h"
30 #include "../device.h"
31 #include "../logger.h"
32 #include "../net.h"
33 #include "../route.h"
34 #include "../utils.h"
35 #include "../xalloc.h"
36
37 #ifndef TUNNEWPPA
38 #warning Missing net/if_tun.h, using hardcoded value for TUNNEWPPA
39 #define TUNNEWPPA       (('T'<<16) | 0x0001)
40 #endif
41
42 #define DEFAULT_TUN_DEVICE "/dev/tun"
43 #define DEFAULT_TAP_DEVICE "/dev/tap"
44 #define IP_DEVICE "/dev/udp"
45
46 static enum {
47         DEVICE_TYPE_TUN,
48         DEVICE_TYPE_TAP,
49 } device_type = DEVICE_TYPE_TUN;
50
51 int device_fd = -1;
52 static int if_fd = -1;
53 static int ip_fd = -1;
54 char *device = NULL;
55 char *iface = NULL;
56 static const char *device_info = NULL;
57
58 uint64_t device_total_in = 0;
59 uint64_t device_total_out = 0;
60
61 static bool setup_device(void) {
62         char *type;
63
64         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
65                 if(routing_mode == RMODE_ROUTER) {
66                         device = xstrdup(DEFAULT_TUN_DEVICE);
67                 } else {
68                         device = xstrdup(DEFAULT_TAP_DEVICE);
69                 }
70         }
71
72         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
73                 if(!strcasecmp(type, "tun"))
74                         /* use default */;
75                 else if(!strcasecmp(type, "tap")) {
76                         device_type = DEVICE_TYPE_TAP;
77                 } else {
78                         logger(LOG_ERR, "Unknown device type %s!", type);
79                         return false;
80                 }
81         } else {
82                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER) {
83                         device_type = DEVICE_TYPE_TAP;
84                 }
85         }
86
87         if(device_type == DEVICE_TYPE_TUN) {
88                 device_info = "Solaris tun device";
89         } else {
90                 device_info = "Solaris tap device";
91         }
92
93         if(device_type == DEVICE_TYPE_TAP && routing_mode == RMODE_ROUTER) {
94                 overwrite_mac = true;
95         }
96
97         /* The following is black magic copied from OpenVPN. */
98
99         if((ip_fd = open(IP_DEVICE, O_RDWR, 0)) < 0) {
100                 logger(LOG_ERR, "Could not open %s: %s\n", IP_DEVICE, strerror(errno));
101                 return false;
102         }
103
104         if((device_fd = open(device, O_RDWR, 0)) < 0) {
105                 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
106                 return false;
107         }
108
109         /* Get unit number. */
110
111         char *ptr = device;
112         get_config_string(lookup_config(config_tree, "Interface"), &ptr);
113
114         while(*ptr && !isdigit(*ptr)) {
115                 ptr++;
116         }
117
118         int ppa = atoi(ptr);
119
120         /* Assign a new PPA and get its unit number. */
121
122         struct strioctl strioc_ppa = {
123                 .ic_cmd = TUNNEWPPA,
124                 .ic_len = sizeof(ppa),
125                 .ic_dp = (char *) &ppa,
126         };
127
128         if(!*ptr) { /* no number given, try dynamic */
129                 bool found = false;
130
131                 while(!found && ppa < 64) {
132                         int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
133
134                         if(new_ppa >= 0) {
135                                 ppa = new_ppa;
136                                 found = true;
137                                 break;
138                         }
139
140                         ppa++;
141                 }
142
143                 if(!found) {
144                         logger(LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
145                         return false;
146                 }
147         } else { /* try this particular one */
148                 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
149                         logger(LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
150                         return false;
151                 }
152         }
153
154         if((if_fd = open(device, O_RDWR, 0)) < 0) {
155                 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
156                 return false;
157         }
158
159         if(ioctl(if_fd, I_PUSH, "ip") < 0) {
160                 logger(LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
161                 return false;
162         }
163
164         xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
165
166         {
167                 /* Remove muxes just in case they are left over from a crashed tincd */
168                 struct lifreq ifr = {};
169                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
170
171                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
172                         int muxid = ifr.lifr_arp_muxid;
173                         ioctl(ip_fd, I_PUNLINK, muxid);
174                         muxid = ifr.lifr_ip_muxid;
175                         ioctl(ip_fd, I_PUNLINK, muxid);
176                 }
177         }
178
179         if(device_type == DEVICE_TYPE_TUN) {
180                 /* Assign ppa according to the unit number returned by tun device */
181                 if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
182                         logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
183                         return false;
184                 }
185         }
186
187         int arp_fd = -1;
188
189         if(device_type == DEVICE_TYPE_TAP) {
190                 struct lifreq ifr = {};
191
192                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
193                         logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
194                         return false;
195                 }
196
197                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
198                 ifr.lifr_ppa = ppa;
199
200                 /* Assign ppa according to the unit number returned by tun device */
201                 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
202                         logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
203                         return false;
204                 }
205
206                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
207                         logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
208                         return false;
209                 }
210
211                 /* Push arp module to if_fd */
212                 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
213                         logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
214                         return false;
215                 }
216
217                 /* Pop any modules on the stream */
218                 while(true) {
219                         if(ioctl(ip_fd, I_POP, NULL) < 0) {
220                                 break;
221                         }
222                 }
223
224                 /* Push arp module to ip_fd */
225                 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
226                         logger(LOG_ERR, "Could not push ARP module onto %s!", IP_DEVICE);
227                         return false;
228                 }
229
230                 /* Open arp_fd */
231                 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
232                         logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
233                         return false;
234                 }
235
236                 /* Push arp module to arp_fd */
237                 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
238                         logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
239                         return false;
240                 }
241
242                 /* Set ifname to arp */
243                 struct strioctl strioc_if = {
244                         .ic_cmd = SIOCSLIFNAME,
245                         .ic_len = sizeof(ifr),
246                         .ic_dp = (char *) &ifr,
247                 };
248
249                 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
250                         logger(LOG_ERR, "Could not set ifname to %s %s", device_info, device);
251                         return false;
252                 }
253         }
254
255         int ip_muxid, arp_muxid;
256
257         if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
258                 logger(LOG_ERR, "Could not link %s %s to IP", device_info, device);
259                 return false;
260         }
261
262         if(device_type == DEVICE_TYPE_TAP) {
263                 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
264                         logger(LOG_ERR, "Could not link %s %s to ARP", device_info, device);
265                         return false;
266                 }
267
268                 close(arp_fd);
269         }
270
271         struct lifreq ifr = {};
272
273         strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
274
275         ifr.lifr_ip_muxid = ip_muxid;
276
277         if(device_type == DEVICE_TYPE_TAP) {
278                 ifr.lifr_arp_muxid = arp_muxid;
279         }
280
281         if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
282                 if(device_type == DEVICE_TYPE_TAP) {
283                         ioctl(ip_fd, I_PUNLINK, arp_muxid);
284                 }
285
286                 ioctl(ip_fd, I_PUNLINK, ip_muxid);
287                 logger(LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
288                 return false;
289         }
290
291         close(if_fd);
292
293 #ifdef FD_CLOEXEC
294         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
295         fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
296 #endif
297
298         logger(LOG_INFO, "%s is a %s", device, device_info);
299
300         return true;
301 }
302
303 static void close_device(void) {
304         if(iface) {
305                 struct lifreq ifr = {};
306                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
307
308                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
309                         int muxid = ifr.lifr_arp_muxid;
310                         ioctl(ip_fd, I_PUNLINK, muxid);
311                         muxid = ifr.lifr_ip_muxid;
312                         ioctl(ip_fd, I_PUNLINK, muxid);
313                 }
314         }
315
316         close(ip_fd);
317         close(device_fd);
318
319         free(device);
320         free(iface);
321 }
322
323 static bool read_packet(vpn_packet_t *packet) {
324         int result;
325         struct strbuf sbuf;
326         int f = 0;
327
328         switch(device_type) {
329         case DEVICE_TYPE_TUN:
330                 sbuf.maxlen = MTU - 14;
331                 sbuf.buf = (char *)packet->data + 14;
332
333                 if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
334                         logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
335                         return false;
336                 }
337
338                 switch(packet->data[14] >> 4) {
339                 case 4:
340                         packet->data[12] = 0x08;
341                         packet->data[13] = 0x00;
342                         break;
343
344                 case 6:
345                         packet->data[12] = 0x86;
346                         packet->data[13] = 0xDD;
347                         break;
348
349                 default:
350                         ifdebug(TRAFFIC) logger(LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
351                         return false;
352                 }
353
354                 memset(packet->data, 0, 12);
355                 packet->len = sbuf.len + 14;
356                 break;
357
358         case DEVICE_TYPE_TAP:
359                 sbuf.maxlen = MTU;
360                 sbuf.buf = (char *)packet->data;
361
362                 if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
363                         logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
364                         return false;
365                 }
366
367                 packet->len = sbuf.len;
368                 break;
369
370         default:
371                 abort();
372         }
373
374         device_total_in += packet->len;
375
376         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
377
378         return true;
379 }
380
381 static bool write_packet(vpn_packet_t *packet) {
382         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
383
384         struct strbuf sbuf;
385
386         switch(device_type) {
387         case DEVICE_TYPE_TUN:
388                 sbuf.len = packet->len - 14;
389                 sbuf.buf = (char *)packet->data + 14;
390
391                 if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
392                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
393                         return false;
394                 }
395
396                 break;
397
398         case DEVICE_TYPE_TAP:
399                 sbuf.len = packet->len;
400                 sbuf.buf = (char *)packet->data;
401
402                 if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
403                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
404                         return false;
405                 }
406
407                 break;
408
409         default:
410                 abort();
411         }
412
413         device_total_out += packet->len;
414
415         return true;
416 }
417
418 static void dump_device_stats(void) {
419         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
420         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
421         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
422 }
423
424 const devops_t os_devops = {
425         .setup = setup_device,
426         .close = close_device,
427         .read = read_packet,
428         .write = write_packet,
429         .dump_stats = dump_device_stats,
430 };