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>
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"
37 #warning Missing net/if_tun.h, using hardcoded value for TUNNEWPPA
38 #define TUNNEWPPA (('T'<<16) | 0x0001)
41 #define DEFAULT_TUN_DEVICE "/dev/tun"
42 #define DEFAULT_TAP_DEVICE "/dev/tap"
47 } device_type = DEVICE_TYPE_TUN;
50 static int if_fd = -1;
51 static int ip_fd = -1;
54 static char *device_info = NULL;
56 uint64_t device_total_in = 0;
57 uint64_t device_total_out = 0;
59 static bool setup_device(void) {
62 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
63 if(routing_mode == RMODE_ROUTER)
64 device = xstrdup(DEFAULT_TUN_DEVICE);
66 device = xstrdup(DEFAULT_TAP_DEVICE);
69 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
70 if(!strcasecmp(type, "tun"))
72 else if(!strcasecmp(type, "tap"))
73 device_type = DEVICE_TYPE_TAP;
75 logger(LOG_ERR, "Unknown device type %s!", type);
79 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
80 device_type = DEVICE_TYPE_TAP;
83 if(device_type == DEVICE_TYPE_TUN)
84 device_info = "Solaris tun device";
86 device_info = "Solaris tap device";
88 /* The following is black magic copied from OpenVPN. */
90 if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
91 logger(LOG_ERR, "Could not open %s: %s\n", "/dev/ip", strerror(errno));
95 if((device_fd = open(device, O_RDWR, 0)) < 0) {
96 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
100 /* Get unit number. */
103 get_config_string(lookup_config(config_tree, "Interface"), &ptr);
105 while(*ptr && !isdigit(*ptr))
109 /* Assign a new PPA and get its unit number. */
111 struct strioctl strioc_ppa = {
113 .ic_len = sizeof ppa,
114 .ic_dp = (char *)&ppa,
117 if(!*ptr) { /* no number given, try dynamic */
119 while(!found && ppa < 64) {
120 int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
129 logger(LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
132 } else { /* try this particular one */
133 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
134 logger(LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
139 if((if_fd = open(device, O_RDWR, 0)) < 0) {
140 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
144 if(ioctl(if_fd, I_PUSH, "ip") < 0) {
145 logger(LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
149 xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
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);
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(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
173 if(device_type == DEVICE_TYPE_TAP) {
174 struct lifreq ifr = {};
176 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
177 logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
181 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
184 /* Assign ppa according to the unit number returned by tun device */
185 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
186 logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
189 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
190 logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
194 /* Push arp module to if_fd */
195 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
196 logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
200 /* Pop any modules on the stream */
202 if(ioctl(ip_fd, I_POP, NULL) < 0)
206 /* Push arp module to ip_fd */
207 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
208 logger(LOG_ERR, "Could not push ARP module onto %s!", "/dev/ip");
213 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
214 logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
218 /* Push arp module to arp_fd */
219 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
220 logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
224 /* Set ifname to arp */
225 struct strioctl strioc_if = {
226 .ic_cmd = SIOCSLIFNAME,
227 .ic_len = sizeof ifr,
228 .ic_dp = (char *)&ifr,
231 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
232 logger(LOG_ERR, "Could not set ifname to %s %s", device_info, device);
237 int ip_muxid, arp_muxid;
239 if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
240 logger(LOG_ERR, "Could not link %s %s to IP", device_info, device);
244 if(device_type == DEVICE_TYPE_TAP) {
245 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
246 logger(LOG_ERR, "Could not link %s %s to ARP", device_info, device);
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;
259 if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
260 if(device_type == DEVICE_TYPE_TAP) {
261 ioctl(ip_fd, I_PUNLINK, arp_muxid);
263 ioctl(ip_fd, I_PUNLINK, ip_muxid);
264 logger(LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
271 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
272 fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
275 logger(LOG_INFO, "%s is a %s", device, device_info);
280 static void close_device(void) {
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);
299 static bool read_packet(vpn_packet_t *packet) {
302 switch(device_type) {
303 case DEVICE_TYPE_TUN:
304 if((inlen = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
305 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
309 switch(packet->data[14] >> 4) {
311 packet->data[12] = 0x08;
312 packet->data[13] = 0x00;
315 packet->data[12] = 0x86;
316 packet->data[13] = 0xDD;
319 ifdebug(TRAFFIC) logger(LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
323 memset(packet->data, 0, 12);
324 packet->len = inlen + 14;
327 case DEVICE_TYPE_TAP:
328 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
329 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
333 packet->len = inlen + 14;
340 device_total_in += packet->len;
342 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
347 static bool write_packet(vpn_packet_t *packet) {
348 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
350 switch(device_type) {
351 case DEVICE_TYPE_TUN:
352 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
353 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
358 case DEVICE_TYPE_TAP:
359 if(write(device_fd, packet->data, packet->len) < 0) {
360 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
369 device_total_out += packet->len;
374 static void dump_device_stats(void) {
375 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
376 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
377 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
380 const devops_t os_devops = {
381 .setup = setup_device,
382 .close = close_device,
384 .write = write_packet,
385 .dump_stats = dump_device_stats,