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

408 lines
8.4 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.
*
* Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 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.
*/
2019-10-22 15:58:34 +00:00
#include <tuntap.h>
#ifdef __sun
#define BSD_COMP
#define TUNSDEBUG _IOW('t', 90, int)
#endif
2018-06-23 12:18:31 +00:00
#include <sys/ioctl.h>
#include <sys/socket.h>
2018-08-15 15:36:34 +00:00
#include <sys/types.h>
2018-06-23 12:18:31 +00:00
#include <arpa/inet.h>
2018-08-15 15:36:34 +00:00
#include <netinet/in.h>
#if defined(Linux)
2018-08-15 15:36:34 +00:00
#include <linux/if_tun.h>
#include <linux/if_ether.h>
2019-10-22 15:58:34 +00:00
#include <linux/if.h>
2018-06-23 12:18:31 +00:00
#else
2018-08-15 15:36:34 +00:00
#include <net/if.h>
#if defined(DragonFly)
2018-08-15 15:36:34 +00:00
#include <net/tun/if_tun.h>
#elif defined(ANDROID)
2018-11-06 14:06:09 +00:00
#include <linux/if_tun.h>
2019-07-05 12:51:54 +00:00
#elif defined(Darwin)
#include <sys/uio.h>
#include <unistd.h>
#else
2018-08-15 15:36:34 +00:00
#include <net/if_tun.h>
#endif
#include <netinet/if_ether.h>
#include <netinet/in.h>
2018-06-23 12:18:31 +00:00
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int
2018-08-15 15:36:34 +00:00
tuntap_start(struct device *dev, int mode, int tun)
{
int sock;
int fd;
fd = sock = -1;
/* Don't re-initialise a previously started device */
if(dev->tun_fd != -1)
{
tuntap_log(TUNTAP_LOG_ERR, "Device is already started");
return -1;
}
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock == -1)
{
goto clean;
}
dev->ctrl_sock = sock;
if(mode & TUNTAP_MODE_PERSIST && tun == TUNTAP_ID_ANY)
{
goto clean; /* XXX: Explain why */
}
fd = tuntap_sys_start(dev, mode, tun);
if(fd == -1)
{
goto clean;
}
dev->tun_fd = fd;
tuntap_set_debug(dev, 0);
return 0;
2018-06-23 12:18:31 +00:00
clean:
2018-08-15 15:36:34 +00:00
if(fd != -1)
{
(void)close(fd);
}
if(sock != -1)
{
(void)close(sock);
}
return -1;
2018-06-23 12:18:31 +00:00
}
void
2018-08-15 15:36:34 +00:00
tuntap_release(struct device *dev)
{
(void)close(dev->tun_fd);
(void)close(dev->ctrl_sock);
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_set_descr(struct device *dev, const char *descr)
{
size_t len;
if(descr == NULL)
{
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'descr'");
return -1;
}
len = strlen(descr);
if(len > IF_DESCRSIZE)
{
/* The value will be troncated */
tuntap_log(TUNTAP_LOG_WARN, "Parameter 'descr' is too long");
}
if(tuntap_sys_set_descr(dev, descr, len) == -1)
{
return -1;
}
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_set_ifname(struct device *dev, const char *ifname)
{
size_t len;
if(ifname == NULL)
{
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'ifname'");
return -1;
}
len = strlen(ifname);
2019-08-02 09:27:27 +00:00
if(len >= IF_NAMESIZE)
2018-08-15 15:36:34 +00:00
{
tuntap_log(TUNTAP_LOG_ERR, "Parameter 'ifname' is too long");
return -1;
}
if(tuntap_sys_set_ifname(dev, ifname, len) == -1)
{
2019-01-10 22:50:20 +00:00
return 0;
2018-08-15 15:36:34 +00:00
}
(void)memset(dev->if_name, 0, IF_NAMESIZE);
2019-08-02 09:27:27 +00:00
(void)strncpy(dev->if_name, ifname, len + 1);
2018-08-15 15:36:34 +00:00
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_up(struct device *dev)
{
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
#ifndef __sun
2018-08-15 15:36:34 +00:00
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
#else
(void)memcpy(ifr.ifr_name, dev->internal_name, sizeof dev->internal_name);
#endif
2018-08-15 15:36:34 +00:00
ifr.ifr_flags = (short int)dev->flags;
ifr.ifr_flags |= IFF_UP;
if(ioctl(dev->ctrl_sock, SIOCSIFFLAGS, &ifr) == -1)
{
return -1;
}
dev->flags = ifr.ifr_flags;
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_down(struct device *dev)
{
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
#ifndef __sun
2018-08-15 15:36:34 +00:00
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
#else
(void)memcpy(ifr.ifr_name, dev->internal_name, sizeof dev->internal_name);
#endif
2018-08-15 15:36:34 +00:00
ifr.ifr_flags = (short)dev->flags;
ifr.ifr_flags &= ~IFF_UP;
if(ioctl(dev->ctrl_sock, SIOCSIFFLAGS, &ifr) == -1)
{
return -1;
}
dev->flags = ifr.ifr_flags;
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_get_mtu(struct device *dev)
{
struct ifreq ifr;
/* Only accept started device */
if(dev->tun_fd == -1)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
if(ioctl(dev->ctrl_sock, SIOCGIFMTU, &ifr) == -1)
{
tuntap_log(TUNTAP_LOG_ERR, "Can't get MTU");
return -1;
}
return ifr.ifr_mtu;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_set_mtu(struct device *dev, int mtu)
{
struct ifreq ifr;
/* Only accept started device */
if(dev->tun_fd == -1)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
ifr.ifr_mtu = mtu;
if(ioctl(dev->ctrl_sock, SIOCSIFMTU, &ifr) == -1)
{
tuntap_log(TUNTAP_LOG_ERR, "Can't set MTU");
return -1;
}
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_read(struct device *dev, void *buf, size_t size)
{
int n;
/* Only accept started device */
if(dev->tun_fd == -1)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
2019-07-06 14:38:53 +00:00
#ifdef Darwin
unsigned int pktinfo = 0;
2019-07-08 14:22:25 +00:00
const struct iovec vecs[2] = {
{.iov_base = &pktinfo, .iov_len = sizeof(unsigned int)},
{.iov_base = buf, .iov_len = size}};
n = readv(dev->tun_fd, vecs, 2);
2019-07-06 14:38:53 +00:00
if(n >= (int)(sizeof(unsigned int)))
n -= sizeof(unsigned int);
#else
2018-08-15 15:36:34 +00:00
n = read(dev->tun_fd, buf, size);
#endif
2018-08-15 15:36:34 +00:00
if(n == -1)
{
tuntap_log(TUNTAP_LOG_WARN, "Can't to read from device");
return -1;
}
return n;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_write(struct device *dev, void *buf, size_t size)
{
int n;
/* Only accept started device */
if(dev->tun_fd == -1)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
#if defined(Darwin)
/** darwin has packet info so let's use writev */
2019-07-08 14:23:26 +00:00
static unsigned int af4 = htonl(AF_INET);
2019-07-05 12:51:54 +00:00
static unsigned int af6 = htonl(AF_INET6);
2019-07-08 14:22:25 +00:00
const struct iovec vecs[2] = {
{.iov_base = (((unsigned char *)buf)[0] & 0x60) == 0x60 ? &af6 : &af4,
.iov_len = sizeof(unsigned int)},
{.iov_base = buf, .iov_len = size}};
n = writev(dev->tun_fd, vecs, 2);
if(n >= (int)sizeof(unsigned int))
n -= sizeof(unsigned int);
2019-07-08 14:22:25 +00:00
#else
n = write(dev->tun_fd, buf, size);
#endif
if(n < 0)
2018-08-15 15:36:34 +00:00
{
tuntap_log(TUNTAP_LOG_WARN, "Can't write to device");
return -1;
}
return n;
2018-06-23 12:18:31 +00:00
}
2019-07-08 14:22:25 +00:00
int
2018-08-15 15:36:34 +00:00
tuntap_get_readable(struct device *dev)
{
int n;
n = 0;
if(ioctl(dev->tun_fd, FIONREAD, &n) == -1)
{
tuntap_log(TUNTAP_LOG_INFO,
"Your system does not support"
" FIONREAD, fallback to MTU");
return tuntap_get_mtu(dev);
}
return n;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_set_nonblocking(struct device *dev, int set)
{
if(ioctl(dev->tun_fd, FIONBIO, &set) == -1)
{
switch(set)
{
case 0:
tuntap_log(TUNTAP_LOG_ERR, "Can't unset nonblocking");
break;
case 1:
tuntap_log(TUNTAP_LOG_ERR, "Can't set nonblocking");
break;
default:
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'set'");
}
return -1;
}
return 0;
2018-06-23 12:18:31 +00:00
}
int
2018-08-15 15:36:34 +00:00
tuntap_set_debug(struct device *dev, int set)
{
/* Only accept started device */
if(dev->tun_fd == -1)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
2018-06-23 12:18:31 +00:00
2019-10-28 15:27:02 +00:00
#if defined(__linux__)
2019-10-22 15:58:34 +00:00
if(ioctl(dev->tun_fd, TUNSETDEBUG, &set) == -1)
2018-08-15 15:36:34 +00:00
{
switch(set)
{
case 0:
tuntap_log(TUNTAP_LOG_WARN, "Can't unset debug");
break;
case 1:
tuntap_log(TUNTAP_LOG_WARN, "Can't set debug");
break;
default:
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'set'");
}
return -1;
}
return 0;
2018-06-23 12:18:31 +00:00
#else
(void)set;
2018-08-15 15:36:34 +00:00
tuntap_log(TUNTAP_LOG_NOTICE,
"Your system does not support tuntap_set_debug()");
return -1;
2018-06-23 12:18:31 +00:00
#endif
}