2018-06-23 12:18:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Tristan Le Guern <tleguern@bouledef.eu>
|
|
|
|
*
|
|
|
|
* 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 <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
2018-09-20 22:31:34 +00:00
|
|
|
#include <net/if_var.h>
|
2018-06-23 12:18:31 +00:00
|
|
|
#include <net/if_types.h>
|
2018-09-20 22:31:34 +00:00
|
|
|
#include <net/route.h>
|
2018-06-23 12:18:31 +00:00
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-09-20 22:31:34 +00:00
|
|
|
#include <errno.h>
|
2018-06-23 12:18:31 +00:00
|
|
|
#include "tuntap.h"
|
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
#include <sys/kern_control.h>
|
|
|
|
#include <sys/sys_domain.h>
|
|
|
|
#include <sys/kern_event.h>
|
2018-06-23 12:18:31 +00:00
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
#define APPLE_UTUN "com.apple.net.utun_control"
|
|
|
|
#define UTUN_OPT_IFNAME 2
|
|
|
|
|
|
|
|
static int
|
|
|
|
fucky_tuntap_sys_start(struct device *dev, int mode, int tun)
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
uint32_t namesz = IFNAMSIZ;
|
|
|
|
char name[IFNAMSIZ+1];
|
2018-09-20 14:16:24 +00:00
|
|
|
int fd;
|
2018-09-20 22:31:34 +00:00
|
|
|
char *ifname;
|
|
|
|
|
2018-09-20 14:16:24 +00:00
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
|
|
|
|
if(fd == -1)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "utun%i", tun);
|
2018-09-20 14:16:24 +00:00
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
struct ctl_info info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
strncpy(info.ctl_name, APPLE_UTUN, strlen(APPLE_UTUN));
|
2018-09-20 14:16:24 +00:00
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
if (ioctl(fd, CTLIOCGINFO, &info) < 0)
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "call to ioctl() failed");
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, strerror(errno));
|
|
|
|
close(fd);
|
2018-09-20 14:16:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
|
|
|
|
struct sockaddr_ctl addr;
|
|
|
|
addr.sc_id = info.ctl_id;
|
|
|
|
|
|
|
|
addr.sc_len = sizeof(addr);
|
|
|
|
addr.sc_family = AF_SYSTEM;
|
|
|
|
addr.ss_sysaddr = AF_SYS_CONTROL;
|
|
|
|
addr.sc_unit = tun + 1;
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
close(fd);
|
2018-09-20 14:16:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
ifname = name;
|
|
|
|
if(getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME,
|
|
|
|
ifname, &namesz) < 0 )
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
close(fd);
|
2018-09-20 14:16:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
strncpy(dev->if_name, ifname, sizeof(dev->if_name));
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2018-09-20 14:16:24 +00:00
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
int tuntap_sys_start(struct device * dev, int mode, int tun)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
while(tun < 128)
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
// yes linear complexity here
|
|
|
|
// sue me but I blame apple
|
|
|
|
fd = fucky_tuntap_sys_start(dev, mode, tun);
|
|
|
|
if(fd != -1)
|
2018-09-20 14:16:24 +00:00
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
return fd;
|
2018-09-20 14:16:24 +00:00
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
++tun;
|
2018-09-20 14:16:24 +00:00
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
return -1;
|
2018-06-23 12:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-20 14:16:24 +00:00
|
|
|
tuntap_sys_destroy(struct device *dev)
|
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
(void) dev;
|
2018-06-23 12:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-09-20 14:16:24 +00:00
|
|
|
tuntap_sys_set_hwaddr(struct device *dev, struct ether_addr *eth_addr)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
(void)memset(&ifr, '\0', sizeof ifr);
|
|
|
|
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
|
|
|
|
ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
|
|
|
|
ifr.ifr_addr.sa_family = AF_LINK;
|
|
|
|
(void)memcpy(ifr.ifr_addr.sa_data, eth_addr, ETHER_ADDR_LEN);
|
|
|
|
if(ioctl(dev->ctrl_sock, SIOCSIFLLADDR, &ifr) < 0)
|
|
|
|
{
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't set link-layer address");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-06-23 12:18:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:31:34 +00:00
|
|
|
struct tuntap_rtmsg
|
|
|
|
{
|
|
|
|
struct rt_msghdr hdr;
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
struct sockaddr_in mask;
|
|
|
|
struct sockaddr_in daddr;
|
|
|
|
};
|
|
|
|
|
2018-06-23 12:18:31 +00:00
|
|
|
int
|
2018-09-20 14:16:24 +00:00
|
|
|
tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s4, uint32_t bits)
|
|
|
|
{
|
|
|
|
struct sockaddr_in mask;
|
2018-09-20 22:31:34 +00:00
|
|
|
mask.sin_family = AF_INET;
|
2018-09-20 14:16:24 +00:00
|
|
|
mask.sin_addr.s_addr = bits;
|
2018-09-20 22:31:34 +00:00
|
|
|
mask.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
char addrbuf[32];
|
|
|
|
inet_ntop(AF_INET, s4, addrbuf, sizeof(struct sockaddr_in));
|
|
|
|
char buf[1028];
|
|
|
|
const char * addr = addrbuf;
|
|
|
|
const char * netmask = inet_ntoa(mask.sin_addr);
|
|
|
|
/** because fuck this other stuff */
|
|
|
|
snprintf(buf, sizeof(buf), "ifconfig %s %s %s mtu 1380 netmask %s up", dev->if_name, addr, addr, netmask);
|
|
|
|
tuntap_log(TUNTAP_LOG_INFO, buf);
|
|
|
|
system(buf);
|
|
|
|
snprintf(buf, sizeof(buf), "route add -cloning -net %s -netmask %s -interface %s", addr, netmask, dev->if_name);
|
|
|
|
tuntap_log(TUNTAP_LOG_INFO, buf);
|
|
|
|
system(buf);
|
2018-09-20 14:16:24 +00:00
|
|
|
/* Simpler than calling SIOCSIFADDR and/or SIOCSIFBRDADDR */
|
2018-09-20 22:31:34 +00:00
|
|
|
/*
|
2018-09-20 14:16:24 +00:00
|
|
|
if(ioctl(dev->ctrl_sock, SIOCSIFADDR, &ifa) == -1)
|
|
|
|
{
|
2018-09-20 22:31:34 +00:00
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't set IP");
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, strerror(errno));
|
2018-09-20 14:16:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-20 22:31:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
int fd = socket(PF_ROUTE, SOCK_RAW, AF_INET);
|
|
|
|
|
|
|
|
struct tuntap_rtmsg msg;
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.hdr.rtm_msglen = sizeof(msg) - sizeof(struct rt_msghdr);
|
|
|
|
msg.hdr.rtm_version = RTM_VERSION;
|
|
|
|
msg.hdr.rtm_type = RTM_ADD;
|
|
|
|
msg.hdr.rtm_addrs = RTA_NETMASK | RTA_IFA | RTA_DST;
|
|
|
|
msg.hdr.rtm_flags = RTF_UP | RTF_STATIC | RTF_IFSCOPE;
|
|
|
|
msg.hdr.rtm_index = if_nametoindex(dev->if_name);
|
|
|
|
msg.hdr.rtm_pid = getpid();
|
|
|
|
|
|
|
|
msg.saddr.sin_addr.s_addr = s4->s_addr & bits;
|
|
|
|
msg.saddr.sin_family = AF_INET;
|
|
|
|
msg.saddr.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
msg.daddr.sin_addr.s_addr = s4->s_addr;
|
|
|
|
msg.daddr.sin_family = AF_INET;
|
|
|
|
msg.daddr.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
msg.mask.sin_addr.s_addr = bits;
|
|
|
|
msg.mask.sin_family = AF_INET;
|
|
|
|
msg.mask.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
int res = write(fd, &msg, sizeof(msg));
|
|
|
|
if(res == -1)
|
|
|
|
{
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "did not add route");
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, strerror(errno));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return res == -1 ? -1 : 0;
|
|
|
|
*/
|
2018-09-20 14:16:24 +00:00
|
|
|
return 0;
|
2018-06-23 12:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-09-20 14:16:24 +00:00
|
|
|
tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len)
|
|
|
|
{
|
|
|
|
tuntap_log(TUNTAP_LOG_NOTICE,
|
|
|
|
"Your system does not support tuntap_set_descr()");
|
|
|
|
return -1;
|
2018-06-23 12:18:31 +00:00
|
|
|
}
|