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