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