2002-12-16 13:33:51 +00:00
|
|
|
/*
|
2003-04-11 09:40:12 +00:00
|
|
|
* TUN interface functions.
|
2004-09-17 11:30:40 +00:00
|
|
|
* Copyright (C) 2002, 2003, 2004 Mondru AB.
|
2017-08-02 22:47:03 +00:00
|
|
|
* Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org>
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
2003-04-11 09:40:12 +00:00
|
|
|
* The contents of this file may be used under the terms of the GNU
|
|
|
|
* General Public License Version 2, provided that the above copyright
|
|
|
|
* notice and this permission notice is included in all copies or
|
|
|
|
* substantial portions of the software.
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-04-11 09:40:12 +00:00
|
|
|
* tun.c: Contains all TUN functionality. Is able to handle multiple
|
|
|
|
* tunnels in the same program. Each tunnel is identified by the struct,
|
|
|
|
* which is passed to functions.
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <errno.h>
|
2003-04-11 09:40:12 +00:00
|
|
|
#include <net/route.h>
|
2017-08-02 22:47:03 +00:00
|
|
|
#include <net/if.h>
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
2004-01-09 15:19:20 +00:00
|
|
|
#include <linux/if_tun.h>
|
2003-04-11 09:40:12 +00:00
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
2004-02-04 22:57:41 +00:00
|
|
|
|
|
|
|
#elif defined (__FreeBSD__)
|
|
|
|
#include <net/if_tun.h>
|
2017-08-02 22:47:03 +00:00
|
|
|
#include <net/if_var.h>
|
|
|
|
#include <netinet/in_var.h>
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined (__APPLE__)
|
|
|
|
#include <net/if.h>
|
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#else
|
|
|
|
#error "Unknown platform!"
|
2004-01-09 15:19:20 +00:00
|
|
|
#endif
|
|
|
|
|
2002-12-16 13:33:51 +00:00
|
|
|
#include "tun.h"
|
2003-04-11 09:40:12 +00:00
|
|
|
#include "syserr.h"
|
|
|
|
|
2017-08-02 22:47:03 +00:00
|
|
|
static int tun_setaddr4(struct tun_t *this, struct in_addr *addr,
|
|
|
|
struct in_addr *dstaddr, struct in_addr *netmask);
|
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
|
2017-08-02 22:47:03 +00:00
|
|
|
#include <linux/ipv6.h>
|
|
|
|
|
2017-08-12 14:38:44 +00:00
|
|
|
static int tun_nlattr(struct nlmsghdr *n, int nsize, int type, void *d, int dlen)
|
2002-12-16 13:33:51 +00:00
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
int len = RTA_LENGTH(dlen);
|
|
|
|
int alen = NLMSG_ALIGN(n->nlmsg_len);
|
|
|
|
struct rtattr *rta = (struct rtattr *)(((void *)n) + alen);
|
|
|
|
if (alen + len > nsize)
|
|
|
|
return -1;
|
|
|
|
rta->rta_len = len;
|
|
|
|
rta->rta_type = type;
|
|
|
|
memcpy(RTA_DATA(rta), d, dlen);
|
|
|
|
n->nlmsg_len = alen + len;
|
|
|
|
return 0;
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
2004-01-10 06:38:43 +00:00
|
|
|
#endif
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2017-08-12 14:38:44 +00:00
|
|
|
static int tun_sifflags(struct tun_t *this, int flags)
|
2011-11-02 12:06:18 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
memset(&ifr, '\0', sizeof(ifr));
|
|
|
|
ifr.ifr_flags = flags;
|
|
|
|
strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
|
|
|
|
ifr.ifr_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2015-05-02 05:52:23 +00:00
|
|
|
return -1;
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
if (ioctl(fd, SIOCSIFFLAGS, &ifr)) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCSIFFLAGS) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tun_addaddr(struct tun_t *this,
|
|
|
|
struct in_addr *addr,
|
2011-11-02 12:06:18 +00:00
|
|
|
struct in_addr *dstaddr, struct in_addr *netmask)
|
|
|
|
{
|
2004-01-09 15:19:20 +00:00
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
2011-11-02 12:06:18 +00:00
|
|
|
struct {
|
|
|
|
struct nlmsghdr n;
|
|
|
|
struct ifaddrmsg i;
|
|
|
|
char buf[TUN_NLBUFSIZE];
|
|
|
|
} req;
|
|
|
|
|
|
|
|
struct sockaddr_nl local;
|
|
|
|
socklen_t addr_len;
|
|
|
|
int fd;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
if (!this->addrs) /* Use ioctl for first addr to make ping work */
|
2017-08-02 22:47:03 +00:00
|
|
|
return tun_setaddr4(this, addr, dstaddr, netmask);
|
2011-11-02 12:06:18 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
|
|
|
|
req.n.nlmsg_type = RTM_NEWADDR;
|
|
|
|
req.i.ifa_family = AF_INET;
|
|
|
|
req.i.ifa_prefixlen = 32; /* 32 FOR IPv4 */
|
|
|
|
req.i.ifa_flags = 0;
|
|
|
|
req.i.ifa_scope = RT_SCOPE_HOST; /* TODO or 0 */
|
2017-08-11 10:31:59 +00:00
|
|
|
req.i.ifa_index = if_nametoindex(this->devname);
|
|
|
|
if (!req.i.ifa_index) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "Unable to get ifindex for %s", this->devname);
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun_nlattr(&req.n, sizeof(req), IFA_ADDRESS, addr, sizeof(addr));
|
|
|
|
tun_nlattr(&req.n, sizeof(req), IFA_LOCAL, dstaddr, sizeof(dstaddr));
|
|
|
|
|
|
|
|
if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&local, 0, sizeof(local));
|
|
|
|
local.nl_family = AF_NETLINK;
|
|
|
|
local.nl_groups = 0;
|
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "bind() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_len = sizeof(local);
|
|
|
|
if (getsockname(fd, (struct sockaddr *)&local, &addr_len) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"getsockname() failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr_len != sizeof(local)) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0,
|
2011-11-02 12:06:18 +00:00
|
|
|
"Wrong address length %d", addr_len);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local.nl_family != AF_NETLINK) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0,
|
2011-11-02 12:06:18 +00:00
|
|
|
"Wrong address family %d", local.nl_family);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov.iov_base = (void *)&req.n;
|
|
|
|
iov.iov_len = req.n.nlmsg_len;
|
|
|
|
|
|
|
|
msg.msg_name = (void *)&nladdr;
|
2015-05-02 05:52:23 +00:00
|
|
|
msg.msg_namelen = sizeof(nladdr);
|
|
|
|
msg.msg_iov = &iov;
|
2011-11-02 12:06:18 +00:00
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
|
|
|
memset(&nladdr, 0, sizeof(nladdr));
|
|
|
|
nladdr.nl_family = AF_NETLINK;
|
|
|
|
nladdr.nl_pid = 0;
|
|
|
|
nladdr.nl_groups = 0;
|
|
|
|
|
|
|
|
req.n.nlmsg_seq = 0;
|
|
|
|
req.n.nlmsg_flags |= NLM_F_ACK;
|
|
|
|
|
2015-05-02 05:52:23 +00:00
|
|
|
status = sendmsg(fd, &msg, 0);
|
|
|
|
if (status != req.n.nlmsg_len) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "sendmsg() failed, returned %d", status);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = tun_sifflags(this, IFF_UP | IFF_RUNNING);
|
|
|
|
if (status == -1) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
this->addrs++;
|
|
|
|
return 0;
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined (__FreeBSD__) || defined (__APPLE__)
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
int fd;
|
|
|
|
struct ifaliasreq areq;
|
|
|
|
|
|
|
|
/* TODO: Is this needed on FreeBSD? */
|
|
|
|
if (!this->addrs) /* Use ioctl for first addr to make ping work */
|
2017-08-02 22:47:03 +00:00
|
|
|
return tun_setaddr4(this, addr, dstaddr, netmask); /* TODO dstaddr */
|
2011-11-02 12:06:18 +00:00
|
|
|
|
|
|
|
memset(&areq, 0, sizeof(areq));
|
|
|
|
|
|
|
|
/* Set up interface name */
|
|
|
|
strncpy(areq.ifra_name, this->devname, IFNAMSIZ);
|
|
|
|
areq.ifra_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */
|
|
|
|
|
|
|
|
((struct sockaddr_in *)&areq.ifra_addr)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)&areq.ifra_addr)->sin_len =
|
|
|
|
sizeof(areq.ifra_addr);
|
|
|
|
((struct sockaddr_in *)&areq.ifra_addr)->sin_addr.s_addr = addr->s_addr;
|
|
|
|
|
|
|
|
((struct sockaddr_in *)&areq.ifra_mask)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)&areq.ifra_mask)->sin_len =
|
|
|
|
sizeof(areq.ifra_mask);
|
|
|
|
((struct sockaddr_in *)&areq.ifra_mask)->sin_addr.s_addr =
|
|
|
|
netmask->s_addr;
|
|
|
|
|
|
|
|
/* For some reason FreeBSD uses ifra_broadcast for specifying dstaddr */
|
|
|
|
((struct sockaddr_in *)&areq.ifra_broadaddr)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)&areq.ifra_broadaddr)->sin_len =
|
|
|
|
sizeof(areq.ifra_broadaddr);
|
|
|
|
((struct sockaddr_in *)&areq.ifra_broadaddr)->sin_addr.s_addr =
|
|
|
|
dstaddr->s_addr;
|
|
|
|
|
|
|
|
/* Create a channel to the NET kernel. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCAIFADDR, (void *)&areq) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCAIFADDR) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
this->addrs++;
|
|
|
|
return 0;
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-01-09 15:19:20 +00:00
|
|
|
#endif
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2017-08-02 22:47:03 +00:00
|
|
|
static int tun_setaddr4(struct tun_t *this, struct in_addr *addr,
|
|
|
|
struct in_addr *dstaddr, struct in_addr *netmask)
|
2003-04-11 09:40:12 +00:00
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
memset(&ifr, '\0', sizeof(ifr));
|
|
|
|
ifr.ifr_addr.sa_family = AF_INET;
|
|
|
|
ifr.ifr_dstaddr.sa_family = AF_INET;
|
2004-02-04 22:57:41 +00:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
2011-11-02 12:06:18 +00:00
|
|
|
ifr.ifr_netmask.sa_family = AF_INET;
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
2011-11-02 12:06:18 +00:00
|
|
|
((struct sockaddr_in *)&ifr.ifr_addr)->sin_len =
|
|
|
|
sizeof(struct sockaddr_in);
|
|
|
|
((struct sockaddr_in *)&ifr.ifr_dstaddr)->sin_len =
|
|
|
|
sizeof(struct sockaddr_in);
|
2004-01-10 06:38:43 +00:00
|
|
|
#endif
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
|
|
|
|
ifr.ifr_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */
|
|
|
|
|
|
|
|
/* Create a channel to the NET kernel. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr) { /* Set the interface address */
|
|
|
|
this->addr.s_addr = addr->s_addr;
|
|
|
|
memcpy(&((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr, addr,
|
|
|
|
sizeof(*addr));
|
|
|
|
if (ioctl(fd, SIOCSIFADDR, (void *)&ifr) < 0) {
|
|
|
|
if (errno != EEXIST) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCSIFADDR) failed");
|
|
|
|
} else {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_NOTICE, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCSIFADDR): Address already exists");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dstaddr) { /* Set the destination address */
|
|
|
|
this->dstaddr.s_addr = dstaddr->s_addr;
|
|
|
|
memcpy(&((struct sockaddr_in *)&ifr.ifr_dstaddr)->sin_addr,
|
|
|
|
dstaddr, sizeof(*dstaddr));
|
|
|
|
if (ioctl(fd, SIOCSIFDSTADDR, (caddr_t) & ifr) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCSIFDSTADDR) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (netmask) { /* Set the netmask */
|
|
|
|
this->netmask.s_addr = netmask->s_addr;
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
2011-11-02 12:06:18 +00:00
|
|
|
memcpy(&((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr,
|
|
|
|
netmask, sizeof(*netmask));
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
2011-11-02 12:06:18 +00:00
|
|
|
((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr =
|
|
|
|
netmask->s_addr;
|
2004-01-10 06:38:43 +00:00
|
|
|
#endif
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
if (ioctl(fd, SIOCSIFNETMASK, (void *)&ifr) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCSIFNETMASK) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
this->addrs++;
|
2004-01-28 09:27:34 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
/* On linux the route to the interface is set automatically
|
|
|
|
on FreeBSD we have to do this manually */
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
/* TODO: How does it work on Solaris? */
|
|
|
|
|
|
|
|
tun_sifflags(this, IFF_UP | IFF_RUNNING);
|
2004-12-30 15:33:58 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#if defined(__FreeBSD__) || defined (__APPLE__)
|
2017-08-02 22:47:03 +00:00
|
|
|
tun_addroute(this, dstaddr, addr, &this->netmask);
|
2011-11-02 12:06:18 +00:00
|
|
|
this->routes = 1;
|
2004-01-28 09:27:34 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
return 0;
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 22:47:03 +00:00
|
|
|
static int tun_setaddr6(struct tun_t *this, struct in6_addr *addr, struct in6_addr *dstaddr,
|
|
|
|
size_t prefixlen)
|
|
|
|
{
|
|
|
|
struct in6_ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
ifr.ifr6_prefixlen = prefixlen;
|
|
|
|
ifr.ifr6_ifindex = if_nametoindex(this->devname);
|
|
|
|
if (ifr.ifr6_ifindex == 0) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0, "Error getting ifindex for %s\n", this->devname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
|
|
|
strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a channel to the NET kernel */
|
|
|
|
if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0, "socket() failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
if (addr) {
|
|
|
|
memcpy(&this->addr, addr, sizeof(*addr));
|
|
|
|
memcpy(&ifr.ifr6_addr, addr, sizeof(*addr));
|
|
|
|
if (ioctl(fd, SIOCSIFADDR, (void *) &ifr) < 0) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0, "ioctl(SIOCSIFADDR) failed");
|
|
|
|
} else {
|
|
|
|
SYS_ERR(DTUN, LOGL_NOTICE, 0, "ioctl(SIOCSIFADDR): Address alreadsy exists");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* FIXME: looks like this is not possible/necessary for IPv6? */
|
|
|
|
if (dstaddr) {
|
|
|
|
memcpy(&this->dstaddr, dstaddr, sizeof(*dstaddr));
|
|
|
|
memcpy(&ifr.ifr6_addr, dstaddr, sizeof(*dstaddr));
|
|
|
|
if (ioctl(fd, SIOCSIFDSTADDR, (caddr_t *) &ifr) < 0) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, "ioctl(SIOCSIFDSTADDR) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
|
|
|
if (addr)
|
|
|
|
memcpy(&ifr.ifr_ifru.ifru_addr, addr, sizeof(ifr.ifr_ifru.ifru_addr));
|
|
|
|
if (dstaddr)
|
|
|
|
memcpy(&ifr.ifr_ifru.ifru_dstaddr, dstaddr, sizeof(ifr.ifr_ifru.ifru_dstaddr));
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCSIFADDR_IN6, (struct ifreq *)&ifr) < 0) {
|
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, 0, "ioctl(SIOCSIFADDR_IN6) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
this->addrs++;
|
|
|
|
|
|
|
|
/* On linux the route to the interface is set automatically
|
|
|
|
on FreeBSD we have to do this manually */
|
|
|
|
|
|
|
|
/* TODO: How does it work on Solaris? */
|
|
|
|
|
|
|
|
tun_sifflags(this, IFF_UP | IFF_RUNNING);
|
|
|
|
|
|
|
|
#if 0 /* FIXME */
|
|
|
|
//#if defined(__FreeBSD__) || defined (__APPLE__)
|
|
|
|
tun_addroute6(this, dstaddr, addr, prefixlen);
|
|
|
|
this->routes = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tun_setaddr(struct tun_t *this, struct in46_addr *addr, struct in46_addr *dstaddr, size_t prefixlen)
|
|
|
|
{
|
|
|
|
struct in_addr netmask;
|
|
|
|
switch (addr->len) {
|
|
|
|
case 4:
|
|
|
|
netmask.s_addr = htonl(0xffffffff << (32 - prefixlen));
|
|
|
|
return tun_setaddr4(this, &addr->v4, dstaddr ? &dstaddr->v4 : NULL, &netmask);
|
|
|
|
case 16:
|
|
|
|
return tun_setaddr6(this, &addr->v6, dstaddr ? &dstaddr->v6 : NULL, prefixlen);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:38:44 +00:00
|
|
|
static int tun_route(struct tun_t *this,
|
2004-12-30 15:33:58 +00:00
|
|
|
struct in_addr *dst,
|
2011-11-02 12:06:18 +00:00
|
|
|
struct in_addr *gateway, struct in_addr *mask, int delete)
|
2003-04-11 09:40:12 +00:00
|
|
|
{
|
2004-01-10 06:38:43 +00:00
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
2004-01-10 06:38:43 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
struct rtentry r;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
memset(&r, '\0', sizeof(r));
|
|
|
|
r.rt_flags = RTF_UP | RTF_GATEWAY; /* RTF_HOST not set */
|
|
|
|
|
|
|
|
/* Create a channel to the NET kernel. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.rt_dst.sa_family = AF_INET;
|
|
|
|
r.rt_gateway.sa_family = AF_INET;
|
|
|
|
r.rt_genmask.sa_family = AF_INET;
|
|
|
|
memcpy(&((struct sockaddr_in *)&r.rt_dst)->sin_addr, dst, sizeof(*dst));
|
|
|
|
memcpy(&((struct sockaddr_in *)&r.rt_gateway)->sin_addr, gateway,
|
|
|
|
sizeof(*gateway));
|
|
|
|
memcpy(&((struct sockaddr_in *)&r.rt_genmask)->sin_addr, mask,
|
|
|
|
sizeof(*mask));
|
|
|
|
|
|
|
|
if (delete) {
|
|
|
|
if (ioctl(fd, SIOCDELRT, (void *)&r) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCDELRT) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ioctl(fd, SIOCADDRT, (void *)&r) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"ioctl(SIOCADDRT) failed");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
2004-01-27 19:47:49 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
struct {
|
|
|
|
struct rt_msghdr rt;
|
|
|
|
struct sockaddr_in dst;
|
|
|
|
struct sockaddr_in gate;
|
|
|
|
struct sockaddr_in mask;
|
|
|
|
} req;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
|
|
|
|
if ((fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&req, 0x00, sizeof(req));
|
|
|
|
|
|
|
|
rtm = &req.rt;
|
|
|
|
|
|
|
|
rtm->rtm_msglen = sizeof(req);
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
if (delete) {
|
|
|
|
rtm->rtm_type = RTM_DELETE;
|
|
|
|
} else {
|
|
|
|
rtm->rtm_type = RTM_ADD;
|
|
|
|
}
|
|
|
|
rtm->rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; /* TODO */
|
|
|
|
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
|
|
|
|
rtm->rtm_pid = getpid();
|
|
|
|
rtm->rtm_seq = 0044; /* TODO */
|
|
|
|
|
|
|
|
req.dst.sin_family = AF_INET;
|
|
|
|
req.dst.sin_len = sizeof(req.dst);
|
|
|
|
req.mask.sin_family = AF_INET;
|
|
|
|
req.mask.sin_len = sizeof(req.mask);
|
|
|
|
req.gate.sin_family = AF_INET;
|
|
|
|
req.gate.sin_len = sizeof(req.gate);
|
|
|
|
|
|
|
|
req.dst.sin_addr.s_addr = dst->s_addr;
|
|
|
|
req.mask.sin_addr.s_addr = mask->s_addr;
|
|
|
|
req.gate.sin_addr.s_addr = gateway->s_addr;
|
|
|
|
|
|
|
|
if (write(fd, rtm, rtm->rtm_msglen) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "write() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2004-01-10 06:38:43 +00:00
|
|
|
#endif
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
2004-12-30 15:33:58 +00:00
|
|
|
int tun_addroute(struct tun_t *this,
|
|
|
|
struct in_addr *dst,
|
2011-11-02 12:06:18 +00:00
|
|
|
struct in_addr *gateway, struct in_addr *mask)
|
2004-12-30 15:33:58 +00:00
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
return tun_route(this, dst, gateway, mask, 0);
|
2004-12-30 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tun_delroute(struct tun_t *this,
|
|
|
|
struct in_addr *dst,
|
2011-11-02 12:06:18 +00:00
|
|
|
struct in_addr *gateway, struct in_addr *mask)
|
2004-12-30 15:33:58 +00:00
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
return tun_route(this, dst, gateway, mask, 1);
|
2004-12-30 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 13:07:02 +00:00
|
|
|
int tun_new(struct tun_t **tun, const char *dev_name)
|
2003-04-11 09:40:12 +00:00
|
|
|
{
|
2004-01-10 06:38:43 +00:00
|
|
|
|
2004-02-04 22:57:41 +00:00
|
|
|
#if defined(__linux__)
|
2011-11-02 12:06:18 +00:00
|
|
|
struct ifreq ifr;
|
2004-02-04 22:57:41 +00:00
|
|
|
|
2004-09-17 11:30:40 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
2011-11-02 12:06:18 +00:00
|
|
|
char devname[IFNAMSIZ + 5]; /* "/dev/" + ifname */
|
|
|
|
int devnum;
|
|
|
|
struct ifaliasreq areq;
|
|
|
|
int fd;
|
2004-01-10 06:38:43 +00:00
|
|
|
#endif
|
2004-02-05 20:39:57 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
if (!(*tun = calloc(1, sizeof(struct tun_t)))) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "calloc() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return EOF;
|
|
|
|
}
|
2004-02-05 20:39:57 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
(*tun)->cb_ind = NULL;
|
|
|
|
(*tun)->addrs = 0;
|
|
|
|
(*tun)->routes = 0;
|
2004-02-05 20:39:57 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
/* Open the actual tun device */
|
|
|
|
if (((*tun)->fd = open("/dev/net/tun", O_RDWR)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "open() failed");
|
2017-08-12 13:06:19 +00:00
|
|
|
goto err_free;
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set device flags. For some weird reason this is also the method
|
|
|
|
used to obtain the network interface name */
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2017-08-12 13:07:02 +00:00
|
|
|
if (dev_name)
|
|
|
|
strcpy(ifr.ifr_name, dev_name);
|
2011-11-02 12:06:18 +00:00
|
|
|
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Tun device, no packet info */
|
|
|
|
if (ioctl((*tun)->fd, TUNSETIFF, (void *)&ifr) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "ioctl() failed");
|
2017-08-12 13:06:19 +00:00
|
|
|
goto err_close;
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strncpy((*tun)->devname, ifr.ifr_name, IFNAMSIZ);
|
|
|
|
(*tun)->devname[IFNAMSIZ - 1] = 0;
|
|
|
|
|
|
|
|
ioctl((*tun)->fd, TUNSETNOCSUM, 1); /* Disable checksums */
|
|
|
|
return 0;
|
2004-02-05 20:39:57 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined (__APPLE__)
|
2004-02-05 20:39:57 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
/* Find suitable device */
|
|
|
|
for (devnum = 0; devnum < 255; devnum++) { /* TODO 255 */
|
|
|
|
snprintf(devname, sizeof(devname), "/dev/tun%d", devnum);
|
|
|
|
if (((*tun)->fd = open(devname, O_RDWR)) >= 0)
|
|
|
|
break;
|
|
|
|
if (errno != EBUSY)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((*tun)->fd < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"Can't find tunnel device");
|
2017-08-12 13:06:19 +00:00
|
|
|
goto err_free;
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf((*tun)->devname, sizeof((*tun)->devname), "tun%d", devnum);
|
2017-08-11 10:56:30 +00:00
|
|
|
(*tun)->devname[sizeof((*tun)->devname)-1] = 0;
|
2011-11-02 12:06:18 +00:00
|
|
|
|
|
|
|
/* The tun device we found might have "old" IP addresses allocated */
|
|
|
|
/* We need to delete those. This problem is not present on Linux */
|
|
|
|
|
|
|
|
memset(&areq, 0, sizeof(areq));
|
|
|
|
|
|
|
|
/* Set up interface name */
|
|
|
|
strncpy(areq.ifra_name, (*tun)->devname, IFNAMSIZ);
|
|
|
|
areq.ifra_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */
|
|
|
|
|
|
|
|
/* Create a channel to the NET kernel. */
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
|
2017-08-12 13:06:19 +00:00
|
|
|
goto err_close;
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete any IP addresses until SIOCDIFADDR fails */
|
|
|
|
while (ioctl(fd, SIOCDIFADDR, (void *)&areq) != -1) ;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2004-01-26 23:06:05 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-12 13:06:19 +00:00
|
|
|
err_close:
|
|
|
|
close((*tun)->fd);
|
|
|
|
err_free:
|
|
|
|
free(*tun);
|
|
|
|
*tun = NULL;
|
|
|
|
return -1;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
int tun_free(struct tun_t *tun)
|
2002-12-16 13:33:51 +00:00
|
|
|
{
|
2004-12-30 15:33:58 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
if (tun->routes) {
|
|
|
|
tun_delroute(tun, &tun->dstaddr, &tun->addr, &tun->netmask);
|
|
|
|
}
|
2004-12-30 15:33:58 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
if (close(tun->fd)) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "close() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
}
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
/* TODO: For solaris we need to unlink streams */
|
2004-01-10 06:38:43 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
free(tun);
|
|
|
|
return 0;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
int tun_set_cb_ind(struct tun_t *this,
|
|
|
|
int (*cb_ind) (struct tun_t * tun, void *pack, unsigned len))
|
|
|
|
{
|
|
|
|
this->cb_ind = cb_ind;
|
|
|
|
return 0;
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
int tun_decaps(struct tun_t *this)
|
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
unsigned char buffer[PACKET_MAX];
|
|
|
|
int status;
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
if ((status = read(this->fd, buffer, sizeof(buffer))) <= 0) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno, "read() failed");
|
2011-11-02 12:06:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->cb_ind)
|
|
|
|
return this->cb_ind(this, buffer, status);
|
|
|
|
|
|
|
|
return 0;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tun_encaps(struct tun_t *tun, void *pack, unsigned len)
|
|
|
|
{
|
2011-11-02 12:06:18 +00:00
|
|
|
return write(tun->fd, pack, len);
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:06:18 +00:00
|
|
|
int tun_runscript(struct tun_t *tun, char *script)
|
|
|
|
{
|
|
|
|
|
|
|
|
char buf[TUN_SCRIPTSIZE];
|
|
|
|
char snet[TUN_ADDRSIZE];
|
|
|
|
char smask[TUN_ADDRSIZE];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
strncpy(snet, inet_ntoa(tun->addr), sizeof(snet));
|
|
|
|
snet[sizeof(snet) - 1] = 0;
|
|
|
|
strncpy(smask, inet_ntoa(tun->netmask), sizeof(smask));
|
|
|
|
smask[sizeof(smask) - 1] = 0;
|
|
|
|
|
|
|
|
/* system("ipup /dev/tun0 192.168.0.10 255.255.255.0"); */
|
|
|
|
snprintf(buf, sizeof(buf), "%s %s %s %s",
|
|
|
|
script, tun->devname, snet, smask);
|
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
rc = system(buf);
|
|
|
|
if (rc == -1) {
|
2014-12-04 15:32:37 +00:00
|
|
|
SYS_ERR(DTUN, LOGL_ERROR, errno,
|
2011-11-02 12:06:18 +00:00
|
|
|
"Error executing command %s", buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
2017-09-24 12:00:34 +00:00
|
|
|
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
|
|
|
/* obtain the link-local address of the tun device */
|
|
|
|
int tun_ipv6_linklocal_get(const struct tun_t *tun, struct in6_addr *ia)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddr, *ifa;
|
|
|
|
static const uint8_t ll_prefix[] = { 0xfe,0x80, 0,0, 0,0, 0,0 };
|
|
|
|
|
|
|
|
if (getifaddrs(&ifaddr) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
|
|
|
|
if (ifa->ifa_addr == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(ifa->ifa_name, tun->devname))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (memcmp(sin6->sin6_addr.s6_addr, ll_prefix, sizeof(ll_prefix)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*ia = sin6->sin6_addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|