Convert sizeof foo to sizeof(foo).
[oweals/tinc.git] / 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 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         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
72                 if(!strcasecmp(type, "tun"))
73                         /* use default */;
74                 else if(!strcasecmp(type, "tap"))
75                         device_type = DEVICE_TYPE_TAP;
76                 else {
77                         logger(LOG_ERR, "Unknown device type %s!", type);
78                         return false;
79                 }
80         } else {
81                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
82                         device_type = DEVICE_TYPE_TAP;
83         }
84
85         if(device_type == DEVICE_TYPE_TUN)
86                 device_info = "Solaris tun device";
87         else
88                 device_info = "Solaris tap device";
89
90         if(device_type == DEVICE_TYPE_TAP && routing_mode == RMODE_ROUTER)
91                 overwrite_mac = true;
92
93         /* The following is black magic copied from OpenVPN. */
94
95         if((ip_fd = open(IP_DEVICE, O_RDWR, 0)) < 0) {
96                 logger(LOG_ERR, "Could not open %s: %s\n", IP_DEVICE, strerror(errno));
97                 return false;
98         }
99
100         if((device_fd = open(device, O_RDWR, 0)) < 0) {
101                 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
102                 return false;
103         }
104
105         /* Get unit number. */
106
107         char *ptr = device;
108         get_config_string(lookup_config(config_tree, "Interface"), &ptr);
109
110         while(*ptr && !isdigit(*ptr))
111                 ptr++;
112         int ppa = atoi(ptr);
113
114         /* Assign a new PPA and get its unit number. */
115
116         struct strioctl strioc_ppa = {
117                 .ic_cmd = TUNNEWPPA,
118                 .ic_len = sizeof(ppa),
119                 .ic_dp = (char *)&ppa,
120         };
121
122         if(!*ptr) { /* no number given, try dynamic */
123                 bool found = false;
124                 while(!found && ppa < 64) {
125                         int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
126                         if(new_ppa >= 0) {
127                                 ppa = new_ppa;
128                                 found = true;
129                                 break;
130                         }
131                         ppa++;
132                 }
133                 if(!found) {
134                         logger(LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
135                         return false;
136                 }
137         } else { /* try this particular one */
138                 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
139                         logger(LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
140                         return false;
141                 }
142         }
143
144         if((if_fd = open(device, O_RDWR, 0)) < 0) {
145                 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
146                 return false;
147         }
148
149         if(ioctl(if_fd, I_PUSH, "ip") < 0) {
150                 logger(LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
151                 return false;
152         }
153
154         xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
155
156         {
157                 /* Remove muxes just in case they are left over from a crashed tincd */
158                 struct lifreq ifr = {};
159                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
160                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
161                         int muxid = ifr.lifr_arp_muxid;
162                         ioctl(ip_fd, I_PUNLINK, muxid);
163                         muxid = ifr.lifr_ip_muxid;
164                         ioctl(ip_fd, I_PUNLINK, muxid);
165                 }
166         }
167
168         if(device_type == DEVICE_TYPE_TUN) {
169                 /* Assign ppa according to the unit number returned by tun device */
170                 if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
171                         logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
172                         return false;
173                 }
174         }
175
176         int arp_fd = -1;
177
178         if(device_type == DEVICE_TYPE_TAP) {
179                 struct lifreq ifr = {};
180
181                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
182                         logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
183                         return false;
184                 }
185
186                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
187                 ifr.lifr_ppa = ppa;
188
189                 /* Assign ppa according to the unit number returned by tun device */
190                 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
191                         logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
192                         return false;
193                 }
194                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
195                         logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
196                         return false;
197                 }
198
199                 /* Push arp module to if_fd */
200                 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
201                         logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
202                         return false;
203                 }
204
205                 /* Pop any modules on the stream */
206                 while(true) {
207                         if(ioctl(ip_fd, I_POP, NULL) < 0)
208                                 break;
209                 }
210
211                 /* Push arp module to ip_fd */
212                 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
213                         logger(LOG_ERR, "Could not push ARP module onto %s!", IP_DEVICE);
214                         return false;
215                 }
216
217                 /* Open arp_fd */
218                 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
219                         logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
220                         return false;
221                 }
222
223                 /* Push arp module to arp_fd */
224                 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
225                         logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
226                         return false;
227                 }
228
229                 /* Set ifname to arp */
230                 struct strioctl strioc_if = {
231                         .ic_cmd = SIOCSLIFNAME,
232                         .ic_len = sizeof(ifr),
233                         .ic_dp = (char *)&ifr,
234                 };
235
236                 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
237                         logger(LOG_ERR, "Could not set ifname to %s %s", device_info, device);
238                         return false;
239                 }
240         }
241
242         int ip_muxid, arp_muxid;
243
244         if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
245                 logger(LOG_ERR, "Could not link %s %s to IP", device_info, device);
246                 return false;
247         }
248
249         if(device_type == DEVICE_TYPE_TAP) {
250                 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
251                         logger(LOG_ERR, "Could not link %s %s to ARP", device_info, device);
252                         return false;
253                 }
254                 close(arp_fd);
255         }
256
257         struct lifreq ifr = {};
258         strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
259         ifr.lifr_ip_muxid = ip_muxid;
260         if(device_type == DEVICE_TYPE_TAP) {
261                 ifr.lifr_arp_muxid = arp_muxid;
262         }
263
264         if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
265                 if(device_type == DEVICE_TYPE_TAP) {
266                         ioctl(ip_fd, I_PUNLINK, arp_muxid);
267                 }
268                 ioctl(ip_fd, I_PUNLINK, ip_muxid);
269                 logger(LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
270                 return false;
271         }
272
273         close(if_fd);
274
275 #ifdef FD_CLOEXEC
276         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
277         fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
278 #endif
279
280         logger(LOG_INFO, "%s is a %s", device, device_info);
281
282         return true;
283 }
284
285 static void close_device(void) {
286         if(iface) {
287                 struct lifreq ifr = {};
288                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
289                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
290                         int muxid = ifr.lifr_arp_muxid;
291                         ioctl(ip_fd, I_PUNLINK, muxid);
292                         muxid = ifr.lifr_ip_muxid;
293                         ioctl(ip_fd, I_PUNLINK, muxid);
294                 }
295         }
296
297         close(ip_fd);
298         close(device_fd);
299
300         free(device);
301         free(iface);
302 }
303
304 static bool read_packet(vpn_packet_t *packet) {
305         int result;
306         struct strbuf sbuf;
307         int f = 0;
308
309         switch(device_type) {
310                 case DEVICE_TYPE_TUN:
311                         sbuf.maxlen = MTU - 14;
312                         sbuf.buf = (char *)packet->data + 14;
313
314                         if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
315                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
316                                 return false;
317                         }
318
319                         switch(packet->data[14] >> 4) {
320                                 case 4:
321                                         packet->data[12] = 0x08;
322                                         packet->data[13] = 0x00;
323                                         break;
324                                 case 6:
325                                         packet->data[12] = 0x86;
326                                         packet->data[13] = 0xDD;
327                                         break;
328                                 default:
329                                         ifdebug(TRAFFIC) logger(LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
330                                         return false;
331                         }
332
333                         memset(packet->data, 0, 12);
334                         packet->len = sbuf.len + 14;
335                         break;
336
337                 case DEVICE_TYPE_TAP:
338                         sbuf.maxlen = MTU;
339                         sbuf.buf = (char *)packet->data;
340
341                         if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
342                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
343                                 return false;
344                         }
345
346                         packet->len = sbuf.len;
347                         break;
348
349                 default:
350                         abort();
351         }
352
353         device_total_in += packet->len;
354
355         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
356
357         return true;
358 }
359
360 static bool write_packet(vpn_packet_t *packet) {
361         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
362
363         struct strbuf sbuf;
364
365         switch(device_type) {
366                 case DEVICE_TYPE_TUN:
367                         sbuf.len = packet->len - 14;
368                         sbuf.buf = (char *)packet->data + 14;
369
370                         if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
371                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
372                                 return false;
373                         }
374                         break;
375
376                 case DEVICE_TYPE_TAP:
377                         sbuf.len = packet->len;
378                         sbuf.buf = (char *)packet->data;
379
380                         if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
381                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
382                                 return false;
383                         }
384                         break;
385
386                 default:
387                         abort();
388         }
389
390         device_total_out += packet->len;
391
392         return true;
393 }
394
395 static void dump_device_stats(void) {
396         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
397         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
398         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
399 }
400
401 const devops_t os_devops = {
402         .setup = setup_device,
403         .close = close_device,
404         .read = read_packet,
405         .write = write_packet,
406         .dump_stats = dump_device_stats,
407 };