If we have a Linux tun/tap device and we are in router mode, open the device

in tun mode.
This commit is contained in:
Guus Sliepen 2003-06-11 19:09:52 +00:00
parent 9e02a3d563
commit 451800eda8

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: device.c,v 1.1.2.13 2002/09/15 22:19:19 guus Exp $ $Id: device.c,v 1.1.2.14 2003/06/11 19:09:52 guus Exp $
*/ */
#include "config.h" #include "config.h"
@ -48,11 +48,15 @@
#include "conf.h" #include "conf.h"
#include "net.h" #include "net.h"
#include "subnet.h" #include "subnet.h"
#include "route.h"
#include "system.h" #include "system.h"
#define DEVICE_TYPE_ETHERTAP 0 enum {
#define DEVICE_TYPE_TUNTAP 1 DEVICE_TYPE_ETHERTAP,
DEVICE_TYPE_TUN,
DEVICE_TYPE_TAP,
};
int device_fd = -1; int device_fd = -1;
int device_type; int device_type;
@ -104,20 +108,24 @@ int setup_device(void)
/* Ok now check if this is an old ethertap or a new tun/tap thingie */ /* Ok now check if this is an old ethertap or a new tun/tap thingie */
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; if(routing_mode == RMODE_ROUTER) {
ifr.ifr_flags = IFF_TUN;
device_type = DEVICE_TYPE_TUN;
device_info = _("Linux tun/tap device (tun mode)");
} else {
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
device_type = DEVICE_TYPE_TAP;
device_info = _("Linux tun/tap device (tap mode)");
}
if(interface) if(interface)
strncpy(ifr.ifr_name, interface, IFNAMSIZ); strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if(!ioctl(device_fd, TUNSETIFF, (void *) &ifr)) { if(!ioctl(device_fd, TUNSETIFF, (void *) &ifr)) {
device_info = _("Linux tun/tap device");
device_type = DEVICE_TYPE_TUNTAP;
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
interface = ifrname; interface = ifrname;
} else if(!ioctl(device_fd, (('T' << 8) | 202), (void *) &ifr)) { } else if(!ioctl(device_fd, (('T' << 8) | 202), (void *) &ifr)) {
syslog(LOG_WARNING, _("Old ioctl() request was needed for %s"), device); syslog(LOG_WARNING, _("Old ioctl() request was needed for %s"), device);
device_type = DEVICE_TYPE_TUNTAP;
device_info = _("Linux tun/tap device");
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
interface = ifrname; interface = ifrname;
} else } else
@ -150,27 +158,40 @@ int read_packet(vpn_packet_t *packet)
cp(); cp();
if(device_type == DEVICE_TYPE_TUNTAP) { switch(device_type) {
lenin = read(device_fd, packet->data, MTU); case DEVICE_TYPE_TUN:
lenin = read(device_fd, packet->data + 10, MTU - 10);
if(lenin <= 0) { if(lenin <= 0) {
syslog(LOG_ERR, _("Error while reading from %s %s: %s"), syslog(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno)); device_info, device, strerror(errno));
return -1; return -1;
} }
packet->len = lenin; packet->len = lenin + 10;
} else { /* ethertap */ break;
case DEVICE_TYPE_TAP:
lenin = read(device_fd, packet->data, MTU);
lenin = read(device_fd, packet->data - 2, MTU + 2); if(lenin <= 0) {
syslog(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno));
return -1;
}
if(lenin <= 0) { packet->len = lenin;
syslog(LOG_ERR, _("Error while reading from %s %s: %s"), break;
device_info, device, strerror(errno)); case DEVICE_TYPE_ETHERTAP:
return -1; lenin = read(device_fd, packet->data - 2, MTU + 2);
}
packet->len = lenin - 2; if(lenin <= 0) {
syslog(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno));
return -1;
}
packet->len = lenin - 2;
break;
} }
device_total_in += packet->len; device_total_in += packet->len;
@ -191,20 +212,31 @@ int write_packet(vpn_packet_t *packet)
syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"), syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
packet->len, device_info); packet->len, device_info);
if(device_type == DEVICE_TYPE_TUNTAP) { switch(device_type) {
if(write(device_fd, packet->data, packet->len) < 0) { case DEVICE_TYPE_TUN:
syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, packet->data[10] = packet->data[11] = 0;
strerror(errno)); if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
return -1; syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
} strerror(errno));
} else { /* ethertap */ return -1;
*(short int *)(packet->data - 2) = packet->len; }
break;
case DEVICE_TYPE_TAP:
if(write(device_fd, packet->data, packet->len) < 0) {
syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
return -1;
}
break;
case DEVICE_TYPE_ETHERTAP:
*(short int *)(packet->data - 2) = packet->len;
if(write(device_fd, packet->data - 2, packet->len + 2) < 0) { if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return -1;
} }
break;
} }
device_total_out += packet->len; device_total_out += packet->len;