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