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"
35 #include "../xalloc.h"
38 #warning Missing net/if_tun.h, using hardcoded value for TUNNEWPPA
39 #define TUNNEWPPA (('T'<<16) | 0x0001)
42 #define DEFAULT_TUN_DEVICE "/dev/tun"
43 #define DEFAULT_TAP_DEVICE "/dev/tap"
48 } device_type = DEVICE_TYPE_TUN;
51 static int ip_fd = -1;
54 static char *device_info = NULL;
56 static bool setup_device(void) {
59 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
60 if(routing_mode == RMODE_ROUTER)
61 device = xstrdup(DEFAULT_TUN_DEVICE);
63 device = xstrdup(DEFAULT_TAP_DEVICE);
66 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
67 if(!strcasecmp(type, "tun"))
69 else if(!strcasecmp(type, "tap"))
70 device_type = DEVICE_TYPE_TAP;
72 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
76 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
77 device_type = DEVICE_TYPE_TAP;
80 if(device_type == DEVICE_TYPE_TUN)
81 device_info = "Solaris tun device";
83 device_info = "Solaris tap device";
85 /* The following is black magic copied from OpenVPN. */
87 if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
88 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", "/dev/ip", strerror(errno));
92 if((device_fd = open(device, O_RDWR, 0)) < 0) {
93 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
97 /* Get unit number. */
100 get_config_string(lookup_config(config_tree, "Interface"), &ptr);
102 while(*ptr && !isdigit(*ptr))
106 /* Assign a new PPA and get its unit number. */
108 struct strioctl strioc_ppa = {
110 .ic_len = sizeof ppa,
111 .ic_dp = (char *)&ppa,
114 if(!*ptr) { /* no number given, try dynamic */
116 while(!found && ppa < 64) {
117 int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
126 logger(DEBUG_ALWAYS, LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
129 } else { /* try this particular one */
130 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
131 logger(DEBUG_ALWAYS, LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
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));
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);
147 xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
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);
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);
171 if(device_type == DEVICE_TYPE_TAP) {
172 struct lifreq ifr = {};
174 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
175 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set flags on %s %s!", device_info, device);
179 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
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);
187 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
188 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set flags on %s %s!", device_info, device);
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);
198 /* Pop any modules on the stream */
200 if(ioctl(ip_fd, I_POP, NULL) < 0)
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");
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));
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);
222 /* Set ifname to arp */
223 struct strioctl strioc_if = {
224 .ic_cmd = SIOCSLIFNAME,
225 .ic_len = sizeof ifr,
226 .ic_dp = (char *)&ifr,
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);
235 int ip_muxid, arp_muxid;
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);
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);
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;
257 if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
258 if(device_type == DEVICE_TYPE_TAP) {
259 ioctl(ip_fd, I_PUNLINK, arp_muxid);
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);
269 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
270 fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
273 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
278 static void close_device(void) {
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);
290 close(ip_fd); ip_fd = -1;
291 close(device_fd); device_fd = -1;
293 free(device); device = NULL;
294 free(iface); iface = NULL;
297 static bool read_packet(vpn_packet_t *packet) {
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));
307 switch(packet->data[14] >> 4) {
309 packet->data[12] = 0x08;
310 packet->data[13] = 0x00;
313 packet->data[12] = 0x86;
314 packet->data[13] = 0xDD;
317 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
321 memset(packet->data, 0, 12);
322 packet->len = inlen + 14;
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));
331 packet->len = inlen + 14;
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(DEBUG_ALWAYS, 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(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
368 const devops_t os_devops = {
369 .setup = setup_device,
370 .close = close_device,
372 .write = write_packet,