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