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/types.h>
20 #include <sys/ioctl.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netinet/ip.h>
32 #include "openbsd-compat/sys-queue.h"
40 * This is the portable version of the SSH tunnel forwarding, it
41 * uses some preprocessor definitions for various platform-specific
44 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
45 * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
46 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
47 * SSH_TUN_PREPEND_AF Prepend/remove the address family
51 * System-specific tunnel open function
54 #if defined(SSH_TUN_LINUX)
56 #include <linux/if_tun.h>
59 sys_tun_open(int tun
, int mode
)
63 const char *name
= NULL
;
65 if ((fd
= open("/dev/net/tun", O_RDWR
)) == -1) {
66 debug("%s: failed to open tunnel control interface: %s",
67 __func__
, strerror(errno
));
71 bzero(&ifr
, sizeof(ifr
));
73 if (mode
== SSH_TUNMODE_ETHERNET
) {
74 ifr
.ifr_flags
= IFF_TAP
;
77 ifr
.ifr_flags
= IFF_TUN
;
80 ifr
.ifr_flags
|= IFF_NO_PI
;
82 if (tun
!= SSH_TUNID_ANY
) {
83 if (tun
> SSH_TUNID_MAX
) {
84 debug("%s: invalid tunnel id %x: %s", __func__
,
85 tun
, strerror(errno
));
88 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), name
, tun
);
91 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
92 debug("%s: failed to configure tunnel (mode %d): %s", __func__
,
93 mode
, strerror(errno
));
97 if (tun
== SSH_TUNID_ANY
)
98 debug("%s: tunnel mode %d fd %d", __func__
, mode
, fd
);
100 debug("%s: %s mode %d fd %d", __func__
, ifr
.ifr_name
, mode
, fd
);
108 #endif /* SSH_TUN_LINUX */
110 #ifdef SSH_TUN_FREEBSD
111 #include <sys/socket.h>
114 #ifdef HAVE_NET_IF_TUN_H
115 #include <net/tun/if_tun.h>
119 sys_tun_open(int tun
, int mode
)
123 int fd
= -1, sock
, flag
;
124 const char *tunbase
= "tun";
126 if (mode
== SSH_TUNMODE_ETHERNET
) {
128 debug("%s: no layer 2 tunnelling support", __func__
);
135 /* Open the tunnel device */
136 if (tun
<= SSH_TUNID_MAX
) {
137 snprintf(name
, sizeof(name
), "/dev/%s%d", tunbase
, tun
);
138 fd
= open(name
, O_RDWR
);
139 } else if (tun
== SSH_TUNID_ANY
) {
140 for (tun
= 100; tun
>= 0; tun
--) {
141 snprintf(name
, sizeof(name
), "/dev/%s%d",
143 if ((fd
= open(name
, O_RDWR
)) >= 0)
147 debug("%s: invalid tunnel %u\n", __func__
, tun
);
152 debug("%s: %s open failed: %s", __func__
, name
,
157 /* Turn on tunnel headers */
159 #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
160 if (mode
!= SSH_TUNMODE_ETHERNET
&&
161 ioctl(fd
, TUNSIFHEAD
, &flag
) == -1) {
162 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__
, fd
,
168 debug("%s: %s mode %d fd %d", __func__
, name
, mode
, fd
);
170 /* Set the tunnel device operation mode */
171 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", tunbase
, tun
);
172 if ((sock
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1)
175 if (ioctl(sock
, SIOCGIFFLAGS
, &ifr
) == -1)
177 if ((ifr
.ifr_flags
& IFF_UP
) == 0) {
178 ifr
.ifr_flags
|= IFF_UP
;
179 if (ioctl(sock
, SIOCSIFFLAGS
, &ifr
) == -1)
191 debug("%s: failed to set %s mode %d: %s", __func__
, name
,
192 mode
, strerror(errno
));
195 #endif /* SSH_TUN_FREEBSD */
198 * System-specific channel filters
201 #if defined(SSH_TUN_FILTER)
202 #define OPENBSD_AF_INET 2
203 #define OPENBSD_AF_INET6 24
206 sys_tun_infilter(struct Channel
*c
, char *buf
, int len
)
208 #if defined(SSH_TUN_PREPEND_AF)
209 char rbuf
[CHAN_RBUF
];
216 #if defined(SSH_TUN_PREPEND_AF)
217 if (len
<= 0 || len
> (int)(sizeof(rbuf
) - sizeof(*af
)))
219 ptr
= (char *)&rbuf
[0];
220 bcopy(buf
, ptr
+ sizeof(u_int32_t
), len
);
221 len
+= sizeof(u_int32_t
);
222 af
= (u_int32_t
*)ptr
;
224 iph
= (struct ip
*)(ptr
+ sizeof(u_int32_t
));
236 #if defined(SSH_TUN_COMPAT_AF)
237 if (len
< (int)sizeof(u_int32_t
))
240 af
= (u_int32_t
*)ptr
;
241 if (*af
== htonl(AF_INET6
))
242 *af
= htonl(OPENBSD_AF_INET6
);
244 *af
= htonl(OPENBSD_AF_INET
);
247 if ((r
= sshbuf_put_string(&c
->input
, ptr
, len
)) != 0)
248 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
253 sys_tun_outfilter(struct Channel
*c
, u_char
**data
, u_int
*dlen
)
260 /* XXX new API is incompatible with this signature. */
261 if ((r
= sshbuf_get_string(&c
->output
, data
, &xxx_dlen
)) != 0)
262 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
265 if (*dlen
< sizeof(*af
))
269 #if defined(SSH_TUN_PREPEND_AF)
270 *dlen
-= sizeof(u_int32_t
);
271 buf
= *data
+ sizeof(u_int32_t
);
272 #elif defined(SSH_TUN_COMPAT_AF)
273 af
= ntohl(*(u_int32_t
*)buf
);
274 if (*af
== OPENBSD_AF_INET6
)
275 *af
= htonl(AF_INET6
);
277 *af
= htonl(AF_INET
);
282 #endif /* SSH_TUN_FILTER */