2005-12-31 05:33:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 Reyk Floeter <reyk@openbsd.org>
|
|
|
|
*
|
|
|
|
* 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 "includes.h"
|
|
|
|
|
2006-07-10 11:33:04 +00:00
|
|
|
#include <sys/types.h>
|
2006-03-15 06:09:50 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-09-02 05:32:40 +00:00
|
|
|
|
2006-07-12 09:05:56 +00:00
|
|
|
#include <netinet/in.h>
|
2006-09-02 05:32:40 +00:00
|
|
|
#include <arpa/inet.h>
|
2006-03-15 06:09:50 +00:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
2006-07-12 12:40:50 +00:00
|
|
|
#include <errno.h>
|
2006-07-10 11:33:04 +00:00
|
|
|
#include <fcntl.h>
|
2006-08-05 05:47:26 +00:00
|
|
|
#include <stdarg.h>
|
2020-12-22 07:56:54 +00:00
|
|
|
#include <stdio.h>
|
2006-07-24 04:51:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2006-07-10 11:33:04 +00:00
|
|
|
|
2008-05-19 05:28:35 +00:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2005-12-31 05:33:36 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "misc.h"
|
2015-01-14 15:35:33 +00:00
|
|
|
#include "sshbuf.h"
|
2006-08-05 04:07:20 +00:00
|
|
|
#include "channels.h"
|
2015-01-14 15:35:33 +00:00
|
|
|
#include "ssherr.h"
|
2005-12-31 05:33:36 +00:00
|
|
|
|
2017-10-25 02:22:29 +00:00
|
|
|
/*
|
|
|
|
* This file contains various portability code for network support,
|
|
|
|
* including tun/tap forwarding and routing domains.
|
|
|
|
*/
|
|
|
|
|
2017-10-25 02:47:59 +00:00
|
|
|
#if defined(SYS_RDOMAIN_LINUX) || defined(SSH_TUN_LINUX)
|
|
|
|
#include <linux/if.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SYS_RDOMAIN_LINUX)
|
|
|
|
char *
|
|
|
|
sys_get_rdomain(int fd)
|
|
|
|
{
|
|
|
|
char dev[IFNAMSIZ + 1];
|
|
|
|
socklen_t len = sizeof(dev) - 1;
|
|
|
|
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, dev, &len) == -1) {
|
|
|
|
error("%s: cannot determine VRF for fd=%d : %s",
|
|
|
|
__func__, fd, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dev[len] = '\0';
|
|
|
|
return strdup(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_set_rdomain(int fd, const char *name)
|
|
|
|
{
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
name, strlen(name)) == -1) {
|
|
|
|
error("%s: setsockopt(%d, SO_BINDTODEVICE, %s): %s",
|
2021-04-03 06:47:37 +00:00
|
|
|
__func__, fd, name, strerror(errno));
|
2017-10-25 02:47:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-10-26 21:42:33 +00:00
|
|
|
sys_valid_rdomain(const char *name)
|
2017-10-25 02:47:59 +00:00
|
|
|
{
|
2017-10-25 03:09:56 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a pretty crappy way to test. It would be better to
|
|
|
|
* check whether "name" represents a VRF device, but apparently
|
|
|
|
* that requires an rtnetlink transaction.
|
|
|
|
*/
|
|
|
|
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
|
|
return 0;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
name, strlen(name)) == -1) {
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
2017-10-25 02:47:59 +00:00
|
|
|
}
|
|
|
|
#elif defined(SYS_RDOMAIN_XXX)
|
2017-10-25 02:22:29 +00:00
|
|
|
/* XXX examples */
|
|
|
|
char *
|
|
|
|
sys_get_rdomain(int fd)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_set_rdomain(int fd, const char *name)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
valid_rdomain(const char *name)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sys_set_process_rdomain(const char *name)
|
|
|
|
{
|
|
|
|
fatal("%s: not supported", __func__);
|
|
|
|
}
|
|
|
|
#endif /* defined(SYS_RDOMAIN_XXX) */
|
|
|
|
|
2005-12-31 05:33:36 +00:00
|
|
|
/*
|
|
|
|
* This is the portable version of the SSH tunnel forwarding, it
|
|
|
|
* uses some preprocessor definitions for various platform-specific
|
|
|
|
* settings.
|
|
|
|
*
|
|
|
|
* SSH_TUN_LINUX Use the (newer) Linux tun/tap device
|
2006-06-23 11:05:12 +00:00
|
|
|
* SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
|
2005-12-31 05:33:36 +00:00
|
|
|
* SSH_TUN_COMPAT_AF Translate the OpenBSD address family
|
|
|
|
* SSH_TUN_PREPEND_AF Prepend/remove the address family
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* System-specific tunnel open function
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(SSH_TUN_LINUX)
|
|
|
|
#include <linux/if_tun.h>
|
2020-01-25 05:30:22 +00:00
|
|
|
#define TUN_CTRL_DEV "/dev/net/tun"
|
2005-12-31 05:33:36 +00:00
|
|
|
|
|
|
|
int
|
2017-10-23 05:08:00 +00:00
|
|
|
sys_tun_open(int tun, int mode, char **ifname)
|
2005-12-31 05:33:36 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd = -1;
|
|
|
|
const char *name = NULL;
|
|
|
|
|
2017-10-23 05:08:00 +00:00
|
|
|
if (ifname != NULL)
|
|
|
|
*ifname = NULL;
|
2020-01-25 05:30:22 +00:00
|
|
|
if ((fd = open(TUN_CTRL_DEV, O_RDWR)) == -1) {
|
|
|
|
debug("%s: failed to open tunnel control device \"%s\": %s",
|
|
|
|
__func__, TUN_CTRL_DEV, strerror(errno));
|
2005-12-31 05:33:36 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:44:25 +00:00
|
|
|
bzero(&ifr, sizeof(ifr));
|
2005-12-31 05:33:36 +00:00
|
|
|
|
|
|
|
if (mode == SSH_TUNMODE_ETHERNET) {
|
|
|
|
ifr.ifr_flags = IFF_TAP;
|
|
|
|
name = "tap%d";
|
|
|
|
} else {
|
|
|
|
ifr.ifr_flags = IFF_TUN;
|
|
|
|
name = "tun%d";
|
|
|
|
}
|
|
|
|
ifr.ifr_flags |= IFF_NO_PI;
|
|
|
|
|
|
|
|
if (tun != SSH_TUNID_ANY) {
|
|
|
|
if (tun > SSH_TUNID_MAX) {
|
|
|
|
debug("%s: invalid tunnel id %x: %s", __func__,
|
|
|
|
tun, strerror(errno));
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
|
|
|
|
debug("%s: failed to configure tunnel (mode %d): %s", __func__,
|
|
|
|
mode, strerror(errno));
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tun == SSH_TUNID_ANY)
|
|
|
|
debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
|
|
|
|
else
|
|
|
|
debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
|
|
|
|
|
2018-04-13 03:38:06 +00:00
|
|
|
if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
|
2017-10-23 05:08:00 +00:00
|
|
|
goto failed;
|
|
|
|
|
2005-12-31 05:33:36 +00:00
|
|
|
return (fd);
|
|
|
|
|
|
|
|
failed:
|
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
#endif /* SSH_TUN_LINUX */
|
|
|
|
|
2006-01-01 08:47:05 +00:00
|
|
|
#ifdef SSH_TUN_FREEBSD
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
2006-06-23 11:05:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_NET_IF_TUN_H
|
2006-01-01 08:47:05 +00:00
|
|
|
#include <net/if_tun.h>
|
2006-06-23 11:05:12 +00:00
|
|
|
#endif
|
2006-01-01 08:47:05 +00:00
|
|
|
|
|
|
|
int
|
2017-10-23 05:08:00 +00:00
|
|
|
sys_tun_open(int tun, int mode, char **ifname)
|
2006-01-01 08:47:05 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
char name[100];
|
2020-08-04 04:59:21 +00:00
|
|
|
int fd = -1, sock;
|
2006-01-01 08:47:05 +00:00
|
|
|
const char *tunbase = "tun";
|
2020-08-04 04:59:21 +00:00
|
|
|
#if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
|
|
|
|
int flag;
|
|
|
|
#endif
|
2006-01-01 08:47:05 +00:00
|
|
|
|
2017-10-23 05:08:00 +00:00
|
|
|
if (ifname != NULL)
|
|
|
|
*ifname = NULL;
|
|
|
|
|
2006-01-01 08:47:05 +00:00
|
|
|
if (mode == SSH_TUNMODE_ETHERNET) {
|
|
|
|
#ifdef SSH_TUN_NO_L2
|
|
|
|
debug("%s: no layer 2 tunnelling support", __func__);
|
|
|
|
return (-1);
|
|
|
|
#else
|
|
|
|
tunbase = "tap";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the tunnel device */
|
|
|
|
if (tun <= SSH_TUNID_MAX) {
|
|
|
|
snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
|
|
|
|
fd = open(name, O_RDWR);
|
|
|
|
} else if (tun == SSH_TUNID_ANY) {
|
|
|
|
for (tun = 100; tun >= 0; tun--) {
|
|
|
|
snprintf(name, sizeof(name), "/dev/%s%d",
|
|
|
|
tunbase, tun);
|
|
|
|
if ((fd = open(name, O_RDWR)) >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug("%s: invalid tunnel %u\n", __func__, tun);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
debug("%s: %s open failed: %s", __func__, name,
|
|
|
|
strerror(errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn on tunnel headers */
|
|
|
|
#if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
|
2020-08-04 04:59:21 +00:00
|
|
|
flag = 1;
|
2006-01-01 08:47:05 +00:00
|
|
|
if (mode != SSH_TUNMODE_ETHERNET &&
|
|
|
|
ioctl(fd, TUNSIFHEAD, &flag) == -1) {
|
|
|
|
debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
|
|
|
|
|
|
|
|
/* Set the tunnel device operation mode */
|
|
|
|
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
|
|
|
|
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
|
|
|
|
goto failed;
|
2010-08-10 02:47:40 +00:00
|
|
|
if ((ifr.ifr_flags & IFF_UP) == 0) {
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
|
|
|
|
goto failed;
|
|
|
|
}
|
2006-01-01 08:47:05 +00:00
|
|
|
|
2018-04-13 03:38:06 +00:00
|
|
|
if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
|
2017-10-23 05:08:00 +00:00
|
|
|
goto failed;
|
|
|
|
|
2006-01-01 08:47:05 +00:00
|
|
|
close(sock);
|
|
|
|
return (fd);
|
|
|
|
|
|
|
|
failed:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
if (sock >= 0)
|
|
|
|
close(sock);
|
|
|
|
debug("%s: failed to set %s mode %d: %s", __func__, name,
|
|
|
|
mode, strerror(errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
#endif /* SSH_TUN_FREEBSD */
|
|
|
|
|
2005-12-31 05:33:36 +00:00
|
|
|
/*
|
|
|
|
* System-specific channel filters
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(SSH_TUN_FILTER)
|
2017-07-21 04:38:16 +00:00
|
|
|
/*
|
|
|
|
* The tunnel forwarding protocol prepends the address family of forwarded
|
|
|
|
* IP packets using OpenBSD's numbers.
|
|
|
|
*/
|
2005-12-31 05:33:36 +00:00
|
|
|
#define OPENBSD_AF_INET 2
|
|
|
|
#define OPENBSD_AF_INET6 24
|
|
|
|
|
|
|
|
int
|
2017-09-12 08:01:35 +00:00
|
|
|
sys_tun_infilter(struct ssh *ssh, struct Channel *c, char *buf, int _len)
|
2005-12-31 05:33:36 +00:00
|
|
|
{
|
2017-07-21 04:38:16 +00:00
|
|
|
int r;
|
|
|
|
size_t len;
|
|
|
|
char *ptr = buf;
|
2005-12-31 05:33:36 +00:00
|
|
|
#if defined(SSH_TUN_PREPEND_AF)
|
|
|
|
char rbuf[CHAN_RBUF];
|
2017-07-21 04:38:16 +00:00
|
|
|
struct ip iph;
|
2005-12-31 05:33:36 +00:00
|
|
|
#endif
|
2017-07-21 04:38:16 +00:00
|
|
|
#if defined(SSH_TUN_PREPEND_AF) || defined(SSH_TUN_COMPAT_AF)
|
|
|
|
u_int32_t af;
|
2005-12-31 05:33:36 +00:00
|
|
|
#endif
|
|
|
|
|
2017-07-21 04:38:16 +00:00
|
|
|
/* XXX update channel input filter API to use unsigned length */
|
|
|
|
if (_len < 0)
|
|
|
|
return -1;
|
|
|
|
len = _len;
|
2005-12-31 05:33:36 +00:00
|
|
|
|
2017-07-21 04:38:16 +00:00
|
|
|
#if defined(SSH_TUN_PREPEND_AF)
|
|
|
|
if (len <= sizeof(iph) || len > sizeof(rbuf) - 4)
|
|
|
|
return -1;
|
|
|
|
/* Determine address family from packet IP header. */
|
|
|
|
memcpy(&iph, buf, sizeof(iph));
|
|
|
|
af = iph.ip_v == 6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET;
|
|
|
|
/* Prepend address family to packet using OpenBSD constants */
|
|
|
|
memcpy(rbuf + 4, buf, len);
|
|
|
|
len += 4;
|
|
|
|
POKE_U32(rbuf, af);
|
|
|
|
ptr = rbuf;
|
|
|
|
#elif defined(SSH_TUN_COMPAT_AF)
|
|
|
|
/* Convert existing address family header to OpenBSD value */
|
|
|
|
if (len <= 4)
|
|
|
|
return -1;
|
|
|
|
af = PEEK_U32(buf);
|
|
|
|
/* Put it back */
|
|
|
|
POKE_U32(buf, af == AF_INET6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET);
|
2005-12-31 05:33:36 +00:00
|
|
|
#endif
|
2006-01-01 08:47:05 +00:00
|
|
|
|
2017-09-12 08:01:35 +00:00
|
|
|
if ((r = sshbuf_put_string(c->input, ptr, len)) != 0)
|
2015-01-14 15:35:33 +00:00
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
2005-12-31 05:33:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_char *
|
2017-09-12 08:01:35 +00:00
|
|
|
sys_tun_outfilter(struct ssh *ssh, struct Channel *c,
|
|
|
|
u_char **data, size_t *dlen)
|
2005-12-31 05:33:36 +00:00
|
|
|
{
|
|
|
|
u_char *buf;
|
2017-07-21 04:38:16 +00:00
|
|
|
u_int32_t af;
|
2015-01-14 15:35:33 +00:00
|
|
|
int r;
|
2005-12-31 05:33:36 +00:00
|
|
|
|
2015-01-14 16:08:29 +00:00
|
|
|
/* XXX new API is incompatible with this signature. */
|
2017-09-12 08:01:35 +00:00
|
|
|
if ((r = sshbuf_get_string(c->output, data, dlen)) != 0)
|
2015-01-14 15:35:33 +00:00
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
2017-07-21 04:38:16 +00:00
|
|
|
if (*dlen < sizeof(af))
|
2005-12-31 05:33:36 +00:00
|
|
|
return (NULL);
|
|
|
|
buf = *data;
|
|
|
|
|
|
|
|
#if defined(SSH_TUN_PREPEND_AF)
|
2017-07-21 04:38:16 +00:00
|
|
|
/* skip address family */
|
|
|
|
*dlen -= sizeof(af);
|
|
|
|
buf = *data + sizeof(af);
|
2005-12-31 05:33:36 +00:00
|
|
|
#elif defined(SSH_TUN_COMPAT_AF)
|
2017-07-21 04:38:16 +00:00
|
|
|
/* translate address family */
|
|
|
|
af = (PEEK_U32(buf) == OPENBSD_AF_INET6) ? AF_INET6 : AF_INET;
|
|
|
|
POKE_U32(buf, af);
|
2005-12-31 05:33:36 +00:00
|
|
|
#endif
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
#endif /* SSH_TUN_FILTER */
|