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.
19 #include <sys/ioctl.h>
20 #include <netinet/ip.h>
27 * This is the portable version of the SSH tunnel forwarding, it
28 * uses some preprocessor definitions for various platform-specific
31 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
32 * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
33 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
34 * SSH_TUN_PREPEND_AF Prepend/remove the address family
38 * System-specific tunnel open function
41 #if defined(SSH_TUN_LINUX)
43 #include <linux/if_tun.h>
46 sys_tun_open(int tun, int mode)
50 const char *name = NULL;
52 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
53 debug("%s: failed to open tunnel control interface: %s",
54 __func__, strerror(errno));
58 bzero(&ifr, sizeof(ifr));
60 if (mode == SSH_TUNMODE_ETHERNET) {
61 ifr.ifr_flags = IFF_TAP;
64 ifr.ifr_flags = IFF_TUN;
67 ifr.ifr_flags |= IFF_NO_PI;
69 if (tun != SSH_TUNID_ANY) {
70 if (tun > SSH_TUNID_MAX) {
71 debug("%s: invalid tunnel id %x: %s", __func__,
72 tun, strerror(errno));
75 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
78 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
79 debug("%s: failed to configure tunnel (mode %d): %s", __func__,
80 mode, strerror(errno));
84 if (tun == SSH_TUNID_ANY)
85 debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
87 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
95 #endif /* SSH_TUN_LINUX */
97 #ifdef SSH_TUN_FREEBSD
98 #include <sys/socket.h>
101 #ifdef HAVE_NET_IF_TUN_H
102 #include <net/if_tun.h>
106 sys_tun_open(int tun, int mode)
110 int fd = -1, sock, flag;
111 const char *tunbase = "tun";
113 if (mode == SSH_TUNMODE_ETHERNET) {
115 debug("%s: no layer 2 tunnelling support", __func__);
122 /* Open the tunnel device */
123 if (tun <= SSH_TUNID_MAX) {
124 snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
125 fd = open(name, O_RDWR);
126 } else if (tun == SSH_TUNID_ANY) {
127 for (tun = 100; tun >= 0; tun--) {
128 snprintf(name, sizeof(name), "/dev/%s%d",
130 if ((fd = open(name, O_RDWR)) >= 0)
134 debug("%s: invalid tunnel %u\n", __func__, tun);
139 debug("%s: %s open failed: %s", __func__, name,
144 /* Turn on tunnel headers */
146 #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
147 if (mode != SSH_TUNMODE_ETHERNET &&
148 ioctl(fd, TUNSIFHEAD, &flag) == -1) {
149 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
155 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
157 /* Set the tunnel device operation mode */
158 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
159 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
162 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
164 ifr.ifr_flags |= IFF_UP;
165 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
176 debug("%s: failed to set %s mode %d: %s", __func__, name,
177 mode, strerror(errno));
180 #endif /* SSH_TUN_FREEBSD */
183 * System-specific channel filters
186 #if defined(SSH_TUN_FILTER)
187 #define OPENBSD_AF_INET 2
188 #define OPENBSD_AF_INET6 24
191 sys_tun_infilter(struct Channel *c, char *buf, int len)
193 #if defined(SSH_TUN_PREPEND_AF)
194 char rbuf[CHAN_RBUF];
200 #if defined(SSH_TUN_PREPEND_AF)
201 if (len <= 0 || len > (int)(sizeof(rbuf) - sizeof(*af)))
203 ptr = (char *)&rbuf[0];
204 bcopy(buf, ptr + sizeof(u_int32_t), len);
205 len += sizeof(u_int32_t);
206 af = (u_int32_t *)ptr;
208 iph = (struct ip *)(ptr + sizeof(u_int32_t));
220 #if defined(SSH_TUN_COMPAT_AF)
221 if (len < (int)sizeof(u_int32_t))
224 af = (u_int32_t *)ptr;
225 if (*af == htonl(AF_INET6))
226 *af = htonl(OPENBSD_AF_INET6);
228 *af = htonl(OPENBSD_AF_INET);
231 buffer_put_string(&c->input, ptr, len);
236 sys_tun_outfilter(struct Channel *c, u_char **data, u_int *dlen)
241 *data = buffer_get_string(&c->output, dlen);
242 if (*dlen < sizeof(*af))
246 #if defined(SSH_TUN_PREPEND_AF)
247 *dlen -= sizeof(u_int32_t);
248 buf = *data + sizeof(u_int32_t);
249 #elif defined(SSH_TUN_COMPAT_AF)
250 af = ntohl(*(u_int32_t *)buf);
251 if (*af == OPENBSD_AF_INET6)
252 *af = htonl(AF_INET6);
254 *af = htonl(AF_INET);
259 #endif /* SSH_TUN_FILTER */