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>
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.
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.
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.
23 #include "../system.h"
25 #include <sys/stropts.h>
26 #include <sys/sockio.h>
29 #include "../device.h"
30 #include "../logger.h"
34 #include "../xalloc.h"
36 #define DEFAULT_TUN_DEVICE "/dev/tun"
37 #define DEFAULT_TAP_DEVICE "/dev/tap"
42 } device_type = DEVICE_TYPE_TUN;
45 static int if_fd = -1;
46 static int ip_fd = -1;
47 static int arp_fd = -1;
50 static char *device_info = NULL;
52 uint64_t device_total_in = 0;
53 uint64_t device_total_out = 0;
55 static bool setup_device(void) {
58 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
59 if(routing_mode == RMODE_ROUTER)
60 device = xstrdup(DEFAULT_TUN_DEVICE);
62 device = xstrdup(DEFAULT_TAP_DEVICE);
65 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
66 if(!strcasecmp(type, "tun"))
68 else if(!strcasecmp(type, "tap"))
69 device_type = DEVICE_TYPE_TAP;
71 logger(LOG_ERR, "Unknown device type %s!", type);
75 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
76 device_type = DEVICE_TYPE_TAP;
79 if(device_type == DEVICE_TYPE_TUN)
80 device_info = "Solaris tun device";
82 device_info = "Solaris tap device";
84 /* The following is black magic copied from OpenVPN. */
86 if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
87 logger(LOG_ERR, "Could not open %s: %s\n", "/dev/ip", strerror(errno));
91 if((device_fd = open(device, O_RDWR, 0)) < 0) {
92 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
96 /* Get unit number. */
99 get_config_string(lookup_config(config_tree, "Interface"), &ptr);
101 while(*ptr && !isdigit(*ptr))
105 /* Assign a new PPA and get its unit number. */
107 struct strioctl strioc_ppa = {
109 .ic_len = sizeof ppa,
110 .ic_dp = (char *)&ppa,
113 if(!*ptr) { /* no number given, try dynamic */
115 while(!found && ppa < 64) {
116 int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
125 logger(LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
128 } else { /* try this particular one */
129 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
130 logger(LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
135 if((if_fd = open(device, O_RDWR, 0)) < 0) {
136 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
140 if(ioctl(if_fd, I_PUSH, "ip") < 0) {
141 logger(LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
145 xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
148 /* Remove muxes just in case they are left over from a crashed tincd */
149 struct lifreq ifr = {};
150 strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
151 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
152 int muxid = ifr.lifr_arp_muxid;
153 ioctl(ip_fd, I_PUNLINK, muxid);
154 muxid = ifr.lifr_ip_muxid;
155 ioctl(ip_fd, I_PUNLINK, muxid);
159 if(device_type == DEVICE_TYPE_TUN) {
160 /* Assign ppa according to the unit number returned by tun device */
161 if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
162 logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
169 if(device_type == DEVICE_TYPE_TAP) {
170 struct lifreq ifr = {};
172 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
173 logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
177 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
180 /* Assign ppa according to the unit number returned by tun device */
181 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
182 logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
185 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
186 logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
190 /* Push arp module to if_fd */
191 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
192 logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
196 /* Pop any modules on the stream */
198 if(ioctl(ip_fd, I_POP, NULL) < 0)
202 /* Push arp module to ip_fd */
203 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
204 logger(LOG_ERR, "Could not push ARP module onto %s!", "/dev/ip");
209 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
210 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
214 /* Push arp module to arp_fd */
215 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
216 logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
220 /* Set ifname to arp */
221 struct strioctl strioc_if = {
222 .ic_cmd = SIOCSLIFNAME,
223 .ic_len = sizeof ifr,
224 .ic_dp = (char *)&ifr,
227 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
228 logger(LOG_ERR, "Could not set ifname to %s %s", device_info, device);
233 int ip_muxid, arp_muxid;
235 if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
236 logger(LOG_ERR, "Could not link %s %s to IP", device_info, device);
240 if(device_type == DEVICE_TYPE_TAP) {
241 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
242 logger(LOG_ERR, "Could not link %s %s to ARP", device_info, device);
248 struct lifreq ifr = {};
249 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
250 ifr.lifr_ip_muxid = ip_muxid;
251 if(device_type == DEVICE_TYPE_TAP) {
252 ifr.lifr_arp_muxid = arp_muxid;
255 if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
256 if(device_type == DEVICE_TYPE_TAP) {
257 ioctl(ip_fd, I_PUNLINK, arp_muxid);
259 ioctl(ip_fd, I_PUNLINK, ip_muxid);
260 logger(LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
267 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
268 fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
271 logger(LOG_INFO, "%s is a %s", device, device_info);
276 static void close_device(void) {
278 struct lifreq ifr = {};
279 strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
280 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
281 int muxid = ifr.lifr_arp_muxid;
282 ioctl(ip_fd, I_PUNLINK, muxid);
283 muxid = ifr.lifr_ip_muxid;
284 ioctl(ip_fd, I_PUNLINK, muxid);
295 static bool read_packet(vpn_packet_t *packet) {
298 switch(device_type) {
299 case DEVICE_TYPE_TUN:
300 if((inlen = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
301 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
305 switch(packet->data[14] >> 4) {
307 packet->data[12] = 0x08;
308 packet->data[13] = 0x00;
311 packet->data[12] = 0x86;
312 packet->data[13] = 0xDD;
315 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
319 memset(packet->data, 0, 12);
320 packet->len = inlen + 14;
323 case DEVICE_TYPE_TAP:
324 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
325 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
329 packet->len = inlen + 14;
336 device_total_in += packet->len;
338 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
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);
346 switch(device_type) {
347 case DEVICE_TYPE_TUN:
348 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
349 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
354 case DEVICE_TYPE_TAP:
355 if(write(device_fd, packet->data, packet->len) < 0) {
356 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
365 device_total_out += packet->len;
370 static void dump_device_stats(void) {
371 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
372 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
373 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
376 const devops_t os_devops = {
377 .setup = setup_device,
378 .close = close_device,
380 .write = write_packet,
381 .dump_stats = dump_device_stats,