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>
30 #include "../device.h"
31 #include "../logger.h"
36 #include "../xalloc.h"
39 #warning Missing net/if_tun.h, using hardcoded value for TUNNEWPPA
40 #define TUNNEWPPA (('T'<<16) | 0x0001)
43 #define DEFAULT_TUN_DEVICE "/dev/tun"
44 #define DEFAULT_TAP_DEVICE "/dev/tap"
45 #define IP_DEVICE "/dev/udp"
50 } device_type = DEVICE_TYPE_TUN;
53 static int ip_fd = -1;
56 static char *device_info = NULL;
58 static bool setup_device(void) {
61 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
62 if(routing_mode == RMODE_ROUTER) {
63 device = xstrdup(DEFAULT_TUN_DEVICE);
65 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(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
79 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER) {
80 device_type = DEVICE_TYPE_TAP;
84 if(device_type == DEVICE_TYPE_TUN) {
85 device_info = "Solaris tun device";
87 device_info = "Solaris tap device";
90 /* The following is black magic copied from OpenVPN. */
92 if((ip_fd = open(IP_DEVICE, O_RDWR, 0)) < 0) {
93 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", IP_DEVICE, strerror(errno));
97 if((device_fd = open(device, O_RDWR, 0)) < 0) {
98 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
102 /* Get unit number. */
105 get_config_string(lookup_config(config_tree, "Interface"), &ptr);
107 while(*ptr && !isdigit(*ptr)) {
113 /* Assign a new PPA and get its unit number. */
115 struct strioctl strioc_ppa = {
117 .ic_len = sizeof(ppa),
118 .ic_dp = (char *) &ppa,
121 if(!*ptr) { /* no number given, try dynamic */
124 while(!found && ppa < 64) {
125 int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
137 logger(DEBUG_ALWAYS, LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
140 } else { /* try this particular one */
141 if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
142 logger(DEBUG_ALWAYS, LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
149 if((if_fd = open(device, O_RDWR, 0)) < 0) {
150 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
154 if(ioctl(if_fd, I_PUSH, "ip") < 0) {
155 logger(DEBUG_ALWAYS, LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
159 xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
162 /* Remove muxes just in case they are left over from a crashed tincd */
163 struct lifreq ifr = {};
164 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
166 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
167 int muxid = ifr.lifr_arp_muxid;
168 ioctl(ip_fd, I_PUNLINK, muxid);
169 muxid = ifr.lifr_ip_muxid;
170 ioctl(ip_fd, I_PUNLINK, muxid);
174 if(device_type == DEVICE_TYPE_TUN) {
175 /* Assign ppa according to the unit number returned by tun device */
176 if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
177 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
184 if(device_type == DEVICE_TYPE_TAP) {
185 struct lifreq ifr = {};
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 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
195 /* Assign ppa according to the unit number returned by tun device */
196 if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
197 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
201 if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
202 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set flags on %s %s!", device_info, device);
206 /* Push arp module to if_fd */
207 if(ioctl(if_fd, I_PUSH, "arp") < 0) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
212 /* Pop any modules on the stream */
214 if(ioctl(ip_fd, I_POP, NULL) < 0) {
219 /* Push arp module to ip_fd */
220 if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
221 logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s!", IP_DEVICE);
226 if((arp_fd = open(device, O_RDWR, 0)) < 0) {
227 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
231 /* Push arp module to arp_fd */
232 if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
233 logger(DEBUG_ALWAYS, LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
237 /* Set ifname to arp */
238 struct strioctl strioc_if = {
239 .ic_cmd = SIOCSLIFNAME,
240 .ic_len = sizeof(ifr),
241 .ic_dp = (char *) &ifr,
244 if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
245 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set ifname to %s %s", device_info, device);
250 int ip_muxid, arp_muxid;
252 if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
253 logger(DEBUG_ALWAYS, LOG_ERR, "Could not link %s %s to IP", device_info, device);
257 if(device_type == DEVICE_TYPE_TAP) {
258 if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
259 logger(DEBUG_ALWAYS, LOG_ERR, "Could not link %s %s to ARP", device_info, device);
266 struct lifreq ifr = {};
268 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
270 ifr.lifr_ip_muxid = ip_muxid;
272 if(device_type == DEVICE_TYPE_TAP) {
273 ifr.lifr_arp_muxid = arp_muxid;
276 if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
277 if(device_type == DEVICE_TYPE_TAP) {
278 ioctl(ip_fd, I_PUNLINK, arp_muxid);
281 ioctl(ip_fd, I_PUNLINK, ip_muxid);
282 logger(DEBUG_ALWAYS, LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
289 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
290 fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
293 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
298 static void close_device(void) {
300 struct lifreq ifr = {};
301 strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
303 if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
304 int muxid = ifr.lifr_arp_muxid;
305 ioctl(ip_fd, I_PUNLINK, muxid);
306 muxid = ifr.lifr_ip_muxid;
307 ioctl(ip_fd, I_PUNLINK, muxid);
322 static bool read_packet(vpn_packet_t *packet) {
327 switch(device_type) {
328 case DEVICE_TYPE_TUN:
329 sbuf.maxlen = MTU - 14;
330 sbuf.buf = (char *)DATA(packet) + 14;
332 if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
333 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
337 switch(DATA(packet)[14] >> 4) {
339 DATA(packet)[12] = 0x08;
340 DATA(packet)[13] = 0x00;
344 DATA(packet)[12] = 0x86;
345 DATA(packet)[13] = 0xDD;
349 logger(DEBUG_TRAFFIC, LOG_ERR, "Unknown IP version %d while reading packet from %s %s", DATA(packet)[14] >> 4, device_info, device);
353 memset(DATA(packet), 0, 12);
354 packet->len = sbuf.len + 14;
357 case DEVICE_TYPE_TAP:
359 sbuf.buf = (char *)DATA(packet);
361 if((result = getmsg(device_fd, NULL, &sbuf, &f)) < 0) {
362 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
366 packet->len = sbuf.len;
373 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
378 static bool write_packet(vpn_packet_t *packet) {
379 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
383 switch(device_type) {
384 case DEVICE_TYPE_TUN:
385 sbuf.len = packet->len - 14;
386 sbuf.buf = (char *)DATA(packet) + 14;
388 if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
389 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
395 case DEVICE_TYPE_TAP:
396 sbuf.len = packet->len;
397 sbuf.buf = (char *)DATA(packet);
399 if(putmsg(device_fd, NULL, &sbuf, 0) < 0) {
400 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
413 const devops_t os_devops = {
414 .setup = setup_device,
415 .close = close_device,
417 .write = write_packet,