2006-06-05 12:41:08 +00:00
|
|
|
/*
|
2008-09-09 19:07:09 +00:00
|
|
|
* Copyright (c) 2006-2008 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
2006-06-05 12:41:08 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-06-08 16:30:50 +00:00
|
|
|
#include <stdlib.h>
|
2006-06-05 12:41:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2006-06-05 12:48:30 +00:00
|
|
|
#include <stdint.h>
|
2006-06-05 12:41:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <err.h>
|
2006-06-11 15:19:07 +00:00
|
|
|
#include <arpa/inet.h>
|
2006-06-24 10:17:16 +00:00
|
|
|
#include <netinet/in.h>
|
2006-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
#include "tun.h"
|
|
|
|
|
|
|
|
#define TUN_MAX_TRY 50
|
|
|
|
|
2006-06-11 15:29:37 +00:00
|
|
|
char if_name[50];
|
2006-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
#ifdef LINUX
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
|
|
|
|
|
|
|
int
|
2006-06-24 10:21:50 +00:00
|
|
|
open_tun(const char *tun_device)
|
2006-06-05 12:41:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
2006-06-05 14:43:04 +00:00
|
|
|
int tun_fd;
|
|
|
|
struct ifreq ifreq;
|
2006-06-24 10:21:50 +00:00
|
|
|
char *tunnel = "/dev/net/tun";
|
2006-06-05 12:41:08 +00:00
|
|
|
|
2006-06-24 10:21:50 +00:00
|
|
|
if ((tun_fd = open(tunnel, O_RDWR)) < 0) {
|
|
|
|
warn("open_tun: %s: %s", tunnel, strerror(errno));
|
2006-06-11 14:42:19 +00:00
|
|
|
return -1;
|
2006-06-05 14:02:42 +00:00
|
|
|
}
|
2006-06-05 12:41:08 +00:00
|
|
|
|
2006-11-08 21:45:28 +00:00
|
|
|
memset(&ifreq, 0, sizeof(ifreq));
|
2006-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
ifreq.ifr_flags = IFF_TUN;
|
|
|
|
|
2006-06-24 10:21:50 +00:00
|
|
|
if (tun_device != NULL) {
|
2006-06-25 11:15:18 +00:00
|
|
|
strncpy(ifreq.ifr_name, tun_device, IFNAMSIZ);
|
2007-08-26 15:47:32 +00:00
|
|
|
ifreq.ifr_name[IFNAMSIZ-1] = '\0';
|
2006-06-25 11:15:18 +00:00
|
|
|
strncpy(if_name, tun_device, sizeof(if_name));
|
2007-08-26 15:47:32 +00:00
|
|
|
if_name[sizeof(if_name)-1] = '\0';
|
2006-06-05 12:41:08 +00:00
|
|
|
|
2006-06-25 11:15:18 +00:00
|
|
|
if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
|
|
|
|
printf("Opened %s\n", ifreq.ifr_name);
|
|
|
|
return tun_fd;
|
|
|
|
}
|
2006-06-05 12:41:08 +00:00
|
|
|
|
2006-06-25 11:15:18 +00:00
|
|
|
if (errno != EBUSY) {
|
|
|
|
warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-24 10:21:50 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < TUN_MAX_TRY; i++) {
|
|
|
|
snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i);
|
|
|
|
|
|
|
|
if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
|
|
|
|
printf("Opened %s\n", ifreq.ifr_name);
|
|
|
|
snprintf(if_name, sizeof(if_name), "dns%d", i);
|
|
|
|
return tun_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != EBUSY) {
|
|
|
|
warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-05 14:02:42 +00:00
|
|
|
}
|
2006-06-05 12:41:08 +00:00
|
|
|
|
2006-06-25 12:05:46 +00:00
|
|
|
warn("open_tun: Couldn't set interface name");
|
2006-06-24 10:21:50 +00:00
|
|
|
}
|
2006-06-11 14:42:19 +00:00
|
|
|
return -1;
|
2006-06-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* BSD */
|
|
|
|
|
|
|
|
int
|
2006-06-24 10:21:50 +00:00
|
|
|
open_tun(const char *tun_device)
|
2006-06-05 12:41:08 +00:00
|
|
|
{
|
2006-06-05 14:43:04 +00:00
|
|
|
int i;
|
|
|
|
int tun_fd;
|
|
|
|
char tun_name[50];
|
|
|
|
|
2006-06-05 12:41:08 +00:00
|
|
|
if (tun_device != NULL) {
|
2006-06-25 11:15:18 +00:00
|
|
|
snprintf(tun_name, sizeof(tun_name), "/dev/%s", tun_device);
|
|
|
|
strncpy(if_name, tun_device, sizeof(if_name));
|
2007-08-26 15:47:32 +00:00
|
|
|
if_name[sizeof(if_name)-1] = '\0';
|
2006-06-25 11:15:18 +00:00
|
|
|
|
|
|
|
if ((tun_fd = open(tun_name, O_RDWR)) < 0) {
|
|
|
|
warn("open_tun: %s: %s", tun_name, strerror(errno));
|
2006-06-11 14:42:19 +00:00
|
|
|
return -1;
|
2006-06-05 14:02:42 +00:00
|
|
|
}
|
2006-06-25 11:33:29 +00:00
|
|
|
|
|
|
|
printf("Opened %s\n", tun_name);
|
|
|
|
return tun_fd;
|
2006-06-05 12:41:08 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < TUN_MAX_TRY; i++) {
|
|
|
|
snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i);
|
|
|
|
|
2006-06-05 13:40:07 +00:00
|
|
|
if ((tun_fd = open(tun_name, O_RDWR)) >= 0) {
|
|
|
|
printf("Opened %s\n", tun_name);
|
2006-06-11 15:29:37 +00:00
|
|
|
snprintf(if_name, sizeof(if_name), "tun%d", i);
|
2006-06-05 14:43:04 +00:00
|
|
|
return tun_fd;
|
2006-06-05 13:40:07 +00:00
|
|
|
}
|
2006-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
if (errno == ENOENT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-06-25 12:05:46 +00:00
|
|
|
warn("open_tun: Failed to open tunneling device");
|
2006-06-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 14:42:19 +00:00
|
|
|
return -1;
|
2006-06-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 18:07:26 +00:00
|
|
|
#endif /* !LINUX */
|
2006-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
void
|
2006-06-05 14:43:04 +00:00
|
|
|
close_tun(int tun_fd)
|
2006-06-05 12:41:08 +00:00
|
|
|
{
|
|
|
|
if (tun_fd >= 0)
|
|
|
|
close(tun_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-08-26 15:47:32 +00:00
|
|
|
write_tun(int tun_fd, char *data, size_t len)
|
2006-06-05 12:41:08 +00:00
|
|
|
{
|
2006-08-25 15:34:20 +00:00
|
|
|
#if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
|
2006-06-24 11:33:57 +00:00
|
|
|
data += 4;
|
|
|
|
len -= 4;
|
2006-06-25 11:33:29 +00:00
|
|
|
#else /* !FREEBSD/DARWIN */
|
2006-06-11 13:49:36 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
data[0] = 0x00;
|
|
|
|
data[1] = 0x00;
|
|
|
|
data[2] = 0x08;
|
|
|
|
data[3] = 0x00;
|
2006-06-24 11:33:57 +00:00
|
|
|
#else /* OPENBSD */
|
2006-06-11 13:49:36 +00:00
|
|
|
data[0] = 0x00;
|
|
|
|
data[1] = 0x00;
|
|
|
|
data[2] = 0x00;
|
|
|
|
data[3] = 0x02;
|
|
|
|
#endif /* !LINUX */
|
2006-06-24 11:33:57 +00:00
|
|
|
#endif /* FREEBSD */
|
2006-06-11 13:49:36 +00:00
|
|
|
|
|
|
|
if (write(tun_fd, data, len) != len) {
|
2006-06-05 22:36:05 +00:00
|
|
|
warn("write_tun");
|
2006-06-05 14:02:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2006-06-05 12:41:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-26 15:47:32 +00:00
|
|
|
ssize_t
|
|
|
|
read_tun(int tun_fd, char *buf, size_t len)
|
2006-06-05 12:41:08 +00:00
|
|
|
{
|
2006-08-25 15:34:20 +00:00
|
|
|
#if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
|
2007-02-11 13:54:40 +00:00
|
|
|
/* FreeBSD/Darwin/NetBSD has no header */
|
2006-06-24 11:33:57 +00:00
|
|
|
return read(tun_fd, buf + 4, len - 4) + 4;
|
|
|
|
#else /* !FREEBSD */
|
2006-06-11 13:49:36 +00:00
|
|
|
return read(tun_fd, buf, len);
|
2006-06-24 11:33:57 +00:00
|
|
|
#endif /* !FREEBSD */
|
2006-06-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 15:19:07 +00:00
|
|
|
int
|
|
|
|
tun_setip(const char *ip)
|
|
|
|
{
|
|
|
|
char cmdline[512];
|
2007-06-17 12:19:23 +00:00
|
|
|
#ifndef LINUX
|
|
|
|
int r;
|
|
|
|
#endif
|
2006-06-11 15:19:07 +00:00
|
|
|
|
2006-06-23 07:58:36 +00:00
|
|
|
if (inet_addr(ip) != INADDR_NONE) {
|
2006-06-11 15:19:07 +00:00
|
|
|
snprintf(cmdline, sizeof(cmdline),
|
2006-06-24 11:33:57 +00:00
|
|
|
"/sbin/ifconfig %s %s %s netmask 255.255.255.0",
|
2006-06-11 15:24:20 +00:00
|
|
|
if_name,
|
2006-06-24 11:33:57 +00:00
|
|
|
ip,
|
2006-06-11 15:19:07 +00:00
|
|
|
ip);
|
2006-06-11 15:29:37 +00:00
|
|
|
|
|
|
|
printf("Setting IP of %s to %s\n", if_name, ip);
|
2006-06-11 15:19:07 +00:00
|
|
|
#ifndef LINUX
|
|
|
|
r = system(cmdline);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
snprintf(cmdline, sizeof(cmdline),
|
|
|
|
"/sbin/route add %s/24 %s",
|
|
|
|
ip, ip);
|
|
|
|
}
|
2006-06-11 15:29:37 +00:00
|
|
|
printf("Adding route %s/24 to %s\n", ip, ip);
|
2006-06-11 15:19:07 +00:00
|
|
|
#endif
|
|
|
|
return system(cmdline);
|
2006-06-23 07:58:36 +00:00
|
|
|
} else {
|
|
|
|
printf("Invalid IP: %s!\n", ip);
|
2006-06-11 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-08-26 15:47:32 +00:00
|
|
|
tun_setmtu(const size_t mtu)
|
2006-06-11 15:19:07 +00:00
|
|
|
{
|
|
|
|
char cmdline[512];
|
|
|
|
|
|
|
|
if (mtu > 200 && mtu < 1500) {
|
|
|
|
snprintf(cmdline, sizeof(cmdline),
|
2007-11-27 20:04:13 +00:00
|
|
|
"/sbin/ifconfig %s mtu %u",
|
2006-06-11 15:24:20 +00:00
|
|
|
if_name,
|
2006-06-11 15:19:07 +00:00
|
|
|
mtu);
|
|
|
|
|
2007-11-27 20:04:13 +00:00
|
|
|
printf("Setting MTU of %s to %u\n", if_name, mtu);
|
2006-06-11 15:19:07 +00:00
|
|
|
return system(cmdline);
|
2006-06-11 18:07:26 +00:00
|
|
|
} else {
|
2007-11-27 20:04:13 +00:00
|
|
|
warn("MTU out of range: %u\n", mtu);
|
2006-06-11 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|