lokinet/vendor/libtuntap-master/tuntap-unix-darwin.c

203 lines
5.2 KiB
C
Raw Normal View History

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>
#include <net/if_var.h>
2018-06-23 12:18:31 +00:00
#include <net/if_types.h>
#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>
#include <errno.h>
2018-06-23 12:18:31 +00:00
#include "tuntap.h"
#include <sys/kern_control.h>
#include <sys/sys_domain.h>
#include <sys/kern_event.h>
2018-06-23 12:18:31 +00:00
#define APPLE_UTUN "com.apple.net.utun_control"
#define UTUN_OPT_IFNAME 2
2019-01-10 17:49:38 +00:00
int
tuntap_sys_start(struct device *dev, int mode, int tun)
{
2019-01-10 17:49:38 +00:00
(void)mode;
(void)tun;
uint32_t namesz = IFNAMSIZ;
2018-09-24 16:07:34 +00:00
char name[IFNAMSIZ + 1];
int fd;
char *ifname;
fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if(fd == -1)
return fd;
2018-09-24 16:07:34 +00:00
struct ctl_info info;
memset(&info, 0, sizeof(info));
strncpy(info.ctl_name, APPLE_UTUN, strlen(APPLE_UTUN));
2018-09-24 16:07:34 +00:00
if(ioctl(fd, CTLIOCGINFO, &info) < 0)
{
tuntap_log(TUNTAP_LOG_ERR, "call to ioctl() failed");
tuntap_log(TUNTAP_LOG_ERR, strerror(errno));
close(fd);
return -1;
}
2018-09-24 16:07:34 +00:00
struct sockaddr_ctl addr;
addr.sc_id = info.ctl_id;
2018-09-24 16:07:34 +00:00
addr.sc_len = sizeof(addr);
addr.sc_family = AF_SYSTEM;
addr.ss_sysaddr = AF_SYS_CONTROL;
2019-01-03 21:10:40 +00:00
addr.sc_unit = 0;
2018-09-24 16:07:34 +00:00
if(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
close(fd);
return -1;
}
ifname = name;
2018-09-24 16:07:34 +00:00
if(getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, ifname, &namesz) < 0)
{
close(fd);
return -1;
}
strncpy(dev->if_name, ifname, sizeof(dev->if_name));
2018-09-24 16:07:34 +00:00
return fd;
}
2018-06-23 12:18:31 +00:00
void
tuntap_sys_destroy(struct device *dev)
{
2018-09-24 16:07:34 +00:00
(void)dev;
2018-06-23 12:18:31 +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
tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s4, uint32_t bits)
{
struct sockaddr_in mask;
2018-09-24 16:07:34 +00:00
mask.sin_family = AF_INET;
mask.sin_addr.s_addr = bits;
2018-09-24 16:07:34 +00:00
mask.sin_len = sizeof(struct sockaddr_in);
char addrbuf[32];
2019-02-06 16:38:28 +00:00
inet_ntop(AF_INET, s4, addrbuf, sizeof(addrbuf));
char buf[1028];
2018-09-24 16:07:34 +00:00
const char *addr = addrbuf;
2019-02-06 18:34:07 +00:00
2019-02-06 16:38:28 +00:00
char daddrbuf[32];
in_addr_t daddr_s4 = {s4->s_addr & bits};
inet_ntop(AF_INET, &daddr_s4, daddrbuf, sizeof(daddrbuf));
const char *daddr = daddrbuf;
2019-02-06 18:34:07 +00:00
2018-09-24 16:07:34 +00:00
const char *netmask = inet_ntoa(mask.sin_addr);
/** because fuck this other stuff */
2019-02-06 16:38:28 +00:00
snprintf(buf, sizeof(buf), "ifconfig %s %s %s mtu 1380 netmask 255.255.255.255 up",
dev->if_name, addr, daddr);
tuntap_log(TUNTAP_LOG_INFO, buf);
system(buf);
2019-02-06 18:34:07 +00:00
snprintf(buf, sizeof(buf),
"route add %s -netmask %s -interface %s", daddr, netmask, dev->if_name);
tuntap_log(TUNTAP_LOG_INFO, buf);
system(buf);
2018-09-24 16:07:34 +00:00
snprintf(buf, sizeof(buf),
2019-02-06 18:34:07 +00:00
"route add %s -interface lo0", addr);
tuntap_log(TUNTAP_LOG_INFO, buf);
system(buf);
2019-02-06 18:34:07 +00:00
/* Simpler than calling SIOCSIFADDR and/or SIOCSIFBRDADDR */
/*
2018-09-24 16:07:34 +00:00
if(ioctl(dev->ctrl_sock, SIOCSIFADDR, &ifa) == -1)
{
tuntap_log(TUNTAP_LOG_ERR, "Can't set IP");
tuntap_log(TUNTAP_LOG_ERR, strerror(errno));
return -1;
}
*/
/*
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);
2018-09-24 16:07:34 +00:00
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;
*/
return 0;
2018-06-23 12:18:31 +00:00
}
int
tuntap_sys_set_descr(__attribute__((unused)) struct device *dev,
__attribute__((unused)) const char *descr,
__attribute__((unused)) 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
}