]> git.meshlink.io Git - meshlink/blob - src/solaris/device.c
4940ddb593b87810885c58b0368508013970b9f1
[meshlink] / 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 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("/dev/ip", O_RDWR, 0)) < 0) {
89                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", "/dev/ip", 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         if((if_fd = open(device, O_RDWR, 0)) < 0) {
138                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
139                 return false;
140         }
141
142         if(ioctl(if_fd, I_PUSH, "ip") < 0) {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
144                 return false;
145         }
146
147         xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
148
149         {
150                 /* Remove muxes just in case they are left over from a crashed tincd */
151                 struct lifreq ifr = {};
152                 strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
153                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
154                         int muxid = ifr.lifr_arp_muxid;
155                         ioctl(ip_fd, I_PUNLINK, muxid);
156                         muxid = ifr.lifr_ip_muxid;
157                         ioctl(ip_fd, I_PUNLINK, muxid);
158                 }
159         }
160
161         if(device_type == DEVICE_TYPE_TUN) {
162                 /* Assign ppa according to the unit number returned by tun device */
163                 if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
164                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
165                         return false;
166                 }       
167         }
168
169         int arp_fd = -1;
170
171         if(device_type == DEVICE_TYPE_TAP) {
172                 struct lifreq ifr = {};
173
174                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
175                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not set flags on %s %s!", device_info, device);
176                         return false;
177                 }
178
179                 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
180                 ifr.lifr_ppa = ppa;
181
182                 /* Assign ppa according to the unit number returned by tun device */
183                 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
184                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
185                         return false;
186                 }
187                 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
188                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not set flags on %s %s!", device_info, device);
189                         return false;
190                 }
191
192                 /* Push arp module to if_fd */
193                 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
194                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
195                         return false;
196                 }
197
198                 /* Pop any modules on the stream */
199                 while(true) {
200                         if(ioctl(ip_fd, I_POP, NULL) < 0)
201                                 break;
202                 }
203
204                 /* Push arp module to ip_fd */
205                 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
206                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s!", "/dev/ip");
207                         return false;
208                 }
209
210                 /* Open arp_fd */
211                 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
212                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
213                         return false;
214                 }
215
216                 /* Push arp module to arp_fd */
217                 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
218                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
219                         return false;
220                 }
221
222                 /* Set ifname to arp */
223                 struct strioctl strioc_if = {
224                         .ic_cmd = SIOCSLIFNAME,
225                         .ic_len = sizeof ifr,
226                         .ic_dp = (char *)&ifr,
227                 };
228
229                 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
230                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not set ifname to %s %s", device_info, device);
231                         return false;
232                 }
233         }
234
235         int ip_muxid, arp_muxid;
236
237         if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
238                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not link %s %s to IP", device_info, device);
239                 return false;
240         }
241
242         if(device_type == DEVICE_TYPE_TAP) {
243                 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
244                         logger(DEBUG_ALWAYS, LOG_ERR, "Could not link %s %s to ARP", device_info, device);
245                         return false;
246                 }
247                 close(arp_fd);
248         }
249
250         struct lifreq ifr = {};
251         strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
252         ifr.lifr_ip_muxid = ip_muxid;
253         if(device_type == DEVICE_TYPE_TAP) {
254                 ifr.lifr_arp_muxid = arp_muxid;
255         }
256
257         if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
258                 if(device_type == DEVICE_TYPE_TAP) {
259                         ioctl(ip_fd, I_PUNLINK, arp_muxid);
260                 }
261                 ioctl(ip_fd, I_PUNLINK, ip_muxid);
262                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
263                 return false;
264         }
265
266         close(if_fd);
267
268 #ifdef FD_CLOEXEC
269         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
270         fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
271 #endif
272
273         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
274
275         return true;
276 }
277
278 static void close_device(void) {
279         if(iface) {
280                 struct lifreq ifr = {};
281                 strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
282                 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
283                         int muxid = ifr.lifr_arp_muxid;
284                         ioctl(ip_fd, I_PUNLINK, muxid);
285                         muxid = ifr.lifr_ip_muxid;
286                         ioctl(ip_fd, I_PUNLINK, muxid);
287                 }
288         }
289
290         close(ip_fd);
291         close(device_fd);
292
293         free(device);
294         free(iface);
295 }
296
297 static bool read_packet(vpn_packet_t *packet) {
298         int inlen;
299
300         switch(device_type) {
301                 case DEVICE_TYPE_TUN:
302                         if((inlen = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
303                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
304                                 return false;
305                         }
306
307                         switch(packet->data[14] >> 4) {
308                                 case 4:
309                                         packet->data[12] = 0x08;
310                                         packet->data[13] = 0x00;
311                                         break;
312                                 case 6:
313                                         packet->data[12] = 0x86;
314                                         packet->data[13] = 0xDD;
315                                         break;
316                                 default:
317                                         logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
318                                         return false;
319                         }
320
321                         memset(packet->data, 0, 12);
322                         packet->len = inlen + 14;
323                         break;
324
325                 case DEVICE_TYPE_TAP:
326                         if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
327                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
328                                 return false;
329                         }
330
331                         packet->len = inlen + 14;
332                         break;
333
334                 default:
335                         abort();
336         }
337
338         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
339
340         return true;
341 }
342
343 static bool write_packet(vpn_packet_t *packet) {
344         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
345
346         switch(device_type) {
347                 case DEVICE_TYPE_TUN:
348                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
349                                 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
350                                 return false;
351                         }
352                         break;
353
354                 case DEVICE_TYPE_TAP:
355                         if(write(device_fd, packet->data, packet->len) < 0) {
356                                 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
357                                 return false;
358                         }
359                         break;
360
361                 default:
362                         abort();
363         }
364
365         return true;
366 }
367
368 const devops_t os_devops = {
369         .setup = setup_device,
370         .close = close_device,
371         .read = read_packet,
372         .write = write_packet,
373 };