2 * Copyright (c) 2005 Reyk Floeter <reyk@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * This is the portable version of the SSH tunnel forwarding, it
25 * uses some preprocessor definitions for various platform-specific
28 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
29 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
30 * SSH_TUN_PREPEND_AF Prepend/remove the address family
34 * System-specific tunnel open function
37 #if defined(SSH_TUN_LINUX)
38 #include <linux/if_tun.h>
41 sys_tun_open(int tun, int mode)
45 const char *name = NULL;
47 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
48 debug("%s: failed to open tunnel control interface: %s",
49 __func__, strerror(errno));
53 bzero(&ifr, sizeof(ifr));
55 if (mode == SSH_TUNMODE_ETHERNET) {
56 ifr.ifr_flags = IFF_TAP;
59 ifr.ifr_flags = IFF_TUN;
62 ifr.ifr_flags |= IFF_NO_PI;
64 if (tun != SSH_TUNID_ANY) {
65 if (tun > SSH_TUNID_MAX) {
66 debug("%s: invalid tunnel id %x: %s", __func__,
67 tun, strerror(errno));
70 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
73 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
74 debug("%s: failed to configure tunnel (mode %d): %s", __func__,
75 mode, strerror(errno));
79 if (tun == SSH_TUNID_ANY)
80 debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
82 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
90 #endif /* SSH_TUN_LINUX */
93 * System-specific channel filters
96 #if defined(SSH_TUN_FILTER)
97 #define OPENBSD_AF_INET 2
98 #define OPENBSD_AF_INET6 24
101 sys_tun_infilter(struct Channel *c, char *buf, int len)
103 #if defined(SSH_TUN_PREPEND_AF)
104 char rbuf[CHAN_RBUF];
109 #if defined(SSH_TUN_PREPEND_AF)
110 if (len > (int)(sizeof(rbuf) - sizeof(*af)))
112 ptr = (char *)&rbuf[0];
113 bcopy(buf, ptr + sizeof(u_int32_t), len);
114 len += sizeof(u_int32_t);
117 #if defined(SSH_TUN_COMPAT_AF)
118 if (len < (int)sizeof(u_int32_t))
121 af = (u_int32_t *)ptr;
122 if (*af == htonl(AF_INET6))
123 *af = htonl(OPENBSD_AF_INET6);
125 *af = htonl(OPENBSD_AF_INET);
127 buffer_put_string(&c->input, ptr, len);
132 sys_tun_outfilter(struct Channel *c, u_char **data, u_int *dlen)
137 *data = buffer_get_string(&c->output, dlen);
138 if (*dlen < sizeof(*af))
142 #if defined(SSH_TUN_PREPEND_AF)
143 *dlen -= sizeof(u_int32_t);
144 buf = *data + sizeof(u_int32_t);
145 #elif defined(SSH_TUN_COMPAT_AF)
146 af = ntohl(*(u_int32_t *)buf);
147 if (*af == OPENBSD_AF_INET6)
148 *af = htonl(AF_INET6);
150 *af = htonl(AF_INET);
155 #endif /* SSH_TUN_FILTER */