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