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